How to build and deploy Git Tags using Jenkins

Photo by Marvin Meyer on Unsplash

In this article, You will learn how we can build git tags automatically using GitLab and Jenkins. You will also learn how we can build different tags & deploy them on different servers using a single Jenkinsfile.

Step 1. Configuring Jenkins

I won't be sharing how to install Jenkins. I am pretty sure if you are here you know how to work with Jenkins.

  • Go to Jenkins dashboard → Manage Jenkins → Manage Plugins → Under available section search for Multibranch Scan Webhook Trigger and Basic Branch Build Strategies Plugin & install these plugins without restart.

Now go to Jenkins dashboard → New item → Name your Project → Choose Multibranch Pipeline → press Ok

  • Now inside your project, Under branch sources enter your project URL and credentials. In behaviors, add discover tags.
  • In build strategy, add tags
  • Now Scroll down a bit and you will see a section named Scan Multibranch Pipeline Triggers, Check the Scan by webhook and enter a token and apply & save it.

We have configured our Jenkins now we have to configure our Gitlab for webhooks. Webhooks will help us in triggering Jenkins automatically if any tags are pushed to our GitLab account.

Step 2. Configuring GitLab Webhooks

  • Inside your GitLab, go to settings → Webhooks → In URL, follow the same sequence shown in the below image (put the token that we created in Jenkins after ‘token=’ in URL), leave the secret token space blank, and check push events and tag push events → Add webhook.

Now Gitlab and Jenkins are configured for automatic triggers via Webhooks. If we push a tag it will automatically trigger the Jenkins.

Step 3. Create App and Jenkinfile

Now we need to have an application, so we can create a Jenkinsfile for it and it will build the app and deploy it to the webserver. For this article, I will be using a simple react app. You can clone the app from my git repo.

Now that you have cloned it, Have a look at Jenkinsfile

In the above Jenkinsfile, you can see I have put when condition with a wildcard (*) -dev,-prod,-staging. this is because let's say we have 3 different servers and we want to build and deploy a particular tag to a particular server then we can commit a tag by adding a suffix(i.e v1.0-prod, v1.5-staging, etc) to it and it will build & deploy that tag on the server its suffix matches in Jenkinsfile.

Tag naming strategy

For our project, We need to add a suffix with every tag that we push to our GitLab. This suffix will help the matching of a tag to its desired environment. For a better understanding of tags naming strategy see the below picture.

  • Let’s say we commit a tag v2.7 and we want this tag to build and deploy on our AUS environment. So in our case, we have to add a suffix i.e v2.7-aus, Using this it will build and deploy only on the desired environment.

Now if I commit a new tag then Jenkins will automatically start build it and will deploy to its respective server. Okay let me commit a new tag v9.0-us in GitLab and You will see the same tag building in Jenkins dashboard.

So if you have followed every step correctly then you got the idea of how you can build and deploy the git tags to different servers.

PS :- If you are trying to perform the above steps and you encounter any issues, feel free to comment here or connect with me over LinkedIn.

Cloud Enthusiast | 2x AWS Certified | 1x Azure Certified