Jenkins

Jenkinsfile Checkout SCM

The Source Code Management or SCM refers to the version control system (VCS) which is used to manage a project’s source code. Jenkins supports multiple SCM systems including Git, Subversion, Mercurial, and many others.

During project configuration, we can specify the SCM repository where the source code for the project is stored. Jenkins then periodically checks the repository for changes. If it detects any new commits or updates, it automatically trigger a project build.

In a Jenkinsfile, we can define the checkout SCM step to retrieve the source code for the pipeline from the specified SCM repository. This step is usually the first step in a Jenkinsfile, ensuring that the source code is available for the rest of the pipeline.

Jenkins SCM Step Plugin

The Jenkins SCM Step plugin allows the Jenkins pipelines to use the standard Jenkins SCM plugins to check out the source code from an SCM source. This provides extensible checkout options, specifying the various SCM sources, specific branches, and more.

To use this plugin, you need to install it on your Jenkins controller. Open the Jenkins dashboard and select “Manage Jenkins” from the left-hand menu. Navigate to the Manage Plugins page -> Available Plugins and search for “SCM Step”.

Using the Jenkins SCM Step

Once we have the SCM plugin installed, we can use the checkout SCM plugin as shown in the following pipeline example:

pipeline {
    agent { label 'my-agent' }
    stages {
        stage('Build') {
            steps {
                checkout scm
                sh make
            }
        }
    }

}

In the previous example, we use the checkout SCM step to check out the source and run the build using the make command.

You can specify the various SCM sources such as AWS Code Pipeline, Bazaar SCM, BitKeeper, Mercuria, Open Shift Images, filesystem, and more.

The following image shows the accepted SCM sources for the checkout step:

Source: Jenkins SCM Documentation.

The following example shows how you can specify the SCM source and other options such as the credentials:

checkout scm: [$class: 'MercurialSCM', source: 'ssh://[email protected]/user/repo', clean: true, credentialsId: '1234-5678-abcd'], poll: false

In the previous example, the checkout SCM block specifies SCM from which the checkout step gets the source code.

We also define the configuration for the target SCM including the URL and credentials for the server.

In this case, the [$class: ‘MercurialSCM’] is defined using the Mercurial SCM to connect to the repository.

Next, we use the source option to specify the URL of the repository which is a Mercurial repository that is hosted on Bitbucket.

We also define the clean option which tells Jenkins to perform a “clean” checkout. This allows Jenkins to discard any local changes and check out a fresh copy of the repository.

The credentialsId option specifies a set of credentials IDs to authenticate with the target repository.

Finally, the option poll is set to false. This prevents Jenkins from polling the repository for changes. Hence, Jenkins will not automatically trigger a build if new commits or updates in the repository are detected. We have to manually trigger a build or use some other means (such as a webhook) to trigger a build.

Conclusion

We explored the fundamentals of using the SCM checkout step in Jenkins to checkout the source code from a given repository.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list