docker run \
--rm \
-u root \
-p 8080:8080 \
-v jenkins-data:/var/jenkins_home \ (1)
-v /var/run/docker.sock:/var/run/docker.sock \
-v "$HOME":/home \ (2)
jenkinsci/blueocean
This tutorial shows you how to use Jenkins to orchestrate building a simple Python application with PyInstaller.
If you are a Python developer who is new to CI/CD concepts, or you might be familiar with these concepts but don’t know how to implement building your application using Jenkins, then this tutorial is for you.
The simple Python application (which you’ll obtain from a sample repository on GitHub) is a command line tool "add2vals" that outputs the addition of two values. If at least one of the values is a string, "add2vals" treats both values as a string and instead concatenates the values. The "add2" function in the "calc" library (which "add2vals" imports) is accompanied by a set of unit tests. These are tested with pytest to check that this function works as expected and the results are saved to a JUnit XML report.
The delivery of the "add2vals" tool through PyInstaller converts this tool into a standalone executable file for Linux, which you can download through Jenkins and execute at the command line on Linux machines without Python.
Note: Unlike the other tutorials in this documentation, this tutorial requires approximately 500 MB more Docker image data to be downloaded.
Duration: This tutorial takes 20-40 minutes to complete (assuming you’ve already met the prerequisites below). The exact duration will depend on the speed of your machine and whether or not you’ve already run Jenkins in Docker from another tutorial.
You can stop this tutorial at any point in time and continue from where you left off.
If you’ve already run though another tutorial, you can skip the Prerequisites and Run Jenkins in Docker sections below and proceed on to forking the sample repository. (Just ensure you have Git installed locally.) If you need to restart Jenkins, simply follow the restart instructions in Stopping and restarting Jenkins and then proceed on.
For this tutorial, you will require:
A macOS, Linux or Windows machine with:
256 MB of RAM, although more than 512MB is recommended.
10 GB of drive space for Jenkins and your Docker images and containers.
The following software installed:
Docker - Read more about installing Docker in the
Installing Docker section of
the Installing Jenkins page.
Note: If you use Linux, this tutorial assumes that you are not running
Docker commands as the root user, but instead with a single user account that
also has access to the other tools used throughout this tutorial.
Git and optionally GitHub Desktop
In this tutorial, you’ll be running Jenkins as a Docker container from the
jenkinsci/blueocean
Docker
image.
To run Jenkins in Docker, follow the relevant instructions below for either macOS and Linux or Windows.
You can read more about Docker container and image concepts in the Docker and Downloading and running Jenkins in Docker sections of the Installing Jenkins page.
Open up a terminal window.
Run the jenkinsci/blueocean
image as a container in Docker using the
following
docker run
command (bearing in mind that this command automatically downloads the image
if this hasn’t been done):
docker run \
--rm \
-u root \
-p 8080:8080 \
-v jenkins-data:/var/jenkins_home \ (1)
-v /var/run/docker.sock:/var/run/docker.sock \
-v "$HOME":/home \ (2)
jenkinsci/blueocean
1 | Maps the /var/jenkins_home directory in the container to the Docker
volume with the name
jenkins-data . If this volume does not exist, then this docker run command
will automatically create the volume for you. |
2 | Maps the $HOME directory on the host (i.e. your local) machine (usually
the /Users/<your-username> directory) to the /home directory in the
container. |
Note: If copying and pasting the command snippet above doesn’t work, try copying and pasting this annotation-free version here:
docker run \
--rm \
-u root \
-p 8080:8080 \
-v jenkins-data:/var/jenkins_home \
-v /var/run/docker.sock:/var/run/docker.sock \
-v "$HOME":/home \
jenkinsci/blueocean
Proceed to the Setup wizard.
Open up a command prompt window.
Run the jenkinsci/blueocean
image as a container in Docker using the
following
docker run
command (bearing in mind that this command automatically downloads the image
if this hasn’t been done):
docker run ^ --rm ^ -u root ^ -p 8080:8080 ^ -v jenkins-data:/var/jenkins_home ^ -v /var/run/docker.sock:/var/run/docker.sock ^ -v "%HOMEPATH%":/home ^ jenkinsci/blueocean
For an explanation of these options, refer to the macOS and Linux instructions above.
Proceed to the Setup wizard.
If you have some experience with Docker and you wish or need to access the
Jenkins/Blue Ocean Docker container through a terminal/command prompt using the
docker exec
command, you can add an option like --name jenkins-tutorials
(with the
docker run
above), which would give the Jenkins/Blue Ocean Docker container the name
"jenkins-tutorials".
This means you could access the Jenkins/Blue Ocean container (through a separate
terminal/command prompt window) with a docker exec
command like:
docker exec -it jenkins-tutorials bash
Before you can access Jenkins, there are a few quick "one-off" steps you’ll need to perform.
When you first access a new Jenkins instance, you are asked to unlock it using an automatically-generated password.
After the 2 sets of asterisks appear in the terminal/command prompt window,
browse to http://localhost:8080
and wait until the Unlock Jenkins page
appears.
From your terminal/command prompt window again, copy the automatically-generated alphanumeric password (between the 2 sets of asterisks).
On the Unlock Jenkins page, paste this password into the Administrator password field and click Continue.
After unlocking Jenkins, the Customize Jenkins page appears.
On this page, click Install suggested plugins.
The setup wizard shows the progression of Jenkins being configured and the suggested plugins being installed. This process may take a few minutes.
Finally, Jenkins asks you to create your first administrator user.
When the Create First Admin User page appears, specify your details in the respective fields and click Save and Finish.
When the Jenkins is ready page appears, click Start using Jenkins.
Notes:
This page may indicate Jenkins is almost ready! instead and if so, click Restart.
If the page doesn’t automatically refresh after a minute, use your web browser to refresh the page manually.
If required, log in to Jenkins with the credentials of the user you just created and you’re ready to start using Jenkins!
Throughout the remainder of this tutorial, you can stop the Jenkins/Blue Ocean
Docker container by typing Ctrl-C
in the terminal/command prompt window from
which you ran the docker run …
command above.
To restart the Jenkins/Blue Ocean Docker container:
Run the same docker run …
command you ran for macOS,
Linux or Windows above.
Note: This process also updates the jenkinsci/blueocean
Docker image, if
an updated one is available.
Browse to http://localhost:8080
.
Wait until the log in page appears and log in.
Obtain the simple "add" Python application from GitHub, by forking the sample repository of the application’s source code into your own GitHub account and then cloning this fork locally.
Ensure you are signed in to your GitHub account. If you don’t yet have a GitHub account, sign up for a free one on the GitHub website.
Fork the
simple-python-pyinstaller-app
on GitHub into your local GitHub account. If you need help with this process,
refer to the Fork A Repo
documentation on the GitHub website for more information.
Clone your forked simple-python-pyinstaller-app
repository (on GitHub)
locally to your machine. To begin this process, do either of the following
(where <your-username>
is the name of your user account on your operating
system):
If you have the GitHub Desktop app installed on your machine:
In GitHub, click the green Clone or download button on your forked repository, then Open in Desktop.
In GitHub Desktop, before clicking Clone on the Clone a Repository dialog box, ensure Local Path for:
macOS is /Users/<your-username>/Documents/GitHub/simple-python-pyinstaller-app
Linux is /home/<your-username>/GitHub/simple-python-pyinstaller-app
Windows is C:\Users\<your-username>\Documents\GitHub\simple-python-pyinstaller-app
Otherwise:
Open up a terminal/command line prompt and cd
to the appropriate directory
on:
macOS - /Users/<your-username>/Documents/GitHub/
Linux - /home/<your-username>/GitHub/
Windows - C:\Users\<your-username>\Documents\GitHub\
(although use a Git
bash command line window as opposed to the usual Microsoft command prompt)
Run the following command to continue/complete cloning your forked repo:
git clone https://github.com/YOUR-GITHUB-ACCOUNT-NAME/simple-python-pyinstaller-app
where YOUR-GITHUB-ACCOUNT-NAME
is the name of your GitHub account.
Go back to Jenkins, log in again if necessary and click create new jobs
under Welcome to Jenkins!
Note: If you don’t see this, click New Item at the top left.
In the Enter an item name field, specify the name for your new Pipeline
project (e.g. simple-python-pyinstaller-app
).
Scroll down and click Pipeline, then click OK at the end of the page.
( Optional ) On the next page, specify a brief description for your Pipeline
in the Description field (e.g. An entry-level Pipeline demonstrating how to
use Jenkins to build a simple Python application with PyInstaller.
)
Click the Pipeline tab at the top of the page to scroll down to the Pipeline section.
From the Definition field, choose the Pipeline script from SCM option. This option instructs Jenkins to obtain your Pipeline from Source Control Management (SCM), which will be your locally cloned Git repository.
From the SCM field, choose Git.
In the Repository URL field, specify the directory path of your locally
cloned repository above,
which is from your user account/home directory on your host machine, mapped to
the /home
directory of the Jenkins container - i.e.
For macOS - /home/Documents/GitHub/simple-python-pyinstaller-app
For Linux - /home/GitHub/simple-python-pyinstaller-app
For Windows - /home/Documents/GitHub/simple-python-pyinstaller-app
Click Save to save your new Pipeline project. You’re now ready to begin
creating your Jenkinsfile
, which you’ll be checking into your locally cloned
Git repository.
You’re now ready to create your Pipeline that will automate building your Python
application with PyInstaller in Jenkins. Your Pipeline will be created as a
Jenkinsfile
, which will be committed to your locally cloned Git repository
(simple-python-pyinstaller-app
).
This is the foundation of "Pipeline-as-Code", which treats the continuous delivery pipeline a part of the application to be versioned and reviewed like any other code. Read more about Pipeline and what a Jenkinsfile is in the Pipeline and Using a Jenkinsfile sections of the User Handbook.
First, create an initial Pipeline with a "Build" stage that executes the first part of the entire production process for your application. This "Build" stage downloads a Python Docker image and runs it as a Docker container, which in turn compiles your simple Python application into byte code.
Using your favorite text editor or IDE, create and save new text file with the
name Jenkinsfile
at the root of your local simple-python-pyinstaller-app
Git repository.
Copy the following Declarative Pipeline code and paste it into your empty
Jenkinsfile
:
pipeline {
agent none (1)
stages {
stage('Build') { (2)
agent {
docker {
image 'python:2-alpine' (3)
}
}
steps {
sh 'python -m py_compile sources/add2vals.py sources/calc.py' (4)
}
}
}
}
1 | The agent section with the none
parameter specified at the top of this Pipeline code block means that no global
agent will be allocated for the entire Pipeline’s execution and that each
stage directive must specify its own
agent section. |
2 | Defines a stage (directive) called
Build that appears on the Jenkins UI. |
3 | This image parameter (of the agent
section’s docker parameter) downloads the
python:2-alpine Docker image (if it’s not
already available on your machine) and runs this image as a separate container.
This means that:
|
4 | This
sh
step (of the steps section) runs the
Python command to compile your application and its calc library into byte code
files (each with .pyc extension), which are placed into the sources
workspace directory (within the
/var/jenkins_home/workspace/simple-python-pyinstaller-app directory in the
Jenkins container). |
Save your edited Jenkinsfile
and commit it to your local
simple-python-pyinstaller-app
Git repository. E.g. Within the
simple-python-pyinstaller-app
directory, run the commands:
git add .
then
git commit -m "Add initial Jenkinsfile"
Go back to Jenkins again, log in again if necessary and click Open Blue Ocean on the left to access Jenkins’s Blue Ocean interface.
In the This job has not been run message box, click Run, then quickly
click the OPEN link which appears briefly at the lower-right to see Jenkins
running your Pipeline project. If you weren’t able to click the OPEN link,
click the row on the main Blue Ocean interface to access this feature.
Note: You may need to wait a few minutes for this first run to complete.
After making a clone of your local simple-python-pyinstaller-app
Git
repository itself, Jenkins:
Initially queues the project to be run on the agent.
Downloads the Python Docker image and runs it in a container on Docker.
Runs the Build
stage (defined in the Jenkinsfile
) on the Python
container. During this time, Python uses the py_compile
module to compile
the code of your Python application and its calc
library into byte code,
which are stored in the sources
workspace directory (within the Jenkins
home directory).
The Blue Ocean interface turns green if Jenkins compiled your Python application successfully.
Click the X at the top-right to return to the main Blue Ocean interface.
Go back to your text editor/IDE and ensure your Jenkinsfile
is open.
Copy and paste the following Declarative Pipeline syntax immediately under the
Build
stage of your Jenkinsfile
:
stage('Test') {
agent {
docker {
image 'qnib/pytest'
}
}
steps {
sh 'py.test --verbose --junit-xml test-reports/results.xml sources/test_calc.py'
}
post {
always {
junit 'test-reports/results.xml'
}
}
}
so that you end up with:
pipeline {
agent none
stages {
stage('Build') {
agent {
docker {
image 'python:2-alpine'
}
}
steps {
sh 'python -m py_compile sources/add2vals.py sources/calc.py'
}
}
stage('Test') { (1)
agent {
docker {
image 'qnib/pytest' (2)
}
}
steps {
sh 'py.test --verbose --junit-xml test-reports/results.xml sources/test_calc.py' (3)
}
post {
always {
junit 'test-reports/results.xml' (4)
}
}
}
}
}
1 | Defines a stage (directive) called
Test that appears on the Jenkins UI. |
2 | This image parameter (of the agent
section’s docker parameter) downloads the
qnib:pytest Docker image (if it’s not
already available on your machine) and runs this image as a separate container.
This means that:
|
3 | This
sh
step (of the steps section) executes
pytest’s py.test command on sources/test_calc.py , which runs a set of unit
tests (defined in test_calc.py ) on the "calc" library’s add2 function (used
by your simple Python application add2vals ). The:
|
4 | This
junit
step (provided by the JUnit Plugin) archives the
JUnit XML report (generated by the py.test command above) and exposes the
results through the Jenkins interface. In Blue Ocean, the results are accessible
through the Tests page of a Pipeline run. The
post section’s always condition that
contains this junit step ensures that the step is always executed at the
completion of the Test stage, regardless of the stage’s outcome. |
Save your edited Jenkinsfile
and commit it to your local
simple-python-pyinstaller-app
Git repository. E.g. Within the
simple-python-pyinstaller-app
directory, run the commands:
git stage .
then
git commit -m "Add 'Test' stage"
Go back to Jenkins again, log in again if necessary and ensure you’ve accessed Jenkins’s Blue Ocean interface.
Click Run at the top left, then quickly click the OPEN link which appears
briefly at the lower-right to see Jenkins running your amended Pipeline
project. If you weren’t able to click the OPEN link, click the top row
on the Blue Ocean interface to access this feature.
Note: It may take a few minutes for the qnib:pytest
Docker image to
download (if this hasn’t already been done).
If your amended Pipeline ran successfully, here’s what the Blue Ocean
interface should look like. Notice the additional "Test" stage. You can click
on the previous "Build" stage circle to access the output from that stage.
Click the X at the top-right to return to the main Blue Ocean interface.
Go back to your text editor/IDE and ensure your Jenkinsfile
is open.
Copy and paste the following Declarative Pipeline syntax immediately under the
Test
stage of your Jenkinsfile
:
stage('Deliver') {
agent {
docker {
image 'cdrx/pyinstaller-linux:python2'
}
}
steps {
sh 'pyinstaller --onefile sources/add2vals.py'
}
post {
success {
archiveArtifacts 'dist/add2vals'
}
}
}
and add a skipStagesAfterUnstable
option so that you end up with:
pipeline {
agent none
options {
skipStagesAfterUnstable()
}
stages {
stage('Build') {
agent {
docker {
image 'python:2-alpine'
}
}
steps {
sh 'python -m py_compile sources/add2vals.py sources/calc.py'
}
}
stage('Test') {
agent {
docker {
image 'qnib/pytest'
}
}
steps {
sh 'py.test --verbose --junit-xml test-reports/results.xml sources/test_calc.py'
}
post {
always {
junit 'test-reports/results.xml'
}
}
}
stage('Deliver') { (1)
agent {
docker {
image 'cdrx/pyinstaller-linux:python2' (2)
}
}
steps {
sh 'pyinstaller --onefile sources/add2vals.py' (3)
}
post {
success {
archiveArtifacts 'dist/add2vals' (4)
}
}
}
}
}
1 | Defines a stage (directive) called
Deliver that appears on the Jenkins UI. |
2 | This image parameter (of the agent
section’s docker parameter) downloads the
cdrx/pyinstaller-linux Docker
image (if it’s not already available on your machine) and runs this image as a
separate container. This means that:
|
3 | This
sh
step (of the steps section) executes
the pyinstaller command (in the PyInstaller container) on your simple Python
application. This bundles your add2vals.py Python application into a single
standalone executable file (via the --onefile option) and outputs the this
file to the dist workspace directory (within the Jenkins home directory).
Although this step consists of a single command, as a general principle, it’s a
good idea to keep your Pipeline code (i.e. the Jenkinsfile ) as tidy as
possible and place more complex build steps (particularly for stages consisting
of 2 or more steps) into separate shell script files like the deliver.sh file.
This ultimately makes maintaining your Pipeline code easier, especially if your
Pipeline gains more complexity. |
4 | This
archiveArtifacts
step (provided as part of Jenkins core) archives the standalone executable file
(generated by the pyinstaller command above at dist/add2vals within the
Jenkins home’s workspace directory) and exposes this file through the Jenkins
interface. In Blue Ocean, archived artifacts like these are accessible through
the Artifacts page of a Pipeline run. The
post section’s success condition that
contains this archiveArtifacts step ensures that the step is executed at the
completion of the Deliver stage only if this stage completed successfully. |
Save your edited Jenkinsfile
and commit it to your local
simple-python-pyinstaller-app
Git repository. E.g. Within the
simple-python-pyinstaller-app
directory, run the commands:
git stage .
then
git commit -m "Add 'Deliver' stage"
Go back to Jenkins again, log in again if necessary and ensure you’ve accessed Jenkins’s Blue Ocean interface.
Click Run at the top left, then quickly click the OPEN link which appears
briefly at the lower-right to see Jenkins running your amended Pipeline
project. If you weren’t able to click the OPEN link, click the top row
on the Blue Ocean interface to access this feature.
Note: It may take a few minutes for the cdrx/pyinstaller-linux
Docker
image to download (if this hasn’t already been done).
If your amended Pipeline ran successfully, here’s what the Blue Ocean
interface should look like. Notice the additional "Deliver" stage. Click on
the previous "Test" and "Build" stage circles to access the outputs from those
stages.
Here’s what the output of the "Deliver" stage should look like, showing you the results of PyInstaller bundling your Python application into a single standalone executable file.
Click the X at the top-right to return to the main Blue Ocean interface, which lists your previous Pipeline runs in reverse chronological order.
If you use Linux, you can try running the standalone add2vals
application you
generated with PyInstaller locally on your machine. To do this:
From the main Blue Ocean interface, access your last Pipeline run you performed above. To do this, click the top row (representing the most recent Pipeline run) on the main Blue Ocean’s Activity page.
On the results page of the Pipeline run, click Artifacts at the top right to access the Artifacts page.
In the list of artifacts, click the down-arrow icon at the far right of the dist/add2vals artifact item to download the standalone executable file to your browser’s "Downloads" directory.
Back in your operating system’s terminal prompt, cd
to your browser’s
"Downloads" directory.
Make the add2vals
file executable - i.e. chmod a+x add2vals
Run the command ./add2vals
and follow the instructions provided by your app.
Well done! You’ve just used Jenkins to build a simple Python application!
The "Build", "Test" and "Deliver" stages you created above are the basis for building more complex Python applications in Jenkins, as well as Python applications that integrate with other technology stacks.
Because Jenkins is extremely extensible, it can be modified and configured to handle practically any aspect of build orchestration and automation.
To learn more about what Jenkins can do, check out:
The Tutorials overview page for other introductory tutorials.
The User Handbook for more detailed information about using Jenkins, such as Pipelines (in particular Pipeline syntax) and the Blue Ocean interface.
The Jenkins blog for the latest events, other tutorials and updates.
Please submit your feedback about this page through this quick form.
Alternatively, if you don't wish to complete the quick form, you can simply indicate if you found this page helpful?
See existing feedback here.