Pipelines Primer 2

(this post if part of the material I cover in my devops course)

Let's add some scm (source control management) abilities to our pipelines.

SCM

  • We can add one more stage to the pipeline created in the previous post that will be used to clone a git repository
  • I'll be using checkout: Check out from version control (that has more options than git:Git)
  • Using the Pipeline Syntax tool (click on it under the pipeline edit window):
    • Use https and not git protocol for a public repository (we'll cover authentication to github in a later post)
    • Make sure you check the correct branch (it was main for me, not master)
  • Here is the added code (created using the Pipeline Syntax as before):
1stage('build'){
2            steps{
3                checkout scmGit(branches: [[name: '*/main']], extensions: [], userRemoteConfigs: [[url: 'https://github.com/YuvalShaul/files.git']])
4            }
5        }
  • Looking at the build output, we can discover the directory where the remote repository was copied to:
1Cloning repository https://github.com/YuvalShaul/employees.git
2> git init /var/jenkins_home/workspace/pipe2 # timeout=10
  • You can exec into the jenkins container and see the code there.

archiveArtifacts

  • Jenkins can store artifacts built "inside" the build itself
  • I have changed the echo 'hello world!' (which is not a shell script but a pipelines command of its own) to a shell command that redirects the output into a text file.
    Then, added a post step to archive that file:
 1stages {
 2       ...
 3       stage('Hello') {
 4           steps {
 5               sh 'echo \'hello world!\' > hello.txt'
 6               sh 'echo $((PAR1 + PAR2))'
 7           }
 8       }
 9       stage('build'){
10           steps{
11               checkout scmGit(branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[url: 'https://github.com/YuvalShaul/employees.git']])
12           }
13       }
14       stage('post'){
15           steps{
16               archiveArtifacts artifacts: 'hello.txt', followSymlinks: false 
17           }
18       }
19   }
  • The result can be seen if you view the build:

Archived artifact in build