Manage GitHub resources like repositories, teams, members, integrations and workflows with the AWS CDK as Custom Resources in CloudFormation with cdk-github.
You configure the endpoint, method and parameters documented by @octokit/rest and AWS CloudFormation runs them anytime you create, update (if you changed the custom resource), or delete stacks. When CloudFormation sends a lifecycle event notification, then your custom resource sends the request to the GitHub REST API.
TypeScript
npm install @pepperize/cdk-github
or
yarn add @pepperize/cdk-github
Python
pip install pepperize.cdk-github
C#
dotnet add package Pepperize.CDK.Github
Java
<dependency>
<groupId>com.pepperize</groupId>
<artifactId>cdk-github</artifactId>
<version>${cdkGithub.version}</version>
</dependency>
Contributions of all kinds are welcome 🚀 Check out our contributor's guide.
For a quick start, fork and check out a development environment:
git clone git@github.com:pepperize/cdk-github
cd cdk-github
# install dependencies
yarn
# build with projen
yarn build
-
Create an AWS Secrets Manager secret
{ "appId": "123456", "privateKey": "-----BEGIN RSA PRIVATE KEY-----\nExample==\n-----END RSA PRIVATE KEY-----", "installationId": "12345678" }
-
Add @pepperize/cdk-github to your project dependencies
yarn add @pepperize/cdk-github
-
Add your
main.ts
const app = new App(); const stack = new Stack(app, "GithubCustomResources");
Just for simplicity, it's up to you how to organize your app 😉
-
Import your secret
const secret = secrets_manager.Secret.fromSecretNameV2(stack, "Auth", "cdk-github/github-token");
-
Configure GitHub App authenticate as an installation
const authOptions = AuthOptions.appAuth(secret);
-
Add your first GitHub Custom Resource with the AWS CDK
new GithubCustomResource(stack, "GithubRepo", { onCreate: { // 👇The endpoint of the GitHub API. endpoint: "repos", // 👇The method of the GitHub API. method: "createInOrg", // https://octokit.github.io/rest.js/v19/#repos-create-in-org parameters: { // 👇The request parameters to send. org: "pepperize", name: "cdk-github", }, // 👇The object keys from the GitHub API response to return to CFN. outputPaths: ["id", "full_name"], // 👇This becomes the CFN Physical ID visible in the Console. physicalResourceId: custom_resources.PhysicalResourceId.fromResponse("full_name"), // 👇Don't throw an error if message matching this regex. ignoreErrorCodesMatching: "name already exists on this account", }, // 👇The implemented authentication strategy. authOptions: AuthOptions.appAuth(secret), });
-
Deploy your first GitHub Custom Resource
npx cdk deploy
Configure the AWS SecretsManager Secret with the AuthOptions that will be passed to octokit.auth
. i.e. as an installation:
{
"appId": "123456",
"privateKey": "-----BEGIN RSA PRIVATE KEY-----\nExample==\n-----END RSA PRIVATE KEY-----",
"installationId": "12345678"
}
Lookup the secret in your AWS CDK app:
// 👇Lookup your secret containing the AuthOptions
const secret = secrets_manager.Secret.fromSecretNameV2(stack, "Auth", "cdk-github/github-token");
// 👇This will send the secret arn to the custom resource handler
const authOptions = AuthOptions.appAuth(secret);
The custom resource handler will configure octokit.js with the createAppAuth
:
const getSecretValueResponse = await SSM.getSecretValue({ SecretId: secret }).promise();
const octokitOptions: OctokitOptions = {
authStrategy: createAppAuth,
auth: (auth = JSON.parse(getSecretValueResponse.SecretString)),
};
Supported through @octokit/auth-app
Just add your PAT to an SSM StringParameter
// 👇Lookup your parameter containing the TOKEN
const parameter = ssm.StringParameter.fromStringParameterName(stack, "Auth", "cdk-github/github-token");
// 👇This will send the parameter arn to the custom resource handler
const authOptions = AuthOptions.tokenAuth(parameter);
Supported through @octokit/auth-token
// 👇This will configure octokit without authentication
const authOptions = AuthOptions.unauthenticated();
@octokit/plugin-rest-endpoint-methods
const auth = secrets_manager.Secret.fromSecretNameV2(stack, "Auth", "cdk-github/github-token");
const repo = new GithubCustomResource(stack, "GithubRepo", {
onCreate: {
// https://octokit.github.io/rest.js/v19/#repos-create-in-org
endpoint: "repos",
method: "createInOrg",
parameters: {
org: "pepperize",
name: "cdk-github",
},
outputPaths: ["id", "full_name"],
physicalResourceId: custom_resources.PhysicalResourceId.fromResponse("full_name"),
ignoreErrorCodesMatching: "name already exists on this account",
},
onUpdate: {
// https://octokit.github.io/rest.js/v19#repos-get
endpoint: "repos",
method: "get",
parameters: {
owner: "pepperize",
repo: "cdk-github",
},
outputPaths: ["id", "full_name"],
physicalResourceId: custom_resources.PhysicalResourceId.fromResponse("full_name"),
},
onDelete: {
// https://octokit.github.io/rest.js/v19#repos-delete
endpoint: "repos",
method: "delete",
parameters: {
owner: "pepperize",
repo: "cdk-github",
},
outputPaths: [],
},
authOptions: AuthOptions.appAuth(auth),
});
// 👇 This will return the created repository id as a CDK Token
repo.getAtt("id");
Manages an environment secret. Will fetch the source AWS SecretsManager secret and encrypt it to store in GitHub.
// 👇The GitHub API authentication secret
const auth = secrets_manager.Secret.fromSecretNameV2(scope, "Auth", "cdk-github/github-token");
// 👇The AWS SecretsManager Secret to configure as GitHub Action secret.
const secret = secrets_manager.Secret.fromSecretNameV2(scope, "Secret", "any-secret/example");
new GithubActionsSecretEnvironment(scope, "GithubRepo", {
// 👇The repository id, which you may lookup from the page source or via a custom resource
repositoryId: "558989134",
environmentName: "production",
// 👇The name of the created GitHub secret
secretName: "example",
// 👇The source AWS SecretsManager secret and JSON field to use
source: GithubActionsSecret.fromSecretsManager(secret, "some-json-field"),
authOptions: AuthOptions.appAuth(auth),
// 👇Whether to delete or retain the GitHub secret on resource removal
removalPolicy: RemovalPolicy.DESTROY,
});
You may retrieve the
repository_id
from the GitHub Repository page source's meta tag i.e.<meta name="octolytics-dimension-repository_id" content="558989134">
or from anotherGithubCustomResource
viagetAtt()
.
See GitHub Developer Guide, API Reference
Manage an GitHib Actions organization secret. Will fetch the source AWS SecretsManager secret and encrypt it to store in GitHub.
// 👇The GitHub API authentication secret
const auth = secrets_manager.Secret.fromSecretNameV2(scope, "Auth", "cdk-github/github-token");
// 👇The AWS SecretsManager Secret to configure as GitHub Action secret.
const secret = secrets_manager.Secret.fromSecretNameV2(scope, "Secret", "any-secret/example");
new GithubActionsSecretOrganization(scope, "GithubRepo", {
organizationName: "pepperize",
// 👇The name of the created GitHub secret
secretName: "example",
// 👇The source AWS SecretsManager secret and JSON field to use
source: GithubActionsSecret.fromSecretsManager(secret, "some-json-field"),
visibility: Visibility.ALL,
authOptions: AuthOptions.appAuth(auth),
// 👇Whether to delete or retain the GitHub secret on resource removal
removalPolicy: RemovalPolicy.DESTROY,
});
See GitHub Developer Guide, API Reference
Manage an GitHib Actions Repository secret. Will fetch the source AWS SecretsManager secret and encrypt it to store in GitHub.
// 👇The GitHub API authentication secret
const auth = secrets_manager.Secret.fromSecretNameV2(scope, "Auth", "cdk-github/github-token");
// 👇The AWS SecretsManager Secret to configure as GitHub Action secret.
const secret = secrets_manager.Secret.fromSecretNameV2(scope, "Secret", "any-secret/example");
new GithubActionsSecretRepository(scope, "GithubRepo", {
owner: "pepperize",
repositoryName: "cdk-github",
// 👇The name of the created GitHub secret
secretName: "example",
// 👇The source AWS SecretsManager secret and JSON field to use
source: GithubActionsSecret.fromSecretsManager(secret, "some-json-field"),
authOptions: AuthOptions.appAuth(auth),
// 👇Whether to delete or retain the GitHub secret on resource removal
removalPolicy: RemovalPolicy.DESTROY,
});
See GitHub Developer Guide, API Reference
new GithubCustomResource(scope, "GithubRepo", {
onCreate: {
// https://octokit.github.io/rest.js/v19/#repos-create-in-org
endpoint: "repos",
method: "createInOrg",
parameters: {
org: "pepperize",
name: "cdk-github",
},
outputPaths: ["id", "full_name"],
physicalResourceId: custom_resources.PhysicalResourceId.fromResponse("full_name"),
ignoreErrorCodesMatching: "name already exists on this account",
},
onUpdate: {
// https://octokit.github.io/rest.js/v19#repos-get
endpoint: "repos",
method: "get",
parameters: {
owner: "pepperize",
repo: "cdk-github",
},
outputPaths: ["id", "full_name"],
physicalResourceId: custom_resources.PhysicalResourceId.fromResponse("full_name"),
},
onDelete: {
// https://octokit.github.io/rest.js/v19#repos-delete
endpoint: "repos",
method: "delete",
parameters: {
owner: "pepperize",
repo: "cdk-github",
},
outputPaths: [],
},
authOptions: AuthOptions.appAuth(auth),
});
import { GithubCustomResource } from '@pepperize/cdk-github'
new GithubCustomResource(scope: Construct, id: string, props: GithubCustomResourceProps)
Name | Type | Description |
---|---|---|
scope |
constructs.Construct |
No description. |
id |
string |
No description. |
props |
GithubCustomResourceProps |
No description. |
- Type: constructs.Construct
- Type: string
Name | Description |
---|---|
toString |
Returns a string representation of this construct. |
getAtt |
Returns the value of an attribute of the custom resource of an arbitrary type. |
getAttString |
Returns the value of an attribute of the custom resource of type string. |
public toString(): string
Returns a string representation of this construct.
public getAtt(attributeName: string): Reference
Returns the value of an attribute of the custom resource of an arbitrary type.
Attributes are returned from the custom resource provider through the
Data
map where the key is the attribute name.
- Type: string
the name of the attribute.
public getAttString(attributeName: string): string
Returns the value of an attribute of the custom resource of type string.
Attributes are returned from the custom resource provider through the
Data
map where the key is the attribute name.
- Type: string
the name of the attribute.
Name | Description |
---|---|
isConstruct |
Checks if x is a construct. |
import { GithubCustomResource } from '@pepperize/cdk-github'
GithubCustomResource.isConstruct(x: any)
Checks if x
is a construct.
- Type: any
Any object.
Name | Type | Description |
---|---|---|
node |
constructs.Node |
The tree node. |
ref |
string |
The physical name of this custom resource. |
public readonly node: Node;
- Type: constructs.Node
The tree node.
public readonly ref: string;
- Type: string
The physical name of this custom resource.
import { GithubCustomResourceBase } from '@pepperize/cdk-github'
new GithubCustomResourceBase(scope: Construct, id: string)
Name | Type | Description |
---|---|---|
scope |
constructs.Construct |
The scope in which to define this construct. |
id |
string |
The scoped construct ID. |
- Type: constructs.Construct
The scope in which to define this construct.
- Type: string
The scoped construct ID.
Must be unique amongst siblings. If
the ID includes a path separator (/
), then it will be replaced by double
dash --
.
Name | Description |
---|---|
toString |
Returns a string representation of this construct. |
getAtt |
Returns the value of an attribute of the custom resource of an arbitrary type. |
getAttString |
Returns the value of an attribute of the custom resource of type string. |
public toString(): string
Returns a string representation of this construct.
public getAtt(attributeName: string): Reference
Returns the value of an attribute of the custom resource of an arbitrary type.
Attributes are returned from the custom resource provider through the
Data
map where the key is the attribute name.
- Type: string
the name of the attribute.
public getAttString(attributeName: string): string
Returns the value of an attribute of the custom resource of type string.
Attributes are returned from the custom resource provider through the
Data
map where the key is the attribute name.
- Type: string
the name of the attribute.
Name | Description |
---|---|
isConstruct |
Checks if x is a construct. |
import { GithubCustomResourceBase } from '@pepperize/cdk-github'
GithubCustomResourceBase.isConstruct(x: any)
Checks if x
is a construct.
- Type: any
Any object.
Name | Type | Description |
---|---|---|
node |
constructs.Node |
The tree node. |
ref |
string |
The physical name of this custom resource. |
public readonly node: Node;
- Type: constructs.Node
The tree node.
public readonly ref: string;
- Type: string
The physical name of this custom resource.
import { GithubApiCall } from '@pepperize/cdk-github'
const githubApiCall: GithubApiCall = { ... }
Name | Type | Description |
---|---|---|
endpoint |
string |
The endpoint to call. |
method |
string |
The method to call. |
ignoreErrorCodesMatching |
string |
The regex pattern to use to catch API errors. |
outputPaths |
string[] |
Filter the data returned by the custom resource to specific paths in the API response. |
parameters |
any |
The parameters for the service action. |
physicalResourceId |
aws-cdk-lib.custom_resources.PhysicalResourceId |
The physical resource id of the custom resource for this call. |
public readonly endpoint: string;
- Type: string
The endpoint to call.
public readonly method: string;
- Type: string
The method to call.
public readonly ignoreErrorCodesMatching: string;
- Type: string
The regex pattern to use to catch API errors.
The message
property of the RequestError
object will be tested against this pattern. If there is a match an error will not be thrown.
public readonly outputPaths: string[];
- Type: string[]
- Default: undefined - it's recommended to define it
Filter the data returned by the custom resource to specific paths in the API response.
The total size of the response body can't exceed 4096 bytes.
Example for octokit.rest.repos.createInOrg: ['id', 'full_name', 'owner.id']](https://docs.github.com/en/rest
Example for octokit.rest.repos.createInOrg: ['id', 'full_name', 'owner.id'])
public readonly parameters: any;
- Type: any
The parameters for the service action.
public readonly physicalResourceId: PhysicalResourceId;
- Type: aws-cdk-lib.custom_resources.PhysicalResourceId
- Default: undefined - for "Create" requests, defaults to the event's RequestId, for "Update" and "Delete", defaults to the current
PhysicalResourceId
.
The physical resource id of the custom resource for this call.
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/crpg-ref-responses.html
import { GithubCustomResourceOptions } from '@pepperize/cdk-github'
const githubCustomResourceOptions: GithubCustomResourceOptions = { ... }
Name | Type | Description |
---|---|---|
authOptions |
IAuthOptions |
Currently, supports only GitHub App. |
public readonly authOptions: IAuthOptions;
- Type: IAuthOptions
Currently, supports only GitHub App.
const auth = { appId, privateKey };
const installationAuth = { appId, privateKey, installationId };
https://github.com/octokit/authentication-strategies.js/#github-app-or-installation-authentication
import { GithubCustomResourceProps } from '@pepperize/cdk-github'
const githubCustomResourceProps: GithubCustomResourceProps = { ... }
Name | Type | Description |
---|---|---|
authOptions |
IAuthOptions |
Currently, supports only GitHub App. |
onCreate |
GithubApiCall |
The GitHub Api call to make when the resource is created. |
onDelete |
GithubApiCall |
The GitHub Api call to make when the resource is deleted. |
onUpdate |
GithubApiCall |
The GitHub Api call to make when the resource is updated. |
resourceType |
string |
Cloudformation Resource type. |
public readonly authOptions: IAuthOptions;
- Type: IAuthOptions
Currently, supports only GitHub App.
const auth = { appId, privateKey };
const installationAuth = { appId, privateKey, installationId };
https://github.com/octokit/authentication-strategies.js/#github-app-or-installation-authentication
public readonly onCreate: GithubApiCall;
- Type: GithubApiCall
The GitHub Api call to make when the resource is created.
public readonly onDelete: GithubApiCall;
- Type: GithubApiCall
The GitHub Api call to make when the resource is deleted.
public readonly onUpdate: GithubApiCall;
- Type: GithubApiCall
The GitHub Api call to make when the resource is updated.
public readonly resourceType: string;
- Type: string
Cloudformation Resource type.
- Implements: IAuthOptions
import { AuthOptions } from '@pepperize/cdk-github'
new AuthOptions()
Name | Type | Description |
---|
Name | Description |
---|---|
appAuth |
GitHub App or installation authentication. |
tokenAuth |
Personal Access Token authentication. |
unauthenticated |
unauthenticated. |
import { AuthOptions } from '@pepperize/cdk-github'
AuthOptions.appAuth(secret: ISecret)
GitHub App or installation authentication.
- Type: aws-cdk-lib.aws_secretsmanager.ISecret
import { AuthOptions } from '@pepperize/cdk-github'
AuthOptions.tokenAuth(parameter: IParameter)
Personal Access Token authentication.
- Type: aws-cdk-lib.aws_ssm.IParameter
import { AuthOptions } from '@pepperize/cdk-github'
AuthOptions.unauthenticated()
unauthenticated.
- Implemented By: AuthOptions, IAuthOptions