How can I check for changes made to Jenkinsfile locally?

When writing tubes, jenkins seems very inconvenient to make every new change to see if it works.

Is there a way to execute them locally without passing code?

+114
jenkins jenkins-pipeline jenkins-workflow
Mar 30 '16 at 12:35
source share
13 answers

You cannot run the Pipeline script locally, since its entire purpose is the Jenkins script. (This is one of the reasons it is best to keep your Jenkinsfile short and limited code that really deals with Jenkins functions; your actual build logic should be handled by external processes or build tools that you invoke via a single-line sh or bat .)

If you want to test the change to Jenkinsfile live, but not Jenkinsfile it, use the Replay function added in 1.14

JENKINS-33925 tracks the desired auto test structure.

+81
Mar 31 '16 at 11:16
source share

TL; DR
Jenkins Pipe Module Testing Framework

Long version
The Jenkins pipeline test is becoming more and more sick. Unlike the classic declarative approach to setting up work, when the user limited himself to opening the new Jenkins Pipeline with the user interface, this is a complete programming language for the build process in which you mix the declarative part with your own code. As good developers, we want to have some unit tests for this type of code.

There are three steps involved in developing Jenkins Pipelines. step 1. should cover 80% of use cases.

  • Make as many build scripts as possible (e.g. Maven, Gradle, Gulp, etc.). Then, in the scripts of your pipeline, the build tasks in the correct order will simply be called. The assembly protocol simply organizes and performs the assembly tasks, but does not have any important logic that requires special testing.
  • If the previous rule cannot be fully applied, go to the Common Pipeline Libraries , where you can independently develop and test your own logic and integrate them into the pipeline.
  • If all of the above does not help you, you can try one of those libraries that appeared recently (March 2017). Jenkins Pipeline Unit or pipelineUnit testing framework (examples)

Examples

pipelineUnit The GitHub repository contains some of Spock's examples of how to use the Jenkins pipeline module testing basics.

+37
Mar 18 '17 at 14:15
source share

I have a solution that works well for me. It consists of a local jenkins running in docker and a git web hook to run the pipeline in local jenkins with every commit. You no longer need to click on your github or bitbucket repository to test the pipeline.

This has been tested only on Linux.

It is quite simple to make this work, although this instruction is a bit. Most of the steps are there.

This is what you need

  • Docker installed and running. This is not part of this manual.
  • Jenkins works in docker locally. Explained as below.
    • The correct permissions (ssh key) for your local Jenkins docker user to fetch from your local git repository. Explained as below.
    • The Jenkins pipeline project that is pulled from your local git repository. Explained below.
    • The git user in your local Jenkins with minimal privileges. Explained below.
  • A git project with a post-fix web hook that launches a pipeline project. Explained below.

This is how you do it

Jenkins docker

Create a file called Dockerfile instead of the one you selected. I /opt/docker/jenkins/Dockerfile it in /opt/docker/jenkins/Dockerfile fill it like this:

 FROM jenkins/jenkins:lts USER root RUN apt-get -y update && apt-get -y upgrade # Your needed installations goes here USER jenkins 

Create a local_jenkins image

You will only need to do this once or after adding something to the Dockerfile.

 $ docker build -t local_jenkins /opt/docker/jenkins/Dockerfile 

Run and restart local_jenkins

From time to time, you want to easily start and restart jenkins. For example, after rebooting your machine. To do this, I made an alias that I placed in .bash_aliases in my home folder.

 $ echo "alias localjenkinsrestart='docker stop jenkins;docker rm jenkins;docker run --name jenkins -i -d -p 8787:8080 -p 50000:50000 -v /opt/docker/jenkins/jenkins_home:/var/jenkins_home:rw local_jenkins'" >> ~/.bash_aliases $ source .bash_aliases # To make it work 

Make sure that the /opt/docker/jenkins/jenkins_home exists and that you have read and write permissions for it.

To start or restart your Jenkins just type:

 $ localjenkinsrestart 

Everything you do in your local jenkins will be stored in the / opt / docker / jenkins / jenkins_home folder and saved between restarts.

Create ssh passkey in your docker

This is a very important part for this to work. First, we launch the Docker container and create a bash shell for it:

 $ localjenkinsrestart $ docker exec -it jenkins /bin/bash 

