In the world of modern software development process, automating the deployment process is essential for ensuring efficiency and reliaibility. Gitlab runner allows you to implement continous integration and continous deployment (CI/CD) pipeline. In this tuturial, we will look into setting up the gitlab runner on our own AWS EC2 instance.
Before moving forward, let understand the basics
What is GItlab?
GitLab is a web-based Git repository that provides free open and private repositories, issue-following capabilities, and wikis. It enables professionals to perform all the tasks in a project — from project planning and source code management to monitoring and security.
You can sign up here and start working further.
What is Gitlab runner?
GitLab Runner is an open-source application that runs jobs in a pipeline with GitLab CI/CD. It’s a lightweight, highly-scalable agent that picks up a CI job, runs the job, and sends the results to GitLab. Runners can run multiple jobs simultaneously, use multiple tokens with multiple servers, limit the number of concurrent jobs per-token, run jobs locally, use Docker containers, and connect to a remote SSH server.
There are three types of runners:
- Shared runners: For use by all projects
- Group runners: For all projects and subgroups in a group
- Project runners: For individual projects
You can find more info about gitlab runner here.
Prerequisites
1. An AWS account with appropriate permissions to create resources.
2. Basic knowledge of GitLab and CI/CD concepts.
3. Familiarity with the AWS Management Console and command-line interface (CLI).
Let’s begin setting up the gitlab runner
Step 1: Creating a project on gitlab
Go to your Gitlab account and click on create new project
Click on create blank project, assign name to your project and click create
You have now successfully created your project.
Once our project is created now let’s navigate to get the runner details so that we can configure it on our onw EC2 server.
From the left nav bar, navigate to settings > CICD
Expand the runner option to view the runner available for this project. Make sure to disable the instance runner
Click on three dots next to New project runner and save the registration token for further steps
Step 2: Launching an EC2 Instance
Login to your AWS account and go to EC2 dashboard. I have created an EC2 instance of type t2.micro with amazon linux 2023 AMI. Make sure your ec2 instance is in public subnet and public IP is enabled.
Step 3: Configuring an EC2 instance with Gitlab
Now let’s connect to our EC2 instance and started the installation process.
- Installing git oso that our runner can clone the code for the each job that it gets triggered
sudo yum -y install git
2. Downloading gitlab runner binary
sudo curl -L --output /usr/bin/gitlab-runner "https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64"
3. Providing executable permissions to the gitlab runner package
sudo chmod +x /usr/bin/gitlab-runner
4. Create a gitlab CI user
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
5. Install the runner
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
5. Start the gitlab-runner
sudo gitlab-runner start
6. Registring runner with the gitlab
Here we will be using the registration token that we copied previously from Settings > CICD > Runners
Run the below command and fill up the required details
sudo gitlab-runner register --url https://gitlab.com/ --registration-token <YOUR_REGISTRATION_TOKEN>
7. Check the runner status
sudo gitlab-runner status
We can also check on gitlab that our gitlab runner is now online and configured with our project.
Wohooo.. our gitlab runner is now successfully setup 🥳
But wait! Now comes the main part i.e testing.
Step 4: Testing
Now let’s go to our Code repo and create a file .gitlab-ci.yml.
stages:
- build
- test
variables:
build_file_name: demo.txt
build-file:
stage: build
script:
- echo "build stage started"
- mkdir build
- touch build/$build_file_name
- echo "Hello from build" >> build/$build_file_name
artifacts:
paths:
- build
tags:
- mygitlabrunner
test file:
stage: test
script:
- echo "test stage started"
- cat build/$build_file_name
tags:
- mygitlabrunner
Once you commit the file, navigate to build > pipelines and you will see that your pipeline has started.
Click on the stages to view pipeline execution logs
Your pipeline execution has completed successfully and likewise you can create more such jobs and add multiple stages in your pipeline for test, build, deploy etc as required by your project
Conclusion
In this blog, we have understood and successfully set up GitLab Runner on an AWS EC2 instance, enabling automated CI/CD pipelines for our projects. This setup empowers us to streamline our development process, increase productivity, and deliver high-quality software efficiently.
I hope you found this content informative and enjoyable. For more blogs and updates, please consider following and clicking the 👏 button below to show your support. Happy coding! 🚀
Thank you for reading! 💚