Skip to content

Instantly share code, notes, and snippets.

@danielTobon43
Last active June 29, 2023 09:34
Show Gist options
  • Save danielTobon43/fccf7dd99cb457dcbdfb62b116b41de1 to your computer and use it in GitHub Desktop.
Save danielTobon43/fccf7dd99cb457dcbdfb62b116b41de1 to your computer and use it in GitHub Desktop.
Sync Bitbucket workspace to AWS Codecommit

Sync Bitbucket workspace/AWS codecommit

This section explains the procedure for synchronizing a workspace in Bitbucket to codecommit. This allows each new repository to be automatically synchronized using a global SSH key provided to the Bitbucket workspace and the pipeline corresponding to the synchronization. (This procedure assumes you have a codecommit repository already created)

SSH key configuration

  • SSH key without passphrase
ssh-keygen -t rsa -f ~/.ssh/[KEY-NAME] -C [USER-NAME] -N ''

e.g.
ssh-keygen -t rsa -f ~/.ssh/codecommit_rsa -C myUserCodeCommit -N ''

AWS configuration

Upload the ssh key generated in the previous step into the AWS user credentials section.

1501855765

  1. Go to the AWS console and click "Upload SSH Public Key" in the "SSH Keys for AWS CodeCommit" section. Paste, accept.

1501855771

  1. copy the SSH key ID

1502117895

Configuration file

Create a configuration file in ~/.ssh/

  • config file
Host git-codecommit.*.amazonaws.com
User APKAQWUUSDIVPDEW3DRH <-- AWS SSH Key ID
IdentityFile ~/.ssh/codecommit_rsa
  • Environment variables in the Bitbucket workspace (take into account that the config and key variables must be encoded in base64)
base64 ~/.ssh/config
base64 ~/.ssh/codecommit_rsa

Bitbucket workspace variables

Define the workspace global variables to be use for all the new repositories 1503494145

Bitbucket pipeline configuration

Now, we will create the bitbucket-pipelines.yml file, add it with your favorite editor to your project root (repository) or click "Configure bitbucket-pipelines.yml" and modify it directly at http://bitbucket.org.

pipelines:
  default:
    - step:
        script:
          - echo $CodeCommitKey > ~/.ssh/codecommit_rsa.tmp
          - base64 -di ~/.ssh/codecommit_rsa.tmp > ~/.ssh/codecommit_rsa
          - chmod 400 ~/.ssh/codecommit_rsa
          - echo $CodeCommitConfig > ~/.ssh/config.tmp
          - base64 -di  ~/.ssh/config.tmp > ~/.ssh/config
          - chmod 600 ~/.ssh/config
          - cat ~/.ssh/config
          - set +e
          - ssh -o StrictHostKeyChecking=no $CodeCommitHost
          - set -e
          - git remote add codecommit [AQUI SSH URL REPOSITORIO EN CODECOMMIT]
          - git push codecommit $BITBUCKET_BRANCH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment