Skip to main content
In this lesson, you’ll learn how to obtain a Jenkins pipeline script directly from a Source Control Management (SCM) system like GitHub. Traditionally, the pipeline script was defined directly in the Jenkins UI. For example, a typical script defined in the Jenkins UI looked like this:
Defining your pipeline script in SCM allows you to maintain version control over your build configuration, making it easier to track changes over time.

Creating a Jenkinsfile

Instead of embedding the pipeline script in Jenkins, you can add it to your GitHub (or Git repository) to keep it version-controlled. In your repository, create a new file (typically named Jenkinsfile) at the root level and paste the pipeline script. Note that Jenkins automatically looks for a file named “Jenkinsfile” unless you specify a different script path. Below is an updated example of a Jenkinsfile saved in the repository. Notice that the Maven tool is now referenced as “M3”:

Pipeline Script from SCM Configuration

When you configure your Jenkins pipeline job with the Pipeline Script from SCM option, Jenkins automatically checks out your repository. This means you can remove or comment out explicit Git commands in your pipeline. For example, the modified Jenkinsfile using SCM looks like this:
To set up your pipeline to retrieve the script from SCM:
  1. Select the SCM Option: Choose “Pipeline Script from SCM” as the definition.
  2. Choose Your SCM: Select Git as the SCM provider.
  3. Provide Details: Enter the repository URL, specify the branch (e.g., ‘main’), and if needed, set the script path if your Jenkinsfile resides in a subdirectory.
An example configuration using a GitLab repository might include the following script:
If you receive an error message indicating that the repository URL is not valid, double-check that the URL is correct and that no credentials are needed for a public repository.
During configuration, you might encounter an error message similar to the one shown below:
The image shows a Jenkins configuration screen for a pipeline project, where a Git repository URL is being entered, but there's an error message indicating that a valid Git repository is required.
Since the repository is public, credentials are not required. In the advanced settings, ensure the correct branch name (such as ‘main’) is specified. Jenkins will search for the Jenkinsfile in the repository root by default unless a different path is defined.

Understanding the Build Process

After the configuration is complete, Jenkins checks out your repository and executes the defined stages. The build log will include details like fetching the repository, commit messages, and more. An excerpt from a typical build log might look like this:
After the checkout, the pipeline moves on to subsequent stages. For instance, the output of the “Echo Version” stage may look like this:

Exploring the Jenkins Workspace

After a successful build, you can review the workspace files via the Jenkins UI. The workspace includes both the repository files and the build outputs (such as the generated Maven target folder, test reports, and packaged application files). For example, here is an interface displaying the workspace files for the project:
The image shows a Jenkins workspace interface displaying a list of directories and files related to a project build, including JAR files and generated sources.
You can also inspect detailed build stages in the pipeline interface:
The image shows a Jenkins build pipeline interface for "hello-world-pipeline," displaying the successful completion of build #7 with various stages like "Checkout SCM," "Tool Install," and "Unit Test." It includes details about changes, build duration, and repository information.

Accessing the Workspace via Terminal

You can also access the Jenkins master to review the workspace directly. For example, using the terminal:
Then, to examine the “hello-world-pipeline” workspace:
This workspace houses all repository files along with build outputs such as Maven targets and test reports.

Conclusion

By following this lesson, you now understand how to retrieve a pipeline script from SCM, version control your build configurations, and streamline the pipeline setup in Jenkins. This approach offers better maintainability and traceability for your continuous integration workflows. Thank you for exploring how to integrate SCM with Jenkins pipeline scripts. Happy building!

Watch Video