Creating my first Jenkins Pipeline

2023-08-11
Jenkins

Install the Docker Pipeline plugin through the Manage Jenkins > Plugins page

Search for “Docker Pipeline”

After installing the plugin, restart Jenkins so that the plugin is ready to use

Go “Dashboard” and click on “New Item”.

Enter an item name and choose “Pipeline”, then click on “OK”.

Here is the example I used for testing the pipeline.

https://github.com/foxfromworld/python-test

First, I chose “Hello World” for my “Pipeline script”.

Then, click on “Pipeline Syntax” to find the script for “checkout”, paste your repo URL here, and click on “Generate Pipeline Script”.

The script will be like:

1
2
3
4
5
6
7
8
9
10
11
pipeline {
agent any

stages {
stage('Checkout') {
steps {
checkout scmGit(branches: [[name: 'main']], extensions: [], userRemoteConfigs: [[url: 'https://github.com/foxfromworld/python-test.git']])
}
}
}
}

Go back to the dashboard, click on “Build Now”, you can see the result.

After successfully check out the code, now we can try to build and test the project.

Click on “Pipeline Syntax” to find the script for “sh: Shell Script”,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
pipeline {
agent any

stages {
stage('Checkout') {
steps {
checkout scmGit(branches: [[name: 'main']], extensions: [], userRemoteConfigs: [[url: 'https://github.com/foxfromworld/python-test.git']])
}
}
stage('Build') {
steps {
sh 'python3 countOdds.py'
}
}
}
}

Click on “Build Now” and you can see the message.

Finally, modify the script to add the test stage.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
pipeline {
agent any

stages {
stage('Checkout') {
steps {
checkout scmGit(branches: [[name: 'main']], extensions: [], userRemoteConfigs: [[url: 'https://github.com/foxfromworld/python-test.git']])
}
}
stage('Build') {
steps {
git branch: 'main', url: 'https://github.com/foxfromworld/python-test.git'
sh 'python3 countOdds.py'
}
}
stage('Test') {
steps {
sh 'python3 -m pytest'
}
}
}
}

Opps, I got this when running the test stage.

1
2
+ /usr/bin/python3 -m pytest
/usr/bin/python3: No module named pytest

Try the new script below to install pytest and specify the python3 version. (Once I couldn’t find the pytest even after the installation then I realized it might have been a different python version.) It fixed the problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
pipeline {
agent any

stages {
stage('Checkout') {
steps {
checkout scmGit(branches: [[name: 'main']], extensions: [], userRemoteConfigs: [[url: 'https://github.com/foxfromworld/python-test.git']])
}
}
stage('Build') {
steps {
git branch: 'main', url: 'https://github.com/foxfromworld/python-test.git'
sh 'python3 countOdds.py'
}
}
stage('Test') {
steps {
echo 'Testing..'

withEnv(["HOME=${env.WORKSPACE}"]) {
sh '/usr/bin/python3 -m pip install --upgrade pip'
sh 'pip install -r requirements.txt'
sh 'python3 --version'
sh '/usr/bin/python3.10 -m pytest'
}
}
}
}
}