Last active
July 29, 2022 14:10
-
-
Save dmccuk/8b17f8ab3b54c2dc5908b29454b8420e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Subscribe to our YouTube channel here: https://www.youtube.com/c/LondonIAC/videos | |
### What you need: | |
1) A Jenkins server with SSH/pem key access to a webserver | |
(Follow this to build install Jenkins: https://youtu.be/-GyAraXQVWM) | |
2) A Github account | |
(The code I'm using today https://github.com/dmccuk/webapp1) | |
3) A webserver to display the results. | |
(that Jenkins can SSH to) | |
We'll complete these steps: | |
• Setup a new Jenkins pipeline. | |
• Add some simple pipeline code for a few demo stages. | |
• Then create an access token in GitHub. | |
• Setup the credentials in Jenkins. | |
• Add a webhook to our Github project. | |
• Add a JenkinsFile with the pipeline code to our GitHub project. | |
• Modify the pipeline in Jenkins to run a script from SCM/Git. | |
• Test the Githook on commit to the GitHub project. | |
• Watch the pipeline in action | |
1) Login to Jenkins | |
2) Create a new item - Give a name, click on pipeline and click ok | |
3) Scroll down to pipeline code | |
Visit: https://www.jenkins.io/doc/book/pipeline/getting-started/ | |
pipeline { | |
agent any | |
stages { | |
stage('Stage 1') { | |
steps { | |
echo 'Hello world!' | |
} | |
} | |
} | |
} | |
Copy the Jenkins file code sample | |
Paste it into the pipe line box in Jenkins | |
4) Save and build - You can see stage one is there | |
5) Now create 3 stages: | |
pipeline { | |
agent any | |
stages { | |
stage('Stage 1') { | |
steps { | |
echo 'stage one here' | |
} | |
} | |
stage('Stage 2') { | |
steps { | |
echo 'stage 2 here' | |
} | |
} | |
stage('Stage 3') { | |
steps { | |
echo 'stage 3 here' | |
} | |
} | |
} | |
} | |
6) Save and run the build again | |
### Now we add my commands into each stage | |
I want to be able to pull down some code from GitHub (my website) everytime there's a commit. | |
This section covers the Github and Jenkins setup to do this: | |
1) Replace stage 1 with this: | |
stage('Clone the repo) { | |
steps { | |
echo 'clone the repo' | |
sh 'rm -fr html' | |
sh 'git clone https://github.com/dmccuk/html.git' | |
} | |
} | |
• Save and build | |
• Fix the typo!! | |
• Save and build | |
2) Replace stage 2 with this code: | |
stage('push repo to remote host') { | |
steps { | |
echo 'connect to remote host and pull down the latest version' | |
sh 'ssh -i ~/working.pem [email protected] sudo git -C /var/www/html pull' | |
} | |
} | |
• Save and build | |
3) Replace stage 3 with this code. | |
stage('Check website is up') { | |
steps { | |
echo 'Check website is up' | |
sh 'curl -Is 35.176.182.32 | head -n 1' | |
} | |
} | |
• Save and build | |
The complete code should look like this: | |
pipeline { | |
agent any | |
stages { | |
stage('Clone the repo') { | |
steps { | |
echo 'clone the repo' | |
sh 'rm -fr webapp1' | |
sh 'git clone https://github.com/dmccuk/webapp1.git' | |
} | |
} | |
stage('push repo to remote host') { | |
steps { | |
echo 'connect to remote host and pull down the latest version' | |
sh 'ssh -i ~/working.pem [email protected] sudo git -C /var/www/html pull' | |
} | |
} | |
stage('Check website is up') { | |
steps { | |
echo 'Check website is up' | |
sh 'curl -Is 35.176.182.32 | head -n 1' | |
} | |
} | |
} | |
} | |
### Now we setup the webhook | |
#### In Github (create the webhook) | |
• In the webapp1 repo - click settings | |
• click webhooks | |
• Enter the Jenkins + github-webhook - URL: http://52.47.204.70:8080/github-webhook/ | |
• click add webhook | |
• Click on the newly created webhook and scroll to the bottom. Do you see a green tick? | |
• You should be good to continue | |
#### In Jenkins (lets modify the pipeline) | |
• Open your pipeline and click configure | |
• Scroll down to the "Build Triggers" section and check the "GitHub hook trigger for GITScm polling" box | |
• In the definition dropdown, select "pipeline script from SCM" | |
• SCM: GIT | |
• Repository URL: https://github.com/dmccuk/webapp1.git | |
• Save the pipeline | |
#### In GitHub (create the Jenkins file): | |
In your GitHub project, create a Jenkinsfile with the correct name! and this contents: | |
pipeline { | |
agent any | |
stages { | |
stage('Clone the repo') { | |
steps { | |
echo 'clone the repo' | |
sh 'rm -fr webapp1' | |
sh 'git clone https://github.com/dmccuk/webapp1.git' | |
} | |
} | |
stage('push repo to remote host') { | |
steps { | |
echo 'connect to remote host and pull down the latest version' | |
sh 'ssh -i ~/working.pem [email protected] sudo git -C /var/www/html pull' | |
} | |
} | |
stage('Check website is up') { | |
steps { | |
echo 'Check website is up' | |
sh 'curl -Is 35.176.182.32 | head -n 1' | |
} | |
} | |
} | |
} | |
#### In Jenkins (back in your pipeline) | |
Attempt to build the pipeline and confirm everything works as expected. | |
### Now test the workflow on Git commit | |
In github, make changes to your index.html file and save/commit those updates. | |
Back in Jenkins, GitHub will let Jenkins know there's an update and kick off the pipeline. | |
Once the pipeline has completed, refresh your webpage and see the updates. | |
Got back and check it again. | |
If it's working, you now have your own Jenkins pipeline. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment