This article describes how to create a Gitlab CI/CD pipeline using gitlab-runner and docker as the build strategy in order to deploy microservices on Openshift.
On my previous articles I have explained how to create your own hosted gitlab instance and deploy a single CI/CD pipeline using gitlab-runner. The whole setup was based on containers, as a result the infrastructure needed can be deployed on Openshift as well.
The pipeline consists of three steps, housekeeping, staging and cleaning. It is based on the default example that gitlab provides and uses oc commands to communicate with Openshift. It is configured to be triggered only for develop branch and every time a new commit is added the build starts.
The housekeeping step will remove every resource that has been created from a previous build.
The staging step will build the microservices based on your Dockerfile instructions as the build strategy is set to docker.
The cleaning task will remove the building pods that have been created from Openshift.
The housekeeping step is allowed to fail so that if no resources are found, the building step will continue its work.
You can see below a simple run of the pipeline.
You can find the code of the pipeline in the below repository:
When you get a failure from your CI/CD pipeline regarding permission denied reasons, you should change them accordingly so that all users could access the oc tool.
The resolution is to provide 751 permissions or any other needed, but make some that user that executes the pipeline will be able to run the oc tool. Personally I added execute for others and I could bypass the error.
In order to create a CI/CD pipeline with gitlab built-in functionality you should firstly create the appropriate .gitlab-ci.yml file. This is the file on which the steps will be described for the pipeline.
This file should be placed on the root structure of the branch and every time a commit is pushed on the remote repository the steps will run. Instructions have been provided from gitlab and can be found here
For this example I chose gitlab runner as the building tool and the deployment method of a docker container.
In order to install gitlab runner as a container perform the below steps:
Register gitlab with your runner. You should get the registration token and runner url from your repository settings.
Inspect container and press gitlab-runner register
Start the runner
The runner should have been registered on your gitlab environment
Perform a commit and push changes to your repository
The run task should have started
Check the pipeline and see its status
The job was not succesful and by checking the logs I could verify that DNS resolution could not be enstablished.
In order to fix that you should add an entry for your named gitlab container to your gitlab runner. Unfortunately there are no tools like vim, nano installed on gitlab-runner. However you can bypass this by echoing a value in your /etc/hosts file.
It is also important that your local computer can resolve by fqdn your gitlab deployment. This is necessary because docker should be able to read this entry and perform actions on it.
After those changes you will be able to run your pipeline successfully.
Portainer is a fantastic tool that includes a GUI in order to manage your container workloads easier than with command line. It is free to use with a community edition and the documentation describes the installation which will take one approximately 5 minutes to complete.
In this article I will show you how to use portainer and its GUI to deploy a gitlab container on your setup.
If you use the default setup instructions then your instance will be created on localhost under 9000 port.