How To Manage An AWS CodeCommit Repository Using AWS CLI
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed how to configure access to your AWS CodeCommit repository.
https://cloudaffaire.com/how-to-access-an-aws-codecommit-repository-remotely/
In this blog post, we will discuss how to manage an AWS CodeCommit repository using AWS CLI. Though for most of the time, apart from creating the initial CodeCommit repository you are likely to use git operations to manage your CodeCommit repository. But it may be helpful to have some knowledge on how to manage the repo using AWS CLI instead of git.
How To Manage An AWS CodeCommit Repository Using AWS CLI:
Requirements:
AWS CLI installed and configured. You can follow the below blog post to install and configure AWS CLI.
https://cloudaffaire.com/how-to-install-aws-cli/
https://cloudaffaire.com/how-to-configure-aws-cli/
Git installed and configured.
https://www.atlassian.com/git/tutorials/install-git
How to create an AWS CodeCommit repository using AWS CLI?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
############################################################## ## How To Manage An AWS CodeCommit Repository Using AWS CLI ## ############################################################## ## I am using a Linux shell to execute AWS CLI commands ## create some files for this demo mkdir -p codecmitdemo/mydir1 && cd codecmitdemo echo "aws" > myfile1.txt echo "gcp" > mydir1/myfile2.txt ## create a codecommit repository aws codecommit create-repository \ --repository-name "my_aws_repo" \ --repository-description "CloudAffaire test repository for codecommit demo" \ --tags "Key=Name,Value=MYAPP" |
How to upload a file to a branch in the AWS CodeCommit repository using AWS CLI?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
## how to create a file in aws codecommit repo using aws cli AWS_CODECMIT_COMMIT_ID1=$(aws codecommit put-file \ --repository-name "my_aws_repo" \ --branch-name "master" \ --file-content file://myfile1.txt \ --file-path myfile1.txt \ --name "Debjeet Bhowmik" \ --email "cloudaffaire@gmail.com" \ --commit-message "Initial commit - myfile1.txt added" \ --query 'commitId' \ --output text) ## get details of the file that we just uploaded aws codecommit get-file \ --repository-name "my_aws_repo" \ --commit-specifier master \ --file-path myfile1.txt |
How to create a folder in the AWS CodeCommit repository using AWS CLI?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
## how to create a directory in aws codecommit repo using aws cli AWS_CODECMIT_COMMIT_ID2=$(aws codecommit put-file \ --repository-name "my_aws_repo" \ --branch-name "master" \ --file-content file://mydir1/myfile2.txt \ --file-path /mydir1/myfile2.txt \ --name "Debjeet Bhowmik" \ --email "cloudaffaire@gmail.com" \ --parent-commit-id "$AWS_CODECMIT_COMMIT_ID1" \ --commit-message "Second commit - mydir1 with myfile2.txt added" \ --query 'commitId' \ --output text) ## get details of a directory in aws codecommit repo aws codecommit get-folder \ --repository-name "my_aws_repo" \ --folder-path "/mydir1" |
How to get commit details of a CodeCommit repository using AWS CLI?
1 2 3 4 5 6 7 |
## get commit details for a codecommit repository aws codecommit get-commit \ --repository-name "my_aws_repo" \ --commit-id "$AWS_CODECMIT_COMMIT_ID1" && aws codecommit get-commit \ --repository-name "my_aws_repo" \ --commit-id "$AWS_CODECMIT_COMMIT_ID2" |
How to create a new branch in the AWS CodeCommit repository using AWS CLI?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
## create a new branch in codecommit repo aws codecommit create-branch \ --repository-name "my_aws_repo" \ --branch-name "mybranch" \ --commit-id "$AWS_CODECMIT_COMMIT_ID2" ## list all branches in a codecommit repository aws codecommit list-branches \ --repository-name "my_aws_repo" ## get a specific codecommit branch details aws codecommit get-branch \ --repository-name "my_aws_repo" \ --branch-name "mybranch" |
How to merge a branch in AWS CodeCommit repository using AWS CLI?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
## upload a new file to the branch mybranch echo "azure" > myfile3.txt && AWS_CODECMIT_COMMIT_ID3=$(aws codecommit put-file \ --repository-name "my_aws_repo" \ --branch-name "mybranch" \ --file-content file://myfile3.txt \ --file-path /myfile3.txt \ --name "Debjeet Bhowmik" \ --email "cloudaffaire@gmail.com" \ --parent-commit-id "$AWS_CODECMIT_COMMIT_ID2" \ --commit-message "Third commit - myfile3.txt added in mybranch branch" \ --query 'commitId' \ --output text) ## get difference between two commits aws codecommit get-differences \ --repository-name "my_aws_repo" \ --before-commit-specifier "$AWS_CODECMIT_COMMIT_ID2" \ --after-commit-specifier "$AWS_CODECMIT_COMMIT_ID3" ## merge mybarnch with master branch aws codecommit merge-branches-by-fast-forward \ --source-commit-specifier "$AWS_CODECMIT_COMMIT_ID3" \ --destination-commit-specifier "$AWS_CODECMIT_COMMIT_ID2" \ --repository-name "my_aws_repo" \ --target-branch "master" |
How to delete a branch in the AWS CodeCommit repository using AWS CLI?
1 2 3 4 |
## delete mybarnch branch aws codecommit delete-branch \ --repository-name "my_aws_repo" \ --branch-name "mybranch" |
How to delete a file in the AWS CodeCommit repository using AWS CLI?
1 2 3 4 5 6 7 8 9 10 11 |
## delete a file in codecommit repository AWS_CODECMIT_COMMIT_ID4=$(aws codecommit delete-file \ --repository-name "my_aws_repo" \ --branch-name "master" \ --file-path /myfile3.txt \ --name "Admin" \ --email "admin@gmail.com" \ --parent-commit-id "$AWS_CODECMIT_COMMIT_ID3" \ --commit-message "Fourth commit - myfile3.txt deleted in master branch" \ --query 'commitId' \ --output text) |
How to delete an AWS CodeCommit repository using AWS CLI.
1 2 3 4 5 6 |
## delete your codecommit repository aws codecommit delete-repository \ --repository-name "my_aws_repo" ## delete demo directory cd .. && rm -rf codecmitdemo |
Hope you have enjoyed this blog post. To get more details on AWS CodeCommit, please refer below AWS documentation
https://docs.aws.amazon.com/codecommit/latest/userguide/welcome.html
https://docs.aws.amazon.com/cli/latest/reference/codecommit/index.html
CodeCommit is a Git-based version control system and you need to have a basic understanding of Git in order to work with CodeCommit. You can follow the below link to get a basic understanding of Git.
https://cloudaffaire.com/category/devops/git/