Continuous Integration is a development practice that requires developers to integrate code into a shared repository several times per day (repos in subversion, CVS, git). Each check-in is then verified by an automated build, allowing everyone to detect and be notified of problems with the package immediately.
Build Pipeline is a process by which the software build is broken down in sections:
The concepts of Continuous Integration, Build Pipeline and the new “DevOps” movement are revolutionizing how we build, deploy and use software. Gitlab is providing the automated pipeline which we need to use the gitlab.yml file.
I am unable to find the docker image which is appropriate for the php version and composer version we are using in our app. So I go with a manual gitlab runner.
Gitlab runner configuration: I installed the gitlab runner and initialized it in the aws ubuntu ec2 instance with the following steps:
# Download the binary for your system
sudo curl -L –output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
# Give it permissions to execute
sudo chmod +x /usr/local/bin/gitlab-runner
# Create a GitLab CI user
sudo useradd –comment ‘GitLab Runner’ –create-home gitlab-runner –shell /bin/bash
# Install and run as service
sudo gitlab-runner install –user=gitlab-runner –working-directory=/home/gitlab-runner
sudo gitlab-runner start
For registering the runner in use the following command:
sudo gitlab-runner register –url https://gitlab.com/ –registration-token $REGISTRATION_TOKEN
After installation, I tried to use the entire ec2 instance as a runner which will be more easy to deploy the application by using ssh as executor. I added pem file as the ssh key in the gitlab cicd variables and also stored the key in ec2 instance location /home/ubuntu/.ssh. This runner uses aws environment as its own environment and doesn’t need anything to install.
But this runner failed because due to some configuration in the AWS EC2 instance while I logged into the instance with root through the pipeline, it logged out automatically. So I rewrote the config file of gitlab runner (replaced executor as shell). This change allows Pipeline to login to the instance, but doesn’t allow the git to pull the files.
SSH Key generation and adding to Gitlab: To allow the git to pull the files from gitlab add the ssh key into the gitlab account. Generate ssh key by using
ssh-keygen -o -f ~/.ssh/id_rsa
This generates an rsa ssh key.
Then move to the /root/.ssh folder and create config file with below code:
Host gitlab.aagnia.com
HostName gitlab.com
AddKeysToAgent yes
#UseKeychain yes
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa1
This allows git operations inside the server (Install the git and use git init before running the pipeline) through the pipeline. Now run the pipeline from the gitlab account.
To set up a gitlab cicd pipeline for an laravel application to host in the aws server, we used the following code as .gitlab-ci.yml:
stages:
– build
building:
stage: build
script:
– cd <path/to/directory>
– sudo git checkout dev
– sudo git pull
– sudo cp .env.example .env
– sudo composer install
– sudo php artisan config:cache
– sudo php artisan config:clear
– sudo php artisan cache:clear
– sudo chown -R root:root <path/to/directory>
– sudo chmod -R 777 <path/to/directory>
only: