A CI/CD pipeline helps you automate your software delivery process. Automated pipelines remove manual errors, provide standardized development, and help you to release software faster.
In this blog, we will create a CI/CD pipeline using the GitLab runner to deploy code in AWS EC2.
- An AWS account.
- GitLab account.
- Basic Understanding of YAML
Setup Environment variables
We need to configure the environment variables in Gitlab to communicate with the AWS EC2. So we need the private key of the EC2 instance and IP address. For that you need to go to your project in Gitlab-> Settings-> CI/CD-> Variables.
Enter Key as DEPLOY_SERVER, value as IP address of your EC2 instance, and add another Key as SSH_PRIVATE_KEY, value as Private key of your ec2 instance.
Gitlab offers a continuous integration service if you add a .gitlab-ci.yml file to the root directory of your code repository. I’ve provided a sample .gitlab-ci.yml file as seen in example Github project. Please be sure to update the group name and repository name for your own project. If you copy this code please be sure to use your own repository name.
Here is an example of .gitlab-ci.yml file
- apt update
- npm install -g @angular/cli
- npm install
- ng build --prod
- echo "BUILD SUCCESSFUL"
- mkdir -p ~/.ssh
- echo -e "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
- chmod 600 ~/.ssh/id_rsa
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
- scp -r dist/* ubuntu@$DEPLOY_SERVER:/home/ubuntu/foldername
If you commit to your master branch with .gitlab-ci.yml and files in the root directory of your project then the CI/CD pipeline will initiate with two steps outlined line-by-line below:
The Build Stage
- Update the packages in the node Docker container
1- apt update
- Install Angular
12- npm install -g @angular/cli- npm install
- Build the production bundle of Angular
1- ng build --prod
- This code block creates a build artifact that will be passed onto the next stage in the pipeline if all operations in the container run without error.
- This keyword tells GitLab only to run this stage when a commit is pushed to the master branch.
The Deploy Stage
- This stage will only run if the build stage has successfully completed.
- Generates to connect to the AWS unit the SSH key.
12- mkdir -p ~/.ssh- echo -e "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
- Sets the permission to 600 to prevent a problem with AWS & disable the host key checking
12- chmod 600 ~/.ssh/id_rsa- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
- Script will run in & execute command
1scp -r dist/* ubuntu@$DEPLOY_SERVER:/home/ubuntu/foldername
The above command will copy all files in the dist folder to ec2 instance using secure copy command.
- Environment which tells GitLab to consider this Job as a production deployment, you can use various environments like development, testing.
Verify CI/CD pipeline
The CI/CD pipeline has been configured.
After making changes in your project, push to your master branch. Gitlab
CI/CD will trigger automatically if you have .gitlab-ci.yml file in the root directory.