AWS CodeCommit Deep Dive
HI Everyone,
We all know AWS provides a set of Developer Tools to achieve CICD requirements in the software delivery life cycle.
Version Control System: CodeCommit
Build and Test: CodeBuild
Deployment: CodeDeploy
Integration of VCS + Build/Test + Deploy: CodePipeline
In this article I would like to touch all the key functionalities of Code Commit. You need to have an AWS Account with console access to implement the topics from your side.
Topics
Initial Setup
Repository Creation
Git Basics
Pull Requests
Branch Protection Rules
Triggers and Notifications
Initial Setup
As part of the Code Commit repository Implementation, we will create a user in IAM and use that for the Git Operations for the entire demonstration.
I will create a user named “testuser” and provide Code Commit Full Access. I will be using same user for Code Deploy/Build/Pipeline so added those permissions as well. If you want, you can remove those for your convenience. As of now both console and cli access is disabled. Once the user is created its permissions will be listed as shown below.
Click on the Security Credentials, we need to create a user credentials for code commit.
Click on Generate Credentials and copy those for your usage while pushing the code to the Repository. Once credentials are created, it will be listed as shown below.
Repository Creation
Search for CodeCommit in the search bar and click on the service, it will take you to the CodeCommit Landing Page.
Create a repository by giving all the details like Name and Description. No need to enable Code Guru functionalities.
Once the repository is created you can clone the empty repository and start the operations.
In order to communicate with CodeCommit, we will use ec2 instance for CLI. I created an EC2 Instance and logged into that. Please install git
sudo yum install git -y
Git Basics
Clone the repository as shown below
git clone <https repo url>
Please provide the https credentials we generated in the IAM Step for Authentication purpose.
Now switch to the repository create a file called f1, we will push this file to CodeCommit repository in AWS.
git add .
git commit -m "commit message"
git status
git push origin master
Once it is pushed it will be visible in the repository as shown below.
Git log
If you want to see the list of commits and also to identify at which commit the master and current Local Head are positioned.
For example, in the below screenshot we can see two commits.
First one with (origin/master) is the commit which is currently in Global Repository Level.
Second one with Head -> master is the commit which signifies the local commit which is yet to be pushed to code commit origin.
once you pushed the commit to the origin, if you run git log both the origin and head points to last commit as shown below
you can verify the same in console repo as well.
GIT CHECKOUT/MERGE
If you want to switch to an existing branch, use git checkout <branch-name>
If the branch is not available, then use -b to create and switch. We are creating a new branch called feature from master branch. so technically it will have the master code by default. Once you updated your feature branch you can use “git merge” to merge your changes into the master branches.
As part of the demonstrations, i created a new file called f3 and commited it to feature branch.
GIT MERGE
I am merging feature branch to master branch using git merge <branch-you want to merge>command.
you switch to master and execute git merge feature, later you can check whether the feature branch files are added to master branch or not.
Pushing feature to code commit repo. As of now the new branch is in local repo only. Before pushing new branch, we can see only one branch
After pushing
GIT RESET
Reset current HEAD to the specified state.
As of now all branches in code commit have latest commits.
Let's add a commit but not push to global repository.
If you want to undo the commit but keep your changes for a bit of editing before you do a better commit. Then we need to use soft reset, if you want to delete the file as well then consider hard reset
git reset HEAD~
Above screenshot you can see the commit is gone but the resetexample file is still available.
But if you want to destroy commit and also throw away any uncommitted changes. Then you need to use HARD RESET.
Now I added the file again to show hard reset.
git reset --hard HEAD~1
Now after hard reset you can see both the commit and file is gone.
GIT REVERT
If you have already made your commits public, you will want to create a new commit which will “revert” the changes you made in your previous commit (current HEAD).
I created a file with revertexample name and pushed to code commit origin.
Now we need to undo those changes. We need to use git revert for that.
Once you do git revert HEAD, it will ask for commit message as shown below, please switch to insert mode and enter message and save it.
Once you reverted it, the commit history visible like below
Now if you see the console, the file is still present in origin.
To completely revert the file from console as well you need to do the git push to make the reverted changes visible in code commit global repo as well.
After git push
GIT DIFF
git diff lists out the changes between your current working directory and your staging area.
I created one file with name diffexample2 and add testing line into that file. Later I committed it.
Git diff showing empty because no conflict between current working directory and staging area as of now.
After updating the file, you can see the updated info which is not yet committed.
After commit, we can see diff is clean.
PULL REQUESTS
Master Branch as of now
Feature Branch as of now
We can see diffexample2 file is missing in feature branch.
so, click on create pull request to merge the master branch to feature (In real time we always merge feature branch to master)
As of now NO Approvals in place, No Conflicts raised. so we can directly merge it without any issues.
Once it is merged, you can see the latest changes in feature branch.
As of now the PR don’t have any approvals in place.
You can set that using Approval Rules in code commit
Approval Rule Template
You can create a Template and apply across repositories/branches.
You can configure who can approve for example User/Role/Pool Member
You can configure number of approvals required for any merge to the particular branch. Usually for lower environments teams prefer 1 approval and for master/production environment they consider 2 approvals.
Now create a new file in feature and raise a PR to Master
Since we enabled approval rule template for master branch in testrepo, approval template automatically applied to this repo
You can override the rule, if you have permissions to do so.
Later you can click on merge
NOTIFICATIONS/TRIGGERS
Notification creation
You can specify the name, type and events to trigger the notifications.
You can select an SNS Topic to notified in case any files deletion/merges/pull requests etc.
TRIGGERS
You can trigger a lambda in case a particular branch is updated.
COMMIT HISTORY
Once the implementation is done, please clear the repository.
Hope you got some good insights of Code Commit Repository with this article.
Happy Learning!!!