What is GitLab..?
In short, Gitlab is web-based Devops lifecycle tool which simplify the Software Development Life Cycle ( SDLC ) by managing, planning, create, verify, package, secure, release our code which is developed by awesome developers.
Let’s Get Started:-
The main key part of Software Development Life Cycle is Development of code. After developing the code we deploy our code to our server and there are many ways to do that. But i am showing you one of the ways which is through Gitlab and ssmtp package ( for mail ).
- Only Gitlab Instance (for Gitlab setup follow this link)
- Create a new Project in Gitlab
First login into Gitlab Instance and make a new Project. Click on Create Blank Project.
Now Give the Project a Name and write some description about the project. Also set the Visibility Level as Private.
And at Last click on Create Project.
Now you have created the Project in Gitlab Instance.
- Setting Up Project
Add a README.md file to your repository that describing your project which is very beneficial for the person who wants to know about Project.
For example I have write this
Now we have to make a gitlab-runner for this project which is used to run our defined jobs in pipeline file.
To get the details which is used in next steps visit project settings -> CI CD pipeline -> Expand the runners tab. Used these details to register the gitlab-runner.
Run the command:
sudo gitlab-runner register
- First Enter the Gitlab Instance URL in which your Instance is running
- Next, Enter the registration token
- Then, Enter a description for the runner
- Now Enter the tags for the runner this is very important as these are used to check which job is run by the runner.
Now your gitlab-runner is setup,
we can verify this by going to project settings -> CI CD pipeline -> Expand Runner tab and see if there are any specific runner running for our project.
Now the most important part about Setting Up Project is writing the .gitlab-ci.yml file. Which defines our pipeline in which we write our Stages and in Stages we write our Jobs which tell about what we have to do like Building the project, Testing the project and Deploying the project on remote server.
Explanation of gitlab-ci.yml file
First we define our stages in yml file
Next, we write our jobs according to our need. In the job_1 execute a python file to show the PREPARING PHASE.
Next, in job_2 we run the TESTING stage in which we write our test cases and if our test cases passed then we get a mail about “Test is Successful” and if not passed then our pipeline will fail and no deployment is done on the remote server.
Let’s hope for good and our test cases are passed then we go to the DEPLOYMENT Phase in which we deploy code to the remote server. And after successful deployment, we also get a mail that tells about the Successful Deployment.
Now we have something left to configure apart from this setup is mail configuration and ssh key based authentication to the remote server.
For the mail configuration we have to install the ssmtp package on the machine by running the command
sudo apt install ssmtp
After installing that package we need to configure it. For this, i am using gmail account and the configuration for any gmail account is pretty much same.
For configure the mail we need to edit the /etc/ssmtp/ssmtp.conf file ( you may need the root permission for this ) paste the following lines in file
mailhub=smtp.gmail.com:587 useSTARTTLS=YES AuthUser=<your gmail id> AuthPass=<gmail account password> TLS_CA_FILE=/etc/pki/tls/certs/ca-bundle.crt
You may also need to allow less secure app access in your gmail account by going to manage your google account -> security -> allow secure app access
You can test correct configuration of mail by running the command
echo “Deploy Successfully” | ssmtp -vvv <sender_email_address>
If sender receive the mail then email configured successfully.
- Now need to configure the key based authentication with the remote server
ssh-keygen ssh-copy-id username@remote_server_ip
Now provide the password to exchange the keys.
Verify it by ssh into remoter server by running this command
And you should get into remote machine without any password
This verify’s the key based authentication on the remote server.
Now We all Set. We have build our pipeline and done all the configurations.
- Push all the Project to the Gitlab repository
Upload your project code to the Gitlab repository and also made changes to the .gitlab-ci.yml file according to your project. This is the final step and we all done. At the time we upload our code or commit our code pipeline will execute automatically.
- See the Result
To see our Pipeline running go to CI CD -> Pipeline and see if the pipeline run successfully or not.
To see more detailed output click on the status of particular pipeline.
Now click on the particular Job to see the output and what that job is doing.
That how I made a pipeline in Gitlab which deploy our code to the remote server and also tell about the successful deployment by sending mail to us.
Post By Jatin (Developer)