6.3.6 Lab - Build A CI-CD Pipeline Using Jenkins - ILM
6.3.6 Lab - Build A CI-CD Pipeline Using Jenkins - ILM
Instructor Note: Red font color or gray highlights indicate text that appears in the instructor copy only.
Objectives
Part 1: Launch the DEVASC VM
Part 2: Commit the Sample App to Git
Part 3: Modify the Sample App and Push Changes to Git
Part 4: Download and Run the Jenkins Docker Image
Part 5: Configure Jenkins
Part 6: Use Jenkins to Run a Build of Your App
Part 7: Use Jenkins to Test a Build
Part 8: Create a Pipeline in Jenkins
Background / Scenario
In this lab, you will commit the Sample App code to a GitHub repository, modify the code locally, and then
commit your changes. You will then install a Docker container that includes the latest version of Jenkins. You
will configure Jenkins and then use Jenkins to download and run your Sample App program. Next, you will
create a testing job inside Jenkins that will verify your Sample App program successfully runs each time you
build it. Finally, you will integrate your Sample App and testing job into a Continuous Integration/Continuous
Development pipeline that will verify your Sample App is ready to be deployed each time you change the
code.
Required Resources
1 PC with operating system of your choice
Virtual Box or VMWare
DEVASC Virtual Machine
Instructions
2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 1 of 25 www.netacad.com
Lab - Build a CI/CD Pipeline Using Jenkins
2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 2 of 25 www.netacad.com
Lab - Build a CI/CD Pipeline Using Jenkins
Step 5: Stage, commit, and push the sample-app files to the GitHub repository.
a. Use the git add command to stage the files in the jenkins/sample-app directory. Use the asterisk (*)
argument to stage all files in the current directory.
devasc@labvm:~/labs/devnet-src/jenkins/sample-app$ git add *
b. Use the git status command to see the files and directories that are staged and ready to be committed to
your GitHub repository.
devasc@labvm:~/labs/devnet-src/jenkins/sample-app$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: sample-app.sh
new file: sample_app.py
new file: static/style.css
new file: templates/index.html
2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 3 of 25 www.netacad.com
Lab - Build a CI/CD Pipeline Using Jenkins
c. Use the git commit command to commit the staged files and start tracking changes. Add a message of
your choice or use the one provided here.
devasc@labvm:~/labs/devnet-src/jenkins/sample-app$ git commit -m "Committing
sample-app files."
[master 4030ab6] Committing sample-app files
4 files changed, 46 insertions(+)
create mode 100644 sample-app.sh
create mode 100644 sample_app.py
create mode 100644 static/style.css
create mode 100644 templates/index.html
d. Use the git push command to push your local sample-app files to your GitHub repository.
devasc@labvm:~/labs/devnet-src/jenkins/sample-app$ git push origin master
Username for 'https://github.com': username
Password for 'https://[email protected]': Personal Access Token
Enumerating objects: 9, done.
Counting objects: 100% (9/9), done.
Delta compression using up to 2 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (8/8), 1.05 KiB | 1.05 MiB/s, done.
Total 8 (delta 0), reused 0 (delta 0)
To https://github.com/AllJohns/sample-app.git
d0ee14a..4030ab6 master -> master
2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 4 of 25 www.netacad.com
Lab - Build a CI/CD Pipeline Using Jenkins
Note: If, instead of a request for your username, you get a message from VS Code with the message,
The extension ‘Git’ wants to sign in using GitHub, then you misconfigured either your GitHub
credentials in Step 2 and/or the GitHub URL in Step 4. The URL must have the correct case-sensitive
username and the name of the repository that you created in Step 1. To reverse your previous git add
command, use the command git remote rm origin. Then return to Step 2 making sure to enter the
correct credentials and, in Step 4, entering the correct URL.
Note: If, after entering your username and Personal Access Token as password, you get a fatal error
stating repository is not found, you most likely submitted an incorrect URL. You will need to reverse your
git add command with the git remote rm origin command.
sample = Flask(__name__)
@sample.route("/")
def main():
return render_template("index.html")
if __name__ == "__main__":
sample.run(host="0.0.0.0", port=5050)
2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 5 of 25 www.netacad.com
Lab - Build a CI/CD Pipeline Using Jenkins
2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 6 of 25 www.netacad.com
Lab - Build a CI/CD Pipeline Using Jenkins
b. In sample-app.sh, change the three instances of port 8080 to 5050 as shown below.
#!/bin/bash
mkdir tempdir
mkdir tempdir/templates
mkdir tempdir/static
cp sample_app.py tempdir/.
cp -r templates/* tempdir/templates/.
cp -r static/* tempdir/static/.
cd tempdir
2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 7 of 25 www.netacad.com
Lab - Build a CI/CD Pipeline Using Jenkins
2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 8 of 25 www.netacad.com
Lab - Build a CI/CD Pipeline Using Jenkins
b. Open a browser tab and navigate to localhost:5050. You should see the message You are calling me
from 172.17.0.1.
c. Shut down the server when you have verified that it is operating on port 5050. Return to the terminal
window where the server is running and press CTRL+C to stop the server.
2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 9 of 25 www.netacad.com
Lab - Build a CI/CD Pipeline Using Jenkins
b. You can verify that your GitHub repository is updated by visiting https://github.com/github-
user/sample-app. You should see your new message (Changed port from 8080 to 5050.) and that the
latest commit timestamp has been updated.
2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 10 of 25 www.netacad.com
Lab - Build a CI/CD Pipeline Using Jenkins
2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 11 of 25 www.netacad.com
Lab - Build a CI/CD Pipeline Using Jenkins
2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 12 of 25 www.netacad.com
Lab - Build a CI/CD Pipeline Using Jenkins
Jenkins initial setup is required. An admin user has been created and a password
generated.
Please use the following password to proceed to installation:
*************************************************************
*************************************************************
*************************************************************
<output omitted>
2020-05-12 16:34:29.608+0000 [id=19] INFO hudson.WebAppMain$3#run: Jenkins is
fully up and running
Note: If you lose the password, or it does not display as shown above, or you need to restart the Jenkins
sever, you can always retrieve the password by accessing the command line of Jenkins Docker container.
Create a second terminal window in VS Code and enter the following commands so that you do not stop the
Jenkins server.:
devasc@labvm:~/labs/devnet-src/jenkins/sample-app$ docker exec -it
jenkins_server /bin/bash
root@19d2a847a54e:/# cat /var/jenkins_home/secrets/initialAdminPassword
77dc402e31324c1b917f230af7bfebf2
root@19d2a847a54e:/# exit
exit
devasc@labvm:~/labs/devnet-src/jenkins/sample-app$
Note: Your container ID (19d2a847a54e highlighted above) and password will be different.
2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 13 of 25 www.netacad.com
Lab - Build a CI/CD Pipeline Using Jenkins
| +----------------------------------+ |
| |DEVASC VM | |
| | +----------------------------+ | |
| | |Docker container | | |
| | | +----------------------+ | | |
| | | | Jenkins server | | | |
| | | | +----------------+ | | | |
| | | | |Docker container| | | | |
| | | | +----------------+ | | | |
| | | +----------------------+ | | |
| | +----------------------------+ | |
| +----------------------------------+ |
+----------------------------------------+
2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 14 of 25 www.netacad.com
Lab - Build a CI/CD Pipeline Using Jenkins
2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 15 of 25 www.netacad.com
Lab - Build a CI/CD Pipeline Using Jenkins
2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 16 of 25 www.netacad.com
Lab - Build a CI/CD Pipeline Using Jenkins
2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 17 of 25 www.netacad.com
Lab - Build a CI/CD Pipeline Using Jenkins
b. Click the Source Code Management tab and choose the Git radio button. In the Repository URL field,
add your GitHub repository link for the sample-app taking care to enter your case-sensitive username. Be
sure to add the .git extension at the end of your URL. For example:
https://github.com/github-username/sample-app.git
2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 18 of 25 www.netacad.com
Lab - Build a CI/CD Pipeline Using Jenkins
d. In the Add Credentials dialog box, fill in your GitHub username and Personal Access Token as
password, and then click Add.
Note: You will receive an error message that the connection has failed. This is because you have not
selected the credentials yet.
2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 19 of 25 www.netacad.com
Lab - Build a CI/CD Pipeline Using Jenkins
e. In the dropdown for Credentials where it currently says None, choose the credentials you just
configured.
f. After you have added the correct URL and credentials, Jenkins tests access to the repository. You should
have no error messages. If you do, verify your URL and credentials. You will need to Add them again as
there is no way at this point to delete the ones you previously entered.
g. At the top of the BuildAppJob configuration window, click the Build tab.
h. For the Add build step dropdown, choose Execute shell.
i. In the Command field, enter the command you use to run the build for sample-app.sh script.
bash ./sample-app.sh
j. Click the Save button. You are returned to the Jenkins dashboard with the BuildAppJob selected.
2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 20 of 25 www.netacad.com
Lab - Build a CI/CD Pipeline Using Jenkins
Step 6: Open another web browser tab and verify sample app is running.
Type in the local address, localhost:5050. You should see the content of your index.html displayed in light
steel blue background color with You are calling me from 172.17.0.1 displayed in as H1.
2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 21 of 25 www.netacad.com
Lab - Build a CI/CD Pipeline Using Jenkins
samplerunning
devasc@labvm:~/labs/devnet-src/jenkins/sample-app$ docker rm samplerunning
samplerunning
Step 3: Write the test script that should run after a stable build of the BuildAppJob.
a. Click the Build tab.
b. Click Add build step and choose Execute shell.
c. Enter the following script. The if command should be all on one line including the ; then. This command
will grep the output returned from the cURL command to see if You are calling me from 172.17.0.1 is
returned. If true, the script exits with a code of 0, which means that there are no errors in the
BuildAppJob build. If false, the script exits with a code of 1 which means the BuildAppJob failed.
if curl http://172.17.0.1:5050/ | grep "You are calling me from 172.17.0.1"; then
exit 0
else
exit 1
fi
d. Click Save and then the Back to Dashboard link on the left side.
2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 22 of 25 www.netacad.com
Lab - Build a CI/CD Pipeline Using Jenkins
b. It is not necessary to verify your sample app is running because the TestAppJob already did this for you.
However, you can open a browser tab for 172.17.0.1:5050 to see that it is indeed running.
2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 23 of 25 www.netacad.com
Lab - Build a CI/CD Pipeline Using Jenkins
}
stage('Results') {
build 'TestAppJob'
}
}
2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 24 of 25 www.netacad.com
Lab - Build a CI/CD Pipeline Using Jenkins
2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 25 of 25 www.netacad.com