Devops — CICD Pipeline 3: Source control using git and CodeCommit

Faithful Anere
5 min readSep 3, 2019

--

for those who are just joining this series for the first time please view the part 1 and part 2 of this series.

Part 1 — CICD Pipeline-AWS Cloud9

Part 2 — CICD Pipeline — AWS Elastic Beanstalk

In this part, i will create a local Git repository and commit the application code to the local repository. You will also create an AWS CodeCommit repository and sync your local repo with the remote CodeCommit repository. Then you will push the application code to the CodeCommit repository. Using an CodeCommit repository enables you to increase the speed and frequency of your development lifecycle.

To begin, follow the steps below.

1. Create and configure a local Git repository.

In this section, you will initialize a local Git repository and configure it with your username and email address. Follow the steps below.

  • Make sure you are in the home directory of your AWS Cloud9 environment by running the command below.
  • Initialize a Git repository in your local AWS Cloud9 environment by running the command below.
  • Configure your user name for your local Git repository by running the command below. Replace REPLACE_WITH_USERNAME with the user name of your choice.
  • Configure your email address for your local Git repository by running the command below. Replace REPLACE_WITH_EMAIL_ADDRESS with the email address of your choice.
  • Create a dev branch on your local Git repository by running the command below.
  • Download a zip file containing a .gitignore file to your local Git repository. The .gitignore file contains a list of files you don’t want to include in the source control. By using a .gitignore file, you can avoid adding files that are not needed when you perform a check in to your local Git repository. Run the command below to download the zip file.
  • Unzip the zip file by running the command below.
  • Open the .gitignore file in the right side tree view. You should see a list of file extensions and folders. Files with these extensions and the folders listed in the .gitignore file will be ignored every time you check in to your local repository.

2. Stage the application code files in your local Git repo.

In this section, you will stage your application code in your local Git repository. Once the code changes are staged, you can commit the code to your local Git repository. Follow the steps below.

  • Add the .gitignore file to your local Git repository by running the command below.
  • Add the application code files to your local Git repository by running the command below.
  • Check the status of your local Git repository by running the command below.
  • You should see a list of application code files in green as shown in the screenshot below.

3. Commit code to the local Git repo.

  • To commit the staged changes to your local Git repository, run the command below.
  • To get a log of the actions performed on your local Git repository, run the command below.
  • To exit out of the git log command, type q at the terminal.

4. Create a CodeCommit repo.

In this section, you will create a remote CodeCommit repository and sync your local repository with the remote CodeCommit repository. Follow the steps below.

  • Sign in to your AWS Management Console as the myDeployingUser AWS IAM user.
  • In the AWS Management Console, click Services, then click CodeCommit to open the CodeCommit dashboard.
  • Make sure you are in the Oregon region.
  • Click Create respository.
  • For Repository name, type my-Deploying
  • Click Create.
  • Once the repository is created, copy the repository URL as shown in the screenshot below. by clicking clone https
  • Configure your local repository with the remote CodeCommit repository by running the command below. It is usually the convention to name the remote as origin. Replace YOUR_REPO_URL with the CodeCommit repository URL you copied earlier.

5. Generate Git credentials.

In this section, you will generate Git credentials for the myDeployingUser IAM user so that you can push code to the remote CodeCommit repository. Follow the steps below.

  • In the AWS Management Console, click Services, then click IAM to open the IAM dashboard.
  • In the left side navigation menu, click Users.
  • From the list of users, select the myDeployingUser user.
  • Click the Security credentials tab.
  • Scroll down to the HTTPS Git credentials for AWS CodeCommit section.
  • Click Generate.
  • Click Download credentials to download the Git credentials. You will use these credentials every time you push any code changes to the CodeCommit repository.

6. Push code to remote CodeCommit repo.

  • To push the application code from your local repository to the remote CodeCommit repository, run the command below. Origin is the name of the remote destination and dev is the branch you are pushing the code to.
  • You should see a prompt to enter the user name and password. Enter the user name and password for the Git credentials you copied earlier. As soon as you enter the credentials, the code is pushed to the CodeCommit repository.
  • To check the log for local repository, run the command below.
  • To exit out of the git log command, type q at the terminal.
  • To verify that your code is pushed to the CodeCommit repository, go to the CodeCommit dashboard in your AWS Management Console by clicking Services and then clicking CodeCommit.
  • You should see that the my-Deploying repository was updated a few minutes ago.
  • Click the my-Deploying repository. You should see the FlaskApp and other files from your local AWS Cloud9 environment.

Optional

You are prompted for your Git user name and password. As you continue to work with Git, you may be prompted again. To keep from being prompted each time you try to interact with the remote repository in the future, consider installing and configuring a Git credentials manager. For example, you can run this command in the terminal to be prompted no sooner than every 15 minutes: git config credential.helper ‘cache — timeout 900’. Or you can run this command to never be prompted again, although Git stores your credentials in clear text in a plain file in your home directory: git config credential.helper ‘store — file ~/.git-credentials’. For more information, see: https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage

Credit goes to edx.org for providing such an organised learning platform. This series is based on an existing edx course for CICD with AWS. There are more amazing courses to learn on this platform for those who want to learn other technologies

--

--