Reference document: https://www.jenkins.io/doc/book/pipeline/jenkinsfile/
Jenkins Controller
- The Central Hub: The Jenkins Controller acts as the central management point for the Jenkins environment. It is responsible for scheduling jobs, orchestrating workflows, managing configurations, and maintaining the overall state of the CI/CD pipeline. It also provides the user interface for users to interact with Jenkins.
- Delegation of Work: Although the Controller can execute jobs, in a distributed setup, its primary role is to delegate work to Jenkins Agents. This approach allows the Controller to focus on management tasks without being overloaded by job execution.
Jenkins Agents
- Executors of Jobs: Jenkins Agents are responsible for executing the jobs assigned to them by the Controller. An agent can be a physical machine, a virtual machine, or a containerized environment that runs build tools and environments necessary for job execution.
- Scalability and Flexibility: By using multiple agents, Jenkins can scale beyond the limitations of a single server, handling multiple builds and tasks concurrently. Agents can be specialized for different types of jobs, depending on the tools and environments they provide, allowing for a flexible and optimized build process.
archiveArtifacts artifacts: '**/target/*.jar', fingerprint: true
archiveArtifacts
captures the files built matching the include pattern (*/target/.jar) and saves them to the Jenkins controller for later retrieval.artifacts
: Specifies the files to archive. You can use wildcards to include multiple files.fingerprint
: When set to true, Jenkins will fingerprint the artifacts, enabling tracking of which builds produced which artifacts. This is useful for traceability and for identifying which build an artifact came from.
junit
step
pipeline {
agent any
stages {
stage('Test') {
steps {
/* `make check` returns non-zero on test failures,
* using `true` to allow the Pipeline to continue nonetheless
*/
sh 'make check || true'
junit '**/target/*.xml'
}
}
}
}
Using an inline shell conditional (sh 'make check || true'
) ensures that the sh
step always sees a zero exit code, giving the junit
step the opportunity to capture and process the test reports.
junit captures and associates the JUnit XML files matching the inclusion pattern (**/target/*.xml).
Accessing the currentBuild.result
variable allows the Pipeline to determine if there were any test failures. In which case, the value would be UNSTABLE
.
The full list of environment variables accessible from within Jenkins Pipeline is documented at ${YOUR_JENKINS_URL}/pipeline-syntax/globals#env
https://automation.hinsonli.com/pipeline-syntax/globals
Reference global environment variable syntax: ${env.BUILD_ID}
Declarative Pipeline supports an environment directive, whereas users of Scripted Pipeline must use the withEnv
step.
pipeline {
agent any
environment {
CC = 'clang'
}
stages {
stage('Example') {
environment {
DEBUG_FLAGS = '-g'
}
steps {
sh 'printenv'
}
}
}
}
- An
environment
directive used in the top-levelpipeline
block will apply to all steps within the Pipeline. - An
environment
directive defined within astage
will only apply the given environment variables to steps within thestage
.
Setting environment variables dynamically
pipeline {
agent any
environment {
// Using returnStdout
CC = """${sh(
returnStdout: true,
script: 'echo "clang"'
)}"""
// Using returnStatus
EXIT_STATUS = """${sh(
returnStatus: true,
script: 'exit 1'
)}"""
}
stages {
stage('Example') {
environment {
DEBUG_FLAGS = '-g'
}
steps {
sh 'printenv'
}
}
}
}