This project provides an AWS CDK construct that creates a CodeBuild project for running GitHub Actions workflows.
The CodebuildHostedGitHubActionsRunner
construct allows you to easily set up an AWS CodeBuild project that can be used as a runner for GitHub Actions. This enables you to leverage AWS infrastructure for your GitHub Actions workflows, providing a scalable and cost-effective solution for CI/CD pipelines.
.
├── API.md
├── package.json
├── README.md
├── src
│ └── index.ts
└── tsconfig.dev.json
-
src/index.ts
: Contains the main implementation of theCodebuildHostedGitHubActionsRunner
construct. -
package.json
: Defines the project dependencies and scripts. -
tsconfig.dev.json
: TypeScript configuration for development.
To use this construct in your AWS CDK project, install it via npm:
npm install @nikovirtala/cdk-codebuild-hosted-github-actions-runner
Here's a basic example of how to use the CodebuildHostedGitHubActionsRunner
construct in your CDK stack:
import { Stack, StackProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { CodebuildHostedGitHubActionsRunner } from '@nikovirtala/cdk-codebuild-hosted-github-actions-runner';
export class MyStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
new CodebuildHostedGitHubActionsRunner(this, 'GitHubActionsRunner', {
repositoryOwner: 'myorg',
repositoryName: 'myrepo',
tokenSecretArn: 'arn:aws:secretsmanager:region:account-id:secret:my-github-token',
});
}
}
The CodebuildHostedGitHubActionsRunner
construct accepts the following properties:
-
codeBuildProjectName
(optional): The name of the CodeBuild project. If not provided, a name is generated based on the repository owner and name. -
repositoryOwner
(required): The owner of the GitHub repository. -
repositoryName
(required): The name of the GitHub repository. -
tokenSecretArn
(optional): The ARN of the Secrets Manager secret containing the GitHub token. This is required if the GitHub repository is private.
To use this CodeBuild project as a runner for your GitHub Actions workflows, you'll need to configure your workflow to use self-hosted runners. Here's an example workflow:
name: My Workflow
on:
push:
branches: [ main ]
jobs:
build:
runs-on: self-hosted
steps:
- uses: actions/checkout@v2
- name: Run a one-line script
run: echo 'Hello, world!'
-
GitHub Token Not Recognized
- Problem: The CodeBuild project fails to authenticate with GitHub.
- Error Message: "Unable to access the repository. Please check the source definition of your project."
- Solution:
- Verify that the GitHub token is correctly stored in AWS Secrets Manager.
- Ensure the
tokenSecretArn
property is correctly set in the construct. - Check if the token has the necessary permissions for the repository.
-
CodeBuild Project Creation Fails
- Problem: The CDK deployment fails when creating the CodeBuild project.
- Error Message: "Resource handler returned message: "Invalid request provided" (RequestToken: ...)"
- Solution:
- Check if you have the necessary permissions to create CodeBuild projects.
- Verify that the provided repository owner and name are correct.
- Ensure you're not exceeding any AWS account limits for CodeBuild projects.
To enable verbose logging for the CDK deployment:
cdk deploy --debug
This will provide more detailed information about the deployment process and any errors encountered.
The CodebuildHostedGitHubActionsRunner
construct sets up a CodeBuild project that integrates with GitHub Actions. Here's how the data flows through the system:
- GitHub Action Workflow Triggered
- GitHub sends a webhook event to CodeBuild
- CodeBuild Project receives the webhook
- CodeBuild Project clones the GitHub repository
- CodeBuild executes the GitHub Actions workflow
- Results are sent back to GitHub
graph TD
A[GitHub] -->|webhook| B[CodeBuild Project]
B -->|clone| C[GitHub Repo]
B -->|execute workflow| D[Execute Workflow]
D -->|results| B
B -->|results| A
Note: If a GitHub token is provided, the construct creates a GitHub source credential in CodeBuild to allow access to private repositories.
The CodebuildHostedGitHubActionsRunner
construct creates the following AWS resources:
-
AWS CodeBuild Project:
- Type:
aws_codebuild.Project
- Purpose: Runs GitHub Actions workflows
- Type:
-
GitHub Source Credentials (if token provided):
- Type:
aws_codebuild.GitHubSourceCredentials
- Purpose: Authenticates CodeBuild with GitHub for private repositories
- Type:
The construct also references an existing AWS Secrets Manager secret if a tokenSecretArn
is provided.