Skip to main content

Manage GitHub resources like repositories, teams, members, integrations and workflows with the AWS CDK as Custom Resources in CloudFormation with [cdk-github](https://github.com/pepperize/cdk-github).

Project description

PRs Welcome GitHub npm (scoped) PyPI Nuget Sonatype Nexus (Releases) GitHub Workflow Status (branch) GitHub release (latest SemVer) Gitpod ready-to-code

CDK Github

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.

View on Construct Hub

Install

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>

Contributing

Contributions of all kinds are welcome :rocket: 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

Getting Started

  1. Creating a GitHub App

  2. Installing GitHub Apps

  3. Create an AWS Secrets Manager secret

    {
      "appId": "123456",
      "privateKey": "-----BEGIN RSA PRIVATE KEY-----\nExample==\n-----END RSA PRIVATE KEY-----",
      "installationId": "12345678"
    }
    
  4. Add @pepperize/cdk-github to your project dependencies

    yarn add @pepperize/cdk-github
    
  5. 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 :wink:

  6. Import your secret

    const secret = secrets_manager.Secret.fromSecretNameV2(stack, "Auth", "cdk-github/github-token");
    
  7. Configure GitHub App authenticate as an installation

    const authOptions = AuthOptions.appAuth(secret);
    
  8. 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),
    });
    
  9. Deploy your first GitHub Custom Resource

    npx cdk deploy
    

Authentication

GitHub App or installation authentication

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

Personal Access Token authentication

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

Unauthenticated

// ๐Ÿ‘‡This will configure octokit without authentication
const authOptions = AuthOptions.unauthenticated();

Manage a GitHub Repository - Example

Manage a GitHub Repository as custom CFN resource

@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");

Manage GitHub Actions Secrets

Environment Secret

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 another GithubCustomResource via getAtt().

See GitHub Developer Guide, API Reference

Organization Secret

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

Repository Secret

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

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pepperize.cdk-github-0.0.546.tar.gz (693.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pepperize.cdk_github-0.0.546-py3-none-any.whl (695.4 kB view details)

Uploaded Python 3

File details

Details for the file pepperize.cdk-github-0.0.546.tar.gz.

File metadata

  • Download URL: pepperize.cdk-github-0.0.546.tar.gz
  • Upload date:
  • Size: 693.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for pepperize.cdk-github-0.0.546.tar.gz
Algorithm Hash digest
SHA256 117abca0e2d089fcc3b6c92153d69d9cb0e1468f90e4c64d89e5584619e0fcd2
MD5 bcf485a7ed870f53ec99ab966f0ea731
BLAKE2b-256 8cd599ee104645d4930f02da261a1d66420ecb0b9efc509e34207ad3bd2ea7e8

See more details on using hashes here.

File details

Details for the file pepperize.cdk_github-0.0.546-py3-none-any.whl.

File metadata

File hashes

Hashes for pepperize.cdk_github-0.0.546-py3-none-any.whl
Algorithm Hash digest
SHA256 29d503c9a0b256bc12ac2675cb6d0f8e35878b9082bd927f5a500023c4705238
MD5 479d757d841e40a7f14ff97976c57a93
BLAKE2b-256 24af6506c03e6069da730b3427ac2b388daa9c95f9bd7502cb2a7a117de7e8ce

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page