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
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
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!