How to use artifactResolver in Jenkinsfile pipeline steps? - jenkins

I am trying to use artifactResolver in a Jenkins pipeline step. However, it is failing.
Trying the following config:
pipeline {
agent any
stages {
stage('Download artifact') {
steps {
artifactResolver {
artifacts {
artifact {
groupId('ch.qos.logback')
artifactId('logback-classic')
version('1.1.1')
classifier('sources')
}
}
}
}
}
}
}
However, I get the following error when I build on Jeninks:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 7: Missing required parameter: "artifacts" # line 7, column 17.
artifactResolver {
^

Related

withCredentials() block jenkinsfile syntax error

The withCredentials block syntax is confusing me. I have tried placing it outside stages{}, outside stage('saving stross...'), and now within the stage('saving stross...'), but I am unable to fix this syntax error in the jenkins pipeline.
jenkinsfile:
pipeline {
agent {
node {
label 'node1'
}
}
stages {
stage('saving stross token and printing') {
withCredentials([string(credentialsId: 'devadrita-stross', variable: 'deva-stross')]) {
steps {
script {
bat """
python -u C://Users//Administrator//Desktop//stross//stross-script.py, token = "${deva-stross}"
"""
}
}
}
}
}
}
error-
Started by user admin
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 8: Unknown stage section "withCredentials". Starting with version 0.5, steps in a stage must be in a ‘steps’ block. # line 8, column 9.
stage('saving stross token and printing') {
^
WorkflowScript: 8: Expected one of "steps", "stages", or "parallel" for stage "saving stross token and printing" # line 8, column 9.
stage('saving stross token and printing') {
^

Jenkins helm chart - job DSL issue

I have created a PipelineJob in Jenkins UI, which calls 2 other Jenkins jobs:
pipeline {
agent any
stages {
stage("Trigger disable script approval") {
steps {
script{
build job: 'Tools/Disable_Script_Approval'
}
}
}
stage("Trigger Jobs loading into Jenkins") {
steps {
script{
build job: 'Tools/Seed_Job_Executor'
}
}
}
}
}
Then, I used xml-job-to-job-dsl plugin in order to get the job syntax in DSL:
pipelineJob("testLior") {
description()
keepDependencies(false)
definition {
cpsScm {
"""pipeline {
agent any
stages {
stage("Trigger disable script approval") {
steps {
script{
build job: 'Tools/Disable_Script_Approval'
}
}
}
stage("Trigger Jobs loading into Jenkins") {
steps {
script{
build job: 'Tools/Seed_Job_Executor'
}
}
}
}
}""" }
}
disabled(false)
configure {
it / 'properties' / 'com.sonyericsson.rebuild.RebuildSettings' {
'autoRebuild'('false')
'rebuildDisabled'('false')
}
}
}
I took the above code, and tried to use in JCasC configuration (we are running Jenkins with helm chart on top of EKS), and created this values file:
controller:
JCasC:
configScripts:
casc-jobs: |
jobs:
- script: >
pipelineJob('DSL_Seed_Job') {
definition {
cpsScm {
'''pipeline {
agent any
stages {
stage('Trigger disable script approval') {
steps {
script{
build job: 'Tools/Disable_Script_Approval'
}
}
}
stage('Trigger Jobs loading into Jenkins') {
steps {
script{
build job: 'Tools/Seed_Job_Executor'
}
}
}
}
}'''
}
}
}
...
...
So once I'm running helm upgrade I see that Jenkins pod fails to read the JCasC jobs configuration, and this error message appears:
2021-10-21 11:04:37.178+0000 [id=22] SEVERE hudson.util.BootFailure#publish: Failed to initialize Jenkins
while scanning a simple key
in /var/jenkins_home/casc_configs/casc-jobs.yaml, line 3, column 3:
pipelineJob('DSL_Seed_Job') {
^
could not find expected ':'
in /var/jenkins_home/casc_configs/casc-jobs.yaml, line 12, column 38:
... build job: 'Tools/Disable_Script_Approval'
What can be the cause for this error? I got the DSL syntax from the xml-job-to-dsl-job Jenkins plugin so I don't understand what am I missing here.
Thanks in advance,
Lior
You probably figured this out by now but it looks to me like a yaml indentation issue, I believe the block starting with "pipelineJob" should be indented like so:
jobs:
- script: >
pipelineJob('DSL_Seed_Job') {
...
}

Jenkins: Unknown stage section "matrix" in declarative pipeline

I am new to jenkins and I try to build a declarative pipeline according to the tutorial.
On the page: https://jenkins.io/doc/book/pipeline/syntax/#matrix-cell-directives
there is an example on how to build a pipeline with a matrix which I tried.
Unfortunately I get the following error:
WorkflowScript: 32: Unknown stage section "matrix". Starting with version 0.5, steps in a stage must be in a ‘steps’ block. # line 32, column 5.
stage ('Deploy NB') {
^
WorkflowScript: 32: Expected one of "steps", "stages", or "parallel" for stage "Deploy NB" # line 32, column 5.
stage ('Deploy NB') {
My pipeline in the jenkinsfile looks like this:
The functions from the lib are surely without any problems because they are used in several other jenkinsfiles which run without problems.
pipeline {
agent {
node {
label ""
// Location of the output files
customWorkspace "/home/wf/builds/${env.JOB_NAME}"
}
}
environment {
// mail addresses that gets notifications about failures, success etc., - comma delimited
MAIL_NOTIFY = "mustbeanonymous"
// Server admin (not necessary for wildfly)
ADMIN_USER = " "
ADMIN_PWD = " "
// home directory
HOME_DIR = "/home/wf"
// Product name
PRODUCT_NAME = "MYPRD"
}
options {
disableConcurrentBuilds()
durabilityHint("PERFORMANCE_OPTIMIZED")
}
stages {
stage ('Deploy NB') {
matrix {
axes {
axis {
name 'ENVIRONMENT'
values 'NB', 'TEST1'
}
axis {
name 'DATABASE'
values 'ORA', 'ORA_INIT', 'DB2', 'DB2_INIT'
}
}
environment {
// Server scripts installation path
SERVER_PATH = "${HOME_DIR}/WildFly16_${PRODUCT_NAME}_${ENVIRONMENT}_${DATABASE}"
// EAR to deploy on server
DEPLOY_EAR = "${PRODUCT_NAME}_WF_${DATABASE}.ear"
}
stages {
/* BUILD */
stage('Init tools') {
steps {
script {
def lib = load "${workspace}/build/Jenkinsfile.lib"
lib.initTools()
}
}
}
stage('Copy Deployment') {
steps {
script {
def lib = load "${workspace}/build/Jenkinsfile.lib"
lib.copyDeployment()
}
}
}
/* DEPLOY */
stage('Install EAR') {
steps {
script {
def lib = load "${workspace}/build/Jenkinsfile.lib"
lib.installEARDeploy()
}
}
}
}
}
}
}
/* POST PROCESSING */
post {
success {
script {
def lib = load "${workspace}/build/Jenkinsfile.lib"
lib.onSuccess()
}
}
failure {
script {
def lib = load "${workspace}/build/Jenkinsfile.lib"
lib.onFailure()
}
}
unstable {
script {
def lib = load "${workspace}/build/Jenkinsfile.lib"
lib.onUnstable()
}
}
always {
script {
def lib = load "${workspace}/build/Jenkinsfile.lib"
lib.onAlways()
}
}
}
}
What I try to achieve is that the pipeline runs for every ENVIRONMENT and DATABASE (each cell) and executes the stages. But where did I make a mistake?
I use Jenkins: 2.198
Update: The solution was to upgrade the plugin to a version above 1.5.0. See accepted answer for more information.
What version of Declarative Pipeline do you use ?
Matrix section was only added in version 1.5.0 of Declarative Pipeline plugin
See https://github.com/jenkinsci/pipeline-model-definition-plugin/releases
To verify the version, search for pipeline-model-definition on jenkins.yourcompany.com/pluginManager/api/xml?depth=1

Step expected in Jenkins Groovy script

I am having below groovy script for my Jenkins pipeline. But when running its giving error as step expected where my script already having step. Can anyone suggest what's wrong here..
Script file
pipeline {
agent any
stages {
stage('Workspace Preparation') {
steps {
sh """
rm -rf ${workspace}/*
"""
}
}
stage('Get Deployment files') {
steps {
dir("${workspace}/deployfiles") {
if("${params.componentType}"=="A") {
echo "A component deployment"
checkout(## necessary step)
}
else if ("${params.componentType}"=="B") {
echo "B component deployment"
checkout(## necessary step)
}
else {
echo "Invalid"
}
}
}
}
}
}
Getting error as
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 19: Expected a step # line 14, column 6.
if("${params.componentType}"=="A") {
^
enter code here
enter code here
You are missing a script-block.
(Source)
Such a block gives you acces to execute groovy code (for, if-else etc. etc.)
stage('Check') {
steps {
script { // Allows to execute groovy code
dir (...) {
if (...)
}
}
}
See also: How to fix Pipeline-Script “Expected a step” error

How to add try catch block in Jenkins declarative syntax.?

I am trying to add try catch block in Jenkins declarative pipeline but I end up with the following error, I read the document on adding try catch block for scripted pipeline syntax of Jenkins(https://jenkins.io/doc/book/pipeline/syntax/#post-conditions) but I didn't get anything on declarative syntax.
pipeline {
agent any
stages {
try {
stage('Checkout') {
steps {
script {
if (ci_branches.contains(env.BRANCH_NAME)) {
// Pull the code from bitbucket repository
checkout scm
}
}
}
}
}
catch(all) {
currentBuild.result='FAILURE'
}
}
}
Jenkins ci build result
[Bitbucket] Build result notified
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 36: Expected a stage # line 36, column 13.
try {
^
WorkflowScript: 35: No stages specified # line 35, column 9.
stages {
^
Try/catch should be inside a script when using declarative pipeline syntax. Test the following:
pipeline {
agent any
stages {
stage('Checkout') {
steps {
script {
try {
if (ci_branches.contains(env.BRANCH_NAME)) {
// Pull the code from bitbucket repository
checkout scm
}
}
catch(all) {
currentBuild.result='FAILURE'
}
}
}
}
}
}

Resources