How to check and commit code to AWS CodeCommit
We use AWS CodeCommit as a backup for our GitHub repositories.
The mirror-repos.yml GitHub Actions workflow copies GitHub repositories tagged with govuk to CodeCommit 4 times daily Mon-Fri.
Accessing CodeCommit requires AWS credentials, which you can obtain in the usual ways via gds-cli.
Quick reference guide
This example demonstrates a simple workflow using the govuk-replatform-test-app repository in CodeCommit.
Follow the installation steps first.
# Clone the repo.
gds aws govuk-tools-developer git clone codecommit::eu-west-2://govuk-replatform-test-app
# Create a local branch.
cd govuk-replatform-test-app
git checkout -b mybranch
# Commit a change locally.
touch mychange
git commit -m "DO NOT MERGE: testing push to CodeCommit" -- mychange
# Push the branch to CodeCommit.
gds aws govuk-tools-developer git push origin mybranch
# Clean up by deleting the example branch we just created.
gds aws govuk-tools-developer git push -d origin mybranch
Install dependencies and set up local environment
You need to have first set up the GDS command line tools.
Install
git-remote-codecommit:brew install git-remote-codecommitOn a Linux machine, you may find it easier to install
git-remote-codecommitviapip:pip3 install git-remote-codecommitConfigure your git client to use
git-remote-codecommitto authenticate to AWS. (See AWS CodeCommit docs for further info.)git config --global credential.helper '!aws codecommit credential-helper $@' git config --global credential.UseHttpPath trueThis should automatically add the following entry to your
~/.gitconfigfile.[credential] helper = !aws codecommit credential-helper $@ UseHttpPath = true
Find a repository in CodeCommit
Repository names should exactly match those in GitHub. If you are unsure whether a given repository exists in CodeCommit, you can browse the available repositories.
Log into the AWS console to see the available repositories using the GDS command line tool
gds aws govuk-tools-developer --loginSelect the London (
eu-west-2) region.Go to the CodeCommit page. You can find it via the search bar or the navigation menu.
Find the repository you want to clone using the repository search bar.
Select the “HTTPS (GRC)” repository URL.
Clone a repository from CodeCommit
- If you already have the repository checked out via github under
/govuk, there will be a destination clash, so make sure to clone into a fresh directory. In your shell, obtain AWS credentials and run
git cloneto clone the repository.gds aws govuk-tools-developer git clone <repository url>For example:
gds aws govuk-tools-developer git clone codecommit::eu-west-2://govuk-replatform-test-app
git cloneon CodeCommit can sometimes be very slow initially. Ifgit cloneappears to hang atremote:orremote: Enumerating objects, it’s worth waiting several minutes as it may start working.
Pushing to a branch
Once you have cloned a repository from CodeCommit, you can create local branches and push changes as normal, provided you have valid AWS credentials in your shell.
For example:
gds aws govuk-tools-developer git push origin mybranch