Using a declarative or scripted pipeline in DevOps
Summarize
Summary of Using a declarative or scripted pipeline in DevOps
This content explains how ServiceNow DevOps integrates with Jenkins pipelines using a Jenkinsfile to automate orchestration tasks. Each root-level stage in the Jenkinsfile maps automatically to a DevOps orchestration task, streamlining pipeline management and change control. This integration supports both declarative and scripted pipelines and enables seamless communication and artifact handling between Jenkins and ServiceNow DevOps.
Show less
Key Features
- Automatic Stage Mapping: Root-level stages in Jenkinsfiles are detected as individual orchestration tasks in DevOps, simplifying pipeline tracking.
- Job Notification Control: Setting the
Trackfield to True in DevOps enables Jenkins job notifications for all active Jenkins configurations. - DevOps Jenkinsfile Commands: Special commands like
snDevOpsChange,snDevOpsArtifact,snDevOpsPackage,snDevOpsGetChangeNumber,snDevOpsUpdateChangeInfo, andsnDevOpsSecurityResultallow pipelines to manage change requests, artifacts, packages, and security scans directly within Jenkins. - Configuration Name Parameter: Jenkins server configurations can be specified per step; if omitted, the default configuration applies. Incorrect names cause failures unless error ignoring is enabled.
- Jenkins Snippet Generator Utility: DevOps offers a snippet generator to create pipeline step templates for scripted pipelines, enabling easy insertion of DevOps tasks into Jenkinsfiles with customizable variables and error handling options.
- Support for Nested and Parallel Stages: While only root-level stages are directly mapped to DevOps steps, nested or parallel stages are processed as part of their parent root-level stage. Only one change request is created for the parent stage, and orchestration tasks are associated accordingly.
Practical Implications for ServiceNow Customers
- Enables automated and consistent orchestration task creation aligned with Jenkins pipeline stages, reducing manual effort and errors.
- Improves change control management by allowing detailed change request updates, closures, and status tracking directly from Jenkins pipelines.
- Facilitates artifact and package registration seamlessly within pipeline executions, enhancing traceability of deliverables.
- Supports integrating security scan results into DevOps Change Velocity for comprehensive pipeline security management.
- Allows flexible configuration and error handling to accommodate diverse Jenkins environments and pipeline complexities.
- Provides tools to generate pipeline code snippets, accelerating pipeline development and ensuring correct usage of DevOps-specific commands.
- Clarifies behavior when using nested or parallel stages, ensuring customers understand how change requests and tasks are generated and associated.
When you use a Jenkinsfile, steps are created, mapped, and associated to orchestration tasks automatically instead of manually.
Jenkinsfile is a text file that contains the definition of a Jenkins pipeline and is checked into source control.
Each root-level stage configured in the Jenkinsfile is discovered as a separate orchestration task in DevOps that is mapped to an individual step.
DevOps Jenkinsfile commands
- snDevOpsChange(ignoreErrors:{true/false},changeRequestDetails:{setCloseCode:{true/false},attributes:})
Where ignoreErrors specifies the setting to prevent job failure if there is an error (true/false)
Where changeRequestDetails specifies closure code and change request fields from within the pipeline
Enables change control for each root-level stage that is mapped to a DevOps step.
- snDevOpsArtifact
Registers artifacts when configuring Artifacts and packages.
- snDevOpsPackage
Creates a package for artifacts when configuring Artifacts and packages.
- snDevOpsGetChangeNumber
Retrieves the change request number in a Jenkins pipeline based on specific change details.
- snDevOpsUpdateChangeInfo
Updates the change request details associated with a Jenkins pipeline.
- snDevOpsSecurityResult
Configures security scans on any stage of the pipeline and the scan details are retrieved from the corresponding stage to DevOps Change Velocity.
You can specify the Jenkins server configuration in any of these steps by passing the configurationName attribute in your pipeline. If the configuration name is not specified in any step, the default configuration will be used in that step. Passing an incorrect configuration name will result in the step to fail unless the Ignore ServiceNow DevOps errors option is selected while configuring the Jenkins plugin.
Jenkins snippet generator for DevOps
- SnDevOpsArtifact
- SnDevOpsChange
- SnDevOpsPackage
- snDevOpsGetChangeNumber
- snDevOpsUpdateChangeInfo
- snDevOpsSecurityResult
snDevOpsChange changeCreationTimeOut: 3600, changeRequestDetails: '{ "attributes": { "short_description": "Test description", "priority": "1", "start_date": "2021-02-05 08:00:00", "end_date": "2022-04-05 08:00:00", "justification": "test justification", "description": "test description", "cab_required": true, "comments": "This update for work notes is from jenkins file", "work_notes": "test work notes", "assignment_group": "a715cd759f2002002920bde8132e7018" }, "setCloseCode": false, "autoCloseChange": true }', changeStepTimeOut: 18000, configurationName: 'Jenkins1', pollingInterval: 60Parallel and sub-stage support
When a stage (or set of parallel stages) is nested within a pipeline stage, these rules apply:
- Any action from the nested stage is processed as part of the parent root-level stage
- Only one change request is created (at the parent root level) even if multiple stages nested under the parent root-level stage trigger a change
- Orchestration tasks created are always associated with the parent root-level stage (not the nested stage)
Sub stage
In this sub-stage example, if a change request gets created from the sub stage (deploy PROD), the details of the parent root-level stage (deploy) are used in the change request, and orchestration tasks are also associated with the parent root-level stage (deploy).
stage("deploy") {
stages{
stage('deploy UAT') {
when{
branch 'dev'
}
stage('deploy PROD') {
when {
branch 'master'
}
steps{
snDevOpsChange()
}
}
}
Parallel stage
In this parallel stage example, if a change request is created from a sub stage (UAT test-1 and/or UAT static code test), only the first change request is created (using the details of the parent root-level stage, UAT test) regardless of whether both sub stages (UAT test-1 and UAT static code test) get triggered.
There is no indication of which parallel stage generated the change, and orchestration tasks are associated with the parent root-level stage (UAT test).
stage('UAT test') {
parallel {
stage('UAT test-1') {
steps {
snDevOpsChange()
// 'UAT test-1' tasks
}
post {
success {
// post success tasks. E.g.: junit '**/target/surefire-reports/*.xml'
}
}
}
stage('UAT static code test') {
steps {
snDevOpsChange()
// 'UAT static code test' tasks
}
}
}
}