Configuring Jenkins using conditional steps

Configuring Jenkins using ‘conditional steps’ for building multiple services that have different build configurations using a single job

In this post, We will tell you how to set up conditional steps in Jenkins to configure multiple services using a single build job. Let’s consider the below use case.

Use case:

Consider the below screenshot, where we have a main repository, with 4 different services.

MS Service 1. MS Service 2, and MS Service 3, use gradle /maven to build the artifact (.jar or. war). After the build, the artifact must be converted to a docker image and to be pushed to AWS ECR

The fourth service contains only a shell script and a Docker File. The build must create a docker image with the execution steps mentioned in the DockerFile for the shell script and push the image to the AWS ECR repository.

In Jenkins, set up a single build job that accomplishes the above tasks rather than having multiple build jobs for each service. This is for better extensibility and maintenance.

1. In Jenkins, under the job, enable choice Parameter option as seen in the screenshot below. Add all the services that need to be built and pushed to the AWS ECR repository.

2. Under Source Code management, give the main repository details as mentioned below.

3. Now in the Build section, look for Conditional steps (multiple) from the Add Build step drop-down.

4. Set up the configuration to build the docker image which does not need gradle and push the image to the ECR repository.

5. Add one more conditional step (multiple) configure the Gradle and execute commands to build the docker image.

Troubleshooting:

While executing the job for conditional steps with gradle, there are chances where it can throw ‘version’ cannot be found. Create a file gradle.properties under the service and define the version of your services. 

Recent Comments

No comments

Leave a Comment