Jenkins is an open source automation server written in Java, intended to automate repetitive technical tasks involved in Continuous Integration, Continuous Delivery and Continuous Deployment of software.
The creation of a pipeline can be done by adding a Jenkinsfile on the root folder of the application repository. There are two possible syntaxes that can be used to define the pipeline:
- Declarative pipeline: Simplified declarative DSL.
- Scripted pipeline: General purpose DSL built on top of groovy.
The Jenkinsfile allows you the steps to build, test, generate test coverage reports, run static code analysis, run stress tests and much more. The number and complexity of the steps on the pipeline depends on the characteristics of the application.
To create a pipeline script we need two things:
- A text editor
- A Jenkins server
Sound simple, right? Yes, but there is a problem: the text editor is on our local box and the Jenkins server is remote. We need to run the code on the server and we can update the Jenkinsfile, what if we had a syntax error? Wouldn't it be better to have immediate feedback?
My approach to developing Jenkins pipeline scripts consists on working on my favorite text editor locally and send the content to the script console using curl to the the output of the execution on the command line. That's what you are going to learn.
- Linux/Mac (Used Linux Mint 18)
- Docker (Used Docker 17.05.0-ce)
Step 1 - Create a testing Jenkins server
Download the repo and customize parameters
Download the content of my repo: https://github.com/alfredocambera/jenkins-pipeline-dev-env.git
git clone https://github.com/alfredocambera/jenkins-pipeline-dev-env.git
The user, password and full are defined on
jenkins-pipeline-dev-env/init-files/basic-security.groovy. The default values are:
def username = "random" def password = "randompassword" def fullName = "The Administrator"
Update any of the parameters as required.
Run the following command on the directory creating after download the repo:
cd jenkins-pipeline-dev-env docker build --no-cache -t jenkins:test .
To run the container:
docker run --name jenkins -d -p 8080:8080 jenkins:test
Execute script on remote server
- To be able to the send the POST request with the content of our groovy script, we will need an username and its corresponding API token.
This information is available on the container log file as the output of the execution of:
jenkins-pipeline-dev-env/init-files/basic-security.groovy. Here is how to get the required information:
[root@servername directory]# docker logs jenkins 2>/dev/null |grep -E "^\[INFO\]" [INFO]-------------------------------------------------- [INFO] Creating user: Username: 'random' [INFO] Creating user: Password: 'randompassword' [INFO] Creating user: Full Name: 'The Administrator' [INFO] Creating user: API token: '3ba6fffeec019991ffca8598a4b663c0' [INFO]--------------------------------------------------
- To run the script:
[root@servername jenkins-pipeline-dev-env]# bash run-script.sh Testing from groovy
This will send the content of
script.groovy to the jenkins server for execution and will return the output. Here is the content of the example script file:
import jenkins.* import jenkins.model.* import hudson.* import hudson.model.* println "Testing from groovy"
Now you can update the code on
script.groovy to develop your pipeline.
In this article you created a local development environment using jenkins:lts docker container. Current method installs very few plugins out of the box but you can install required plugins using the UI or updating the
plugins.txt and rebuilding the container.
Being able to edit groovy code using an IDE, and running the code directly on the target server will greatly improve your productivity.
On the next part I'll show you how to create a Jenkins job by using out development environment.
How do you develop you Jenkins pipelines? Leave a comment. I'm very interesting on knowing other approaches to the problem.