Error WorkflowScript: 8: Expected one of "steps", "stages", or "parallel" for stage "check out scm" when need to run a pipeline - jenkins

I write this pipeline for run that after push on the develop branch or master branch and doing some workers related to that branch.
I want to check the repository and run pipeline after push on the any branches
pipeline {
triggers {
pollSCM('*/1 * * * * ')
}
agent any
stages {
stage('Check out scm') {
when {
branch 'master'
}
checkout scm
}
stage('Install npm') {
steps {
sh 'npm install'
}
}
stage('Build Project Develop') {
when {
branch 'develop'
}
steps {
sh 'ng build --prod '
}
}
stage('Build Project Realase')
{
when {
branch 'master'
}
steps {
sh 'ng build --prod '
}
}
stage('Move to Var') {
steps {
sh 'chown -R root:jenkins /var/lib/jenkins/workspace/Angular-CI-CD--Test_master/dist/ang-CICD/. && /var/www/html'
}
}
}
}
But it shows me this error:
Branch indexing
Connecting to https://api.github.com using kiadr9372/****** (GitHub Access Token)
Obtained Jenkinsfile from d57840a79c46a88969381cc978f378c7d6804cec
Running in Durability level: MAX_SURVIVABILITY
GitHub has been notified of this commit’s build result
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 8: Unknown stage section "checkout". Starting with version 0.5, steps in a stage must be in a ‘steps’ block. # line 8, column 9.
stage('check out scm') {
^
WorkflowScript: 8: Expected one of "steps", "stages", or "parallel" for stage "check out scm" # line 8, column 9.
stage('check out scm') {
^
What is the problem?

Solution
You need to place checkout scm within a step closure. You also have an additional closing bracket.
pipeline {
triggers {
pollSCM('*/1 * * * * ')
}
agent any
stages {
stage('check out scm') {
when {
branch 'master'
}
steps {
checkout scm
}
}
stage('Install npm') {
steps {
sh 'npm install'
}
}
stage('Build Project Develop') {
when {
branch 'develop'
}
steps {
sh 'ng build --prod '
}
}
stage('Build Project Realase') {
when {
branch 'master'
}
steps {
sh 'ng build --prod '
}
}
stage('Move to Var') {
steps {
sh 'chown -R root:jenkins /var/lib/jenkins/workspace/Angular-CI-CD--Test_master/dist/ang-CICD/. && /var/www/html'
}
}
}
}

Related

How to add a jmeter build step to jenkins pipeline when using jmeter maven plugin

I have two stages on jenkins pipeline, and it is expecting to add steps in execute jmeter stage
Could someone help to resolve this....
I got below error:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 339: Expected one of "steps", "stages", or "parallel" for stage "Execute Jmeter" # line 339, column 9.
stage('Execute Jmeter') {
Below is the code snippet of jenkins pipeline:
pipeline {
agent {
label 'qatest'
}
tools {
maven 'Maven'
jdk 'JDK8'
}
environment {
VIRTUOSO_URL = 'qa.myapp.com'
}
stages {
stage('BUILD') {
steps {
sh 'mvn clean verify'
}
}
stage('Execute Jmeter') {
post{
always{
dir("scenarioLoadTests/target/jmeter/results/"){
sh 'pwd'
sh 'mv *myapp_UserLoginAndLogout.csv UserLoginAndLogout.csv '
sh 'mv *myapp_myappPortfolioScenario.csv myappPortfolioScenario.csv '
sh 'mv *myapp_myappDesign.csv myappDesign.csv '
perfReport '*.csv'
}
}
}
}
}
}
Shouldn't you use script blocks like:
pipeline {
agent {
label 'qatest'
}
tools {
maven 'Maven'
jdk 'JDK8'
}
environment {
VIRTUOSO_URL = 'qa.myapp.com'
}
stages {
stage('BUILD') {
steps {
script { // <----------------- here
sh 'mvn clean verify'
}
}
}
stage('Execute Jmeter') {
post {
always {
dir('scenarioLoadTests/target/jmeter/results/') {
script { <-------------------------------------- and here
sh 'pwd'
sh 'mv *myapp_UserLoginAndLogout.csv UserLoginAndLogout.csv '
sh 'mv *myapp_myappPortfolioScenario.csv myappPortfolioScenario.csv '
sh 'mv *myapp_myappDesign.csv myappDesign.csv '
perfReport '*.csv'
}
}
}
}
}
}
}
More information:
Pipeline Maven Integration
Running a JMeter Test via Jenkins Pipeline - A Tutorial

How make dynamic change stage name in Jenkinsfile Declarative pipeline?

I have Jenkinsfile (Scripted Pipeline)
def template1 = "spread_sshkeys"
node {
// Clean before build
stage('Checkout') {
deleteDir()
checkout scm
sh "git submodule foreach --recursive git pull origin master";
}
stage("import template ${template1}") {
script{
sh "ls -las; cd jenkins-ci-examples; ls -las";
jenkins_ci_examples.sub_module = load "jenkins-ci-examples/${template1}"
}
}
stage("run template ${template1}") {
sh "echo ${jenkins_ci_examples.sub_module}";
}
}
after want to Converting to Declarative
def template1 = "spread_sshkeys"
pipeline {
agent any
stages {
stage ("Checkout") {
steps {
deleteDir()
checkout scm
sh "git submodule foreach --recursive git pull origin master"
}
}
stage("import template ${template1}") {
steps {
script {
sh "ls -las; cd jenkins-ci-examples; ls -las";
jenkins_ci_examples.sub_module = load "jenkins-ci-examples/${template1}"
}
}
}
stage("run template ${template1}") {
steps {
sh "echo ${jenkins_ci_examples.sub_module}";
}
}
}
}
After start Jenkins Job stop and return Error
WorkflowScript: 22: Expected string literal # line 22, column 19.
stage("import template ${template1}") {
^
WorkflowScript: 30: Expected string literal # line 30, column 19.
stage("run template ${template1}") {
^
Try to use
stage('run template ${template1}')
and else
stage('run template '+template1)
returned error too.
How solve this problem?
You can create dynamic stages using sequential stages as below:
def template1 ="spread_sshkeys"
pipeline {
agent any
stages {
stage('Dynamic Stages') {
steps {
script {
stage("import template ${template1}"){
println("${env.STAGE_NAME}")
}
stage("run template ${template1}"){
println("${env.STAGE_NAME}")
}
}
}
}
}
}

Multiple parallel stages, jenkins checks out source on every stage

I'm not sure this is default behavior of Jenkins.
I have three parallel stages each assumes files produced in previous stage is present in the directory.
I run yarn install in stage 1 and yarn build in stage 2 and in stage 3 I have yarn package.
Each stage tries to checks out source and complains, for example, stage 2 complains that node_modules is not present, when I inspected I found after stage 1 workspace was reset.
Is there way to retain the directories/files in workspace after the stage is complete?
Any help is greatly appreciated.
EDIT 1
pipeline {
agent none
options {
skipDefaultCheckout true
}
environment {
BRANCH_NAME = 'mater'
}
stages {
stage ('Checkout source') {
agent any
steps {
checkout([
$class: 'GitSCM',
branches: [[name: "${env.BRANCH_NAME}"]],
doGenerateSubmoduleConfigurations: false,
extensions: [],
submoduleCfg: [],
userRemoteConfigs: [[
credentialsId: '5db9bedc-fa88-4f64-9e6a-3e9a9d5c999f',
url: 'git#gitserver.io:acme/acme-repo.git'
]]
])
}
}
stage ('Test') {
parallel {
stage ('[Test] Mac') {
agent {
node {
label 'iMac'
customWorkspace "/Users/acme/workspace/acme-repo/nightly/${env.BRANCH_NAME}"
}
}
steps {
sh 'npm install yarn -g'
sh 'yarn install --network-timeout 1000000'
sh 'yarn build'
sh 'yarn test -u'
}
}
stage ('[Test] Linux') {
agent {
node {
label 'master'
customWorkspace "/var/lib/jenkins/acme/acme-repo/nightly/${env.BRANCH_NAME}"
}
}
steps {
sh 'sudo npm install yarn -g'
sh 'yarn install --network-timeout 1000000'
sh 'yarn build'
sh 'yarn test -u'
}
}
stage ('[Test] Windows') {
agent {
node {
label 'win'
customWorkspace "E:\\jenkins_agent\\acme\\nightly\\${env.BRANCH_NAME}"
}
}
steps {
bat 'npm install yarn -g'
bat 'yarn install --network-timeout 1000000'
bat 'yarn build'
bat 'yarn test -u'
}
}
}
}
stage ('Package') {
parallel {
stage ('Mac') {
agent {
node {
label 'iMac'
customWorkspace "/Users/acme/workspace/acme-repo/nightly/${env.BRANCH_NAME}"
}
}
steps {
sh 'yarn package-mac'
}
}
stage ('Linux') {
agent {
node {
label 'master'
customWorkspace "/var/lib/jenkins/acme/acme-repo/nightly/${env.BRANCH_NAME}"
}
}
steps {
sh './node_modules/.bin/yarn package-linux'
}
}
stage ('Windows') {
agent {
node {
label 'win'
customWorkspace "E:\\jenkins_agent\\acme\\nightly\\${env.BRANCH_NAME}"
}
}
steps {
bat 'yarn package-win'
}
}
}
}
stage ('Publish') {
parallel {
stage ('[Publish] Mac') {
agent {
node {
label 'iMac'
customWorkspace "/Users/acme/workspace/acme-repo/nightly/${env.BRANCH_NAME}"
}
}
steps {
sh 'yarn publish-mac'
}
}
stage ('[Publish] Linux') {
agent {
node {
label 'master'
customWorkspace "/var/lib/jenkins/acme/acme-repo/nightly/${env.BRANCH_NAME}"
}
}
steps {
sh './node_modules/.bin/yarn publish-linux'
}
}
stage ('[Publish] Windows') {
agent {
node {
label 'win'
customWorkspace "E:\\jenkins_agent\\acme\\nightly\\${env.BRANCH_NAME}"
}
}
steps {
bat 'yarn publish-win'
}
}
}
}
stage ('Tag & Push') {
agent {
node {
label 'master'
customWorkspace "/var/lib/jenkins/acme/acme-repo/nightly/${env.BRANCH_NAME}"
}
}
steps {
sh "yarn version --patch"
sh 'git push git#gitserver.io:acme/acme-repo.git HEAD:$BRANCH_NAME'
}
}
}
}
Just to give more details - there are
checkout source
test (parallel)
package (parallel)
publish (parallel)
Each of these stages are defined for different environment. The problem is when running package stage, evidently, the workspace is cleaned up so there is nothing to run package
As it turns out, I need to use -
options {
// This is key setting that enables one checkout across different stages
skipDefaultCheckout true
}
to skip default checkout.

Jenkinsfile - use Global properties in post

Here is my Jenkinsfile for multi-branch pipeline project:
pipeline {
agent any
stages {
stage('MASTER build') {
when {
branch 'master'
}
steps {
sh 'mvn -P x clean deploy'
}
}
stage('BRANCH build') {
when {
not { branch 'master' }
}
steps {
sh 'mvn -P x clean package'
}
}
}
post {
failure {
emailext "${EMAIL_TEMPLATE}"
}
}
}
When I build my project in Jenkis following error occurs:
WorkflowScript: 26: Step does not take a single required parameter - use named parameters instead # line 26, column 13.
emailext "${EMAIL_TEMPLATE}"
Why I cant use EMAIL_TEMPLATE global variable containing all emailext definition?

Jenkins declarative pipeline can't find some scripts

I have written a Jenkins pipeline where the relevant parts looks as follows:
pipeline {
agent {
dockerfile true
}
triggers {
pollSCM('H 1 * * 1-5')
}
options {
buildDiscarder(logRotator(artifactNumToKeepStr: "${NUMBER_OF_ARTIFACTS_TO_KEEP}"))
disableConcurrentBuilds()
timeout(time: 60, unit: 'MINUTES')
timestamps()
}
stages {
stage('Metadata') {
steps {
script {
sh 'java -version'
}
script {
sh './mvnw -v'
}
}
}
stage('Build') {
steps {
script {
sh './mvnw --batch-mode clean install'
}
}
}
stage('Archive artifacts (develop/master)') {
when {
anyOf {
branch 'master'
branch 'develop'
}
}
steps {
script {
sh './package.sh'
}
archive '**/target/*.jar'
archiveArtifacts artifacts: '*.deb'
}
}
}
post {
always {
deleteDir()
}
failure {
sendNotifications currentBuild.result
}
unstable {
sendNotifications currentBuild.result
}
}
}
And my Dockerfile:
FROM alpine
RUN apk add --no-cache dpkg openjdk8
All scripts run fine, except package.sh where I get the following log in the output:
07:47:25 [chx-sync_-sync_master-A2F53LY4I2X54TLDEU2Z2PXI423NI6FODHQDS7CRIKCCNDF5UGOA] Running shell script
07:47:25 + ./package.sh
07:47:25 /home/jenkins/jenkins/workspace/chx-sync_-sync_master-A2F53LY4I2X54TLDEU2Z2PXI423NI6FODHQDS7CRIKCCNDF5UGOA#tmp/durable-dbcb4143/script.sh: line 1: ./package.sh: not found
I can't figure out why all scripts except this one would work. They are all located in the root of the project in Git. Is there some command in my pipeline that would change the working directory, or what is going on here?
EDIT:
I'm guessing the shebang in package.sh might be relevant? It is #!/bin/bash.
I got the answer from this answer on SO. I simply changed the using #!/bin/sh and it is working.

Resources