Now you are logged into the docker container, you can see it with something like jenkins@e7b23bad10aa:/$ in your terminal. The hash after @ will probably be different.

Create key

 jenkins@e7b23bad10aa:/$ ssh-keygen 

Press enter for all questions until you get an answer

Copy the key to your computer. If you are interested, from the Docker container your computer is 172.17.0.1.

 jenkins@e7b23bad10aa:/$ ssh-copy-id user@172.17.0.1 

user = your username, and 172.17.0.1 is the IP address of your computer from the Docker container.

You will need to enter your password at this point.

Now let's try to end the cycle by running the ssh command on your computer from the Docker container.

 jenkins@e7b23bad10aa:/$ ssh user@172.17.0.1 

This time you do not need to enter a password. If you do, something went wrong and you should try again.

You will now be in the home folder of your computer. Try ls and see.

Do not stop here because we have a chain of ssh shells that we need to exit.

 $ exit jenkins@e7b23bad10aa:/$ exit 

Right! Now we are back and ready to continue.

Set up your jenkins

You will find your local Jenkins in your browser at http: // localhost: 8787 .

When you point your browser to your local Jenkins for the first time, you will be marked by the installation wizard. By default, everything is in order, do not forget to install the pipeline plugin during installation.

Customize Your Jenkins

It is very important that you activate matrix protection on http: // localhost: 8787 / configureSecurity and give yourself all the rights by adding yourself to the matrix and checking all the checkboxes. (There is a check mark in the far right corner)

  • Select Jenkins own user database as the security area
  • Select Matrix-based security on Matrix-based security in the Authorization section
  • Enter your username in the User/group to add: field and click on the [ Add ] button
  • In the table above, your username should appear next to the people icon. If it intersects, you have entered an invalid username.
  • Go to the far right corner of the table and click the checkmark button or manually select all the fields in your row.
  • Make sure the Prevent Cross Site Request Forgery exploits check box is unchecked. (Since this Jenkins is only accessible from your computer, this is not such a big deal)
  • Click [ Save ] and exit Jenkins and log in again to make sure this works. If this is not the case, you need to start over and /opt/docker/jenkins/jenkins_home folder /opt/docker/jenkins/jenkins_home before restarting

Add git user

We need to allow our git hook to connect to our local Jenkins with minimal permissions. Just look and build jobs. Therefore, we create a user with the name git and password login .

Go to the browser at http: // localhost: 8787 / securityRealm / addUser and add git as the username and login in login as the password. Click [ Create User ] .

Add permissions to git user

Go to the http: // localhost: 8787 / configureSecurity page in your browser. Add the git user to the matrix:

  • Type git in the User/group to add: field and click [ Add ]

Now it's time to check the boxes for minimal git user rights. Only this is necessary:

  • in general: read
  • work: assembly
  • Work: open
  • work: reading

Make sure that the " Prevent Cross Site Request Forgery exploits " box is unchecked and click [ Save ]

Create a pipeline project

We assume that we have the name user and our project with git support with Jenkinsfile is called project in it and is located at /home/user/projects/project

In your http: // localhost: 8787 Jenkins add a new pipeline project. I called it a hook for reference.

  • Click on New Item on the Jenkins menu
  • Name the project hookpipeline
  • Click on the pipeline
  • Click [ OK ]
  • Select the Poll SCM check box in the Build Triggers section. Leave the schedule empty.
  • In the Pipeline section:
    • select Pipeline script from SCM
    • in the Repository URL field, enter user@172.17.0.1:projects/project/.git
    • in the Script Path field enter Jenkinsfile
  • Save Hookpipeline Project
  • Manually create a trunk once, this is necessary for SCM polling to work.

Create a git hook

Go to the /home/user/projects/project/.git/hooks folder and create a file called post-commit that contains this:

 #!/bin/sh BRANCHNAME=$(git rev-parse --abbrev-ref HEAD) MASTERBRANCH='master' curl -XPOST -u git:login http://localhost:8787/job/hookpipeline/build echo "Build triggered successfully on branch: $BRANCHNAME" 

Make this file executable:

 $ chmod +x /home/user/projects/project/.git/hooks/post-commit 

Check hook after commit:

 $ /home/user/projects/project/.git/hooks/post-commit 

Check in Jenkins to see if your trap project has been launched.

Finally, make some arbitrary changes to your project, add the changes, and commit. This will now trigger the pipeline in your local Jenkins.

