Setup Jenkins Declarative Pipeline

Vipul Tiwari
2 min readJan 25, 2022

In this blog, we’ll work on how to set up Jenkins and create a pipeline that will connect to your git repo and run the build. To progress ahead for this POC I have installed Jenkins running in my docker container. Use the below command if you want to create one.

sudo docker run -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home jenkins/jenkins:lts-jdk11

This command will set up Jenkins in a docker container and can be accessed on http://localhost:8080/.

You might face issues with maven installation in a container so to resolve this issue use: docker exec -t -u root<CONTAINER_ID> bash and install the maven using apt install maven.

Now create Jenkinsfile as shown below and place the same in your root access of git repo.

Use the environment above in case you want to set some different environment variables for your pipeline.

Now set up credentials in Jenkins to connect to your git repo. Go to GitHub -> Setting -> Developer Settings -> Generate new token with repo privileges. You’ll get a token in the popup. Save this token we need this later.

Now click New Item -> Pipeline -> Advanced Project Options.

Now add the Repository URL and select Add from the credential. Popup will appear where you need to place Github username and password which we generated above and also select the checkbox for Treat username as secret.

Hit the save button after selecting credentials If any access error comes before selecting credentials they will automatically be removed after authentication is successful.

You will start seeing the below screen.

Now hit Build Now from the left options.

Build output will show you which stage is been executed and what is the output. Check the echo we placed in the Jenkins file is showing in Jenkins build output.

Thank you. Happy Reading!!

--

--