JenkinsFile pipeline loop - jenkins

Does anyone have a pipeline{} example with a loop?
I'm trying to "condense" a long and repetitive JenkinsFile by using loops to run the same logic on multiple hosts.
... but ... I'm not getting loops right in my piepline{} JenkinsFile.
Specifically, I'm doing this ...
pipeline {
agent { label 'buildhost' }
stages {
stage("checks") {
parallel {
// ['mac-amd64', 'linux-arm', 'win-amd64', 'linux-x86', 'linux-armv8', 'linux-amd64' ].each {
['mac-amd64', 'linux-armv8', 'linux-amd64'].each {
host ->
stage("checks / ${host}") {
agent { label "buildhost-${host}" }
steps {
dir ('peterlavalle.sbt/') {
// combining these seemed to launch the SBT server which makes Jenkins hang
sh 'java -Dsbt.log.noformat=true -jar ./sbt-launch.jar clean'
sh 'java -Dsbt.log.noformat=true -jar ./sbt-launch.jar test'
sh 'java -Dsbt.log.noformat=true -jar ./sbt-launch.jar publish'
}
['cgc3.gradle/', 'kanobi.sbt/'].each {
gradle ->
dir (path) {
sh 'chmod +=rwx ./gradlew'
sh './gradlew --stacktrace --console=plain clean'
sh './gradlew --stacktrace --console=plain check'
sh './gradlew --stacktrace --console=plain publish'
}
}
dir ("coffeeskript.cgc/") {
sh 'chmod +=rwx ./gradlew'
sh './gradlew --stacktrace --console=plain check'
}
}
}
}
}
}
}
}
}
... and Jenkins says "only stages here!" or somesuch.
...
...
...
Running in Durability level: MAX_SURVIVABILITY
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 47: Expected a stage # line 47, column 11.
['mac-amd64', 'linux-armv8', 'linux-amd64'].each {
^
WorkflowScript: 44: Expected one of "steps", "stages", or "parallel" for stage "checks" # line 44, column 4.
stage("checks") {
^
...
...
...

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

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

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'
}
}
}
}

Jenkins piepleine with dir correct syntax

Hi guys I'm trying to write a jenkins pipeline with change dir, I can't get the syntax right, can anyone help me fix this?
the documentation is kinda unclear on where should I put this
pipeline {
agent {
docker {
image 'node'
args '-p 5000:5000'
}
}
stages {
dir("Backend") {
stage('build') {
steps {
sh 'pwd'
sh 'echo start build'
sh 'ls'
sh 'npm install'
}
}
}
dir("Backend") {
stage('Test') {
steps {
sh 'cd Backend'
sh 'echo start test'
sh 'ls'
sh 'npm run test'
}
}
}
}
}
dir is a step, therefore it must be contained within steps block:
stage('build') {
steps {
dir("Backend") {
sh 'pwd'
sh 'echo start build'
sh 'ls'
sh 'npm install'
}
}
}

Jenkins pipeline error - Unknown stage section "sh" and steps in a stage must be in a steps block

I had something almost identical to this and it worked fine and now I get the below errors. I have parallel, steps and stages all there.
Error:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 23: Unknown stage section "sh". Starting with version 0.5, steps in a stage must be in a steps block. # line 23, column 7.
stage('build api image') {
^
WorkflowScript: 26: Unknown stage section "sh". Starting with version 0.5, steps in a stage must be in a steps block. # line 26, column 7.
stage('build view image') {
^
WorkflowScript: 20: No "steps" or "parallel" to execute within stage "gradle test" # line 20, column 7.
stage('gradle test') {
^
WorkflowScript: 23: No "steps" or "parallel" to execute within stage "build api image" # line 23, column 7.
stage('build api image') {
^
WorkflowScript: 26: No "steps" or "parallel" to execute within stage "build view image" # line 26, column 7.
stage('build view image') {
pipeline {
agent any
stages {
stage('run test cases in modules') {
steps {
parallel(
"Gradle clean": {
sh "./gradlew clean"
},
"api-service-tests": {
sh "./gradlew api:test"
},
"cache-api-tests": {
sh "./gradlew view:test"
}
)
}
}
stage('gradle test') {
// sh "./gradlew --no-daemon test"
}
stage('build api image') {
sh "oc start-build cms-api --from-dir=api/docker --follow"
}
stage('build view image') {
sh "oc start-build --from-dir=view --follow"
}
}
}
You are missing the steps directive inside of your other stage directives.
stage('gradle test') {
// sh "./gradlew --no-daemon test"
}
Should be something more like:
stage('gradle test') {
steps {
sh "./gradlew --no-daemon test"
}
}

JenkinsFile for Sonar Quality Gates doesn't work

I have read the documentation of Jenkins and Sonar to write and configure my pipeline and everything works except the Sonar Quality Gate which remains for the response from the Sonar without success. This is my code :
pipeline {
agent master
tools {
jdk 'JAVA'
maven 'mvn_3_5_2'
}
stages {
stage('test Java & Maven installation') {
steps {
sh 'java -version'
sh 'which java'
sh 'mvn -version'
sh 'which mvn'
}
}
stage('Clean stage') {
steps {
sh 'mvn -f project/pom.xml clean'
}
}
stage("build & SonarQube analysis") {
node {
withSonarQubeEnv('Sonar') {
sh 'mvn -f project/pom.xml org.sonarsource.scanner.maven:sonar-maven-plugin:3.2:sonar'
}
}
}
stage("Quality Gate"){
timeout(time: 2, unit: 'MINUTES') {
def qg = waitForQualityGate()
if (qg.status != 'OK') {
error "Pipeline aborted due to quality gate failure: ${qg.status}"
}
}
}
stage('Test stage') {
steps {
sh 'mvn -f project/pom.xml test'
}
}
stage('Build stage') {
steps {
sh 'mvn -f project/pom.xml install'
}
}
stage('Deploy stage') {
steps {
echo 'Deployment...'
// sh 'mvn -f project/pom.xml appengine:deploy -Dapp.deploy.project=acn-shelf-check -Dapp.deploy.version=v1'
}
}
}
}
In the stage "Quality Gate" I never received the result. I have configured the webhook on the sonar with correct URL of Jenkins.
Do I miss any step?

Resources