Happy Days!

+34
Dec 06 '17 at 10:06 on
source share

At the time of writing (end of July 2017) , using the Blue Ocean plugin, you can check the syntax of the declarative pipeline directly in the visual editor of the pipeline . The editor works from the Blue Ocean user interface when you click "configure" only for github projects (this is a known issue and they work to make it work on git too, etc.).

But, as explained in this question, you can open the editor by clicking on the link:

[Jenkins URL]/blue/organizations/jenkins/pipeline-editor/

Then click in the middle of the page and press Ctrl+S , this will open the text area where you can paste the declarative script of the pipeline. If you click Refresh if a syntax error occurs, the editor will tell you where the syntax error is. Like in this screenshot:

As a quick test I wrongly typed "stepps" instead of "steps"

If there is no syntax error, the text box closes and the page displays your pipeline. Do not worry, this will not save anything (if it is a github project, it will make a change to Jenkinsfile).

I am new to Jenkins, and it is very useful, without it I had to fix Jenkinsfile many times before it worked (very annoying!). Hope this helps. Greetings.

+9
Jul 26 '17 at 15:00
source share

Jenkins has a Play function that allows you to quickly play a task without updating sources:

Replay feature

+7
Jul 04 '18 at 13:41
source share

As far as I know, this Pipeline Plugin is the β€œengine” of the new Jenkinsfile mechanics, so I'm pretty sure you can use this to test your scripts locally.

I am not sure that when copying to a Jenkins file you need to follow any additional steps, however the syntax, etc. must be exactly the same.

Edit: Found a link to the "engine", mark this description of the function, the last paragraph, the first entry.

+4
Mar 30 '16 at 16:07
source share

In my development setup β€” missing the correct Groovy editor β€” a large number of Jenkinsfile problems arise from simple syntax errors. To solve this problem, you can check the Jenkins file for your Jenkins instance (works in $JENKINS_HTTP_URL ):

curl -X POST -H $(curl '$JENKINS_HTTP_URL/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)') -F "jenkinsfile=<Jenkinsfile" $JENKINS_HTTP_URL/pipeline-model-converter/validate

The above command is a slightly modified version from https://github.com/jenkinsci/pipeline-model-definition-plugin/wiki/Validating-(or-linting)-a-Declarative-Jenkinsfile-from-the-command-line

+4
Apr 20 '17 at 8:47
source share

A bit late for the party, but that is why I wrote jenny , a small reimplementation of some of the basic steps of Jenkinsfile. ( https://github.com/bmustiata/jenny )

+4
May 11 '18 at 20:59
source share

Besides the snooze function that others have already mentioned (not to mention its usefulness!), I found the following useful:

  • Create a Pipeline test task in which you can enter the pipeline code or specify your Jenkins repo / file branch to quickly check something. For more accurate testing, use the Multibranch Pipeline, which points to your own plug where you can quickly make changes and commit without affecting prod. Things like BRANCH_NAME env are only available on Multibranch.
  • Since Jenkinsfile is Groovy code, just call it with groovy Jenkinsfile "to check the basic syntax.
+1
Jul 21 '17 at 21:17
source share

Put your SSH key in your Jenkins profile, then use the declarative linter as follows:

 ssh jenkins.hostname.here declarative-linter < Jenkinsfile 

This will do a static analysis of your Jenkinsfile. In the editor of your choice, define the keyboard shortcut that automatically launches this command. In the Visual Studio Code that I'm using, go to Tasks> Configure Tasks, then use the following JSON to create the Validate Jenkinsfile command:

 { "version": "2.0.0", "tasks": [ { "label": "Validate Jenkinsfile", "type": "shell", "command": "ssh jenkins.hostname declarative-linter < ${file}" } ] } 
0
Feb 16 '18 at 11:26
source share

I use future playback to make some updates and run fast.

0
Jul 03 '18 at 12:19
source share

With some limitations for script pipelines, I use this solution:

  1. Pipeline work with a built-in sheet pile script:



 node('master') { stage('Run!') { def script = load('...you job file...') } } 



  1. Jenkinsfile for testing has the same structure as for lesfurets:



 def execute() { ... main job code here ... } execute() 
0
Aug 01 '18 at 13:03
source share

A good summary of the tools available here: Jenkins development tools

0
Jan 16 '19 at 15:25
source share



All Articles