Migrating CDK Pipelines from CodeCommit to GitHub
AWS closed new customer access to CodeCommit on July 25, 2024. Time to migrate your CDK Pipelines to GitHub. Good news: it’s straightforward and you don’t need to rebuild everything.
First, push your code to GitHub:
# Add GitHub as a new remotegit remote add github https://github.com/your-username/your-repo.git
# Push everything to GitHubgit push github --all && git push github --tagsCreate a GitHub personal access token (Settings > Developer settings) with repo scope and store it in AWS Secrets Manager as github-token.
Now swap the source in your CDK code:
import { CodePipeline, CodePipelineSource } from 'aws-cdk-lib/pipelines';import { Repository } from 'aws-cdk-lib/aws-codecommit';
const codeCommitRepository = Repository.fromRepositoryName(this, 'my-codecommit-repo-id', 'my-codecommit-repo-name');
const pipeline = new CodePipeline(this, 'Pipeline', { pipelineName: 'my-pipeline', synth: new ShellStep('Synth', { // Replace CodeCommit source with GitHub input: CodePipelineSource.codeCommit(codeCommitRepository, 'main'), input: CodePipelineSource.gitHub('your-username/your-repo', 'main'), commands: ['npm ci', 'npm run build', 'npx cdk synth'] })});Deploy through CodeCommit one last time, then switch remotes:
# Commit and push the changes to CodeCommitgit add .git commit -m "chore: migrate pipeline source to GitHub"git push origin main
# Push to GitHub to ensure both repos are in syncgit push github main
# Switch GitHub to be primary remote, but keep CodeCommit as backupgit remote rename origin codecommit-backupgit remote rename github origin
# After validating the pipeline works with GitHub, you can remove the old remote# git remote remove codecommit-backupCodePipeline handles the transition without recreating anything. Your deployment history stays intact 🎉.