Jenkins piepleine with dir correct syntax - jenkins

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

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

Jenkins error ImportError: No module named boto3

everyone.
There is Jenkins running on my ubunut host.
Trying to execute the creation of the "aws ECR" in Jenkins file.
but,Jenkins error ImportError: No module named boto3error
my jenkins file
node {
stage('Clone repository') {
checkout scm
}
stage('Build image') {
app = docker.build("xxxxxxxxx.dkr.ecr.ap-northeast-2.amazonaws.com/xxx")
}
stage('Create ECR') {
steps {
'''
sh 'pip3 install boto3 --upgrade'
sh 'python3 ecr.py'
'''
}
}
stage('Push image') {
sh 'rm ~/.dockercfg || true'
sh 'rm ~/.docker/config.json || true'
docker.withRegistry('https://xxxxxxxxxx.dkr.ecr.ap-northeast-2.amazonaws.com', 'ecr:ap-northeast-2:xxx-aws-ecr') {
app.push("xxx")
app.push("${env.BUILD_NUMBER}")
app.push("latest")
}
}
}
Thanks you!!

Jenkins "No such DSL method 'steps' found among steps"

Jenkins output error..
[Checks API] No suitable checks publisher found.
java.lang.NoSuchMethodError: No such DSL method 'steps' found among steps
My jenkinsfile.
node {
stage('Clone repository') {
checkout scm
}
stage('Build packer') {
steps {
dir('packer') {
sh 'git clone https://github.com/changhyuni/packer'
sh 'packer build ec2.json'
}
}
}
stage('Build image') {
app = docker.build("475667265637.dkr.ecr.ap-northeast-2.amazonaws.com/chang")
}
stage('Create ECR') {
sh 'pip3 install boto3 --upgrade'
sh 'python3 ecr.py'
}
stage('Push image') {
sh 'rm ~/.dockercfg || true'
sh 'rm ~/.docker/config.json || true'
docker.withRegistry('https://475667265637.dkr.ecr.ap-northeast-2.amazonaws.com', 'ecr:ap-northeast-2:chang-aws-ecr') {
app.push("chang")
app.push("${env.BUILD_NUMBER}")
app.push("latest")
}
}
}
steps is a directive from declarative syntax https://www.jenkins.io/doc/book/pipeline/syntax/#steps
Your example is scripted syntax https://www.jenkins.io/doc/book/pipeline/syntax/#scripted-pipeline

Reuse agent (docker container) in Jenkins between multiple stages

I have a pipeline with multiple stages, and I want to reuse a docker container between only "n" number of stages, rather than all of them:
pipeline {
agent none
stages {
stage('Install deps') {
agent {
docker { image 'node:10-alpine' }
}
steps {
sh 'npm install'
}
}
stage('Build, test, lint, etc') {
agent {
docker { image 'node:10-alpine' }
}
parallel {
stage('Build') {
agent {
docker { image 'node:10-alpine' }
}
// This fails because it runs in a new container, and the node_modules created during the first installation are gone at this point
// How do I reuse the same container created in the install dep step?
steps {
sh 'npm run build'
}
}
stage('Test') {
agent {
docker { image 'node:10-alpine' }
}
steps {
sh 'npm run test'
}
}
}
}
// Later on, there is a deployment stage which MUST deploy using a specific node,
// which is why "agent: none" is used in the first place
}
}
See reuseNode option for Jenkins Pipeline docker agent:
https://jenkins.io/doc/book/pipeline/syntax/#agent
pipeline {
agent any
stages {
stage('NPM install') {
agent {
docker {
/*
* Reuse the workspace on the agent defined at top-level of
* Pipeline, but run inside a container.
*/
reuseNode true
image 'node:12.16.1'
}
}
environment {
/*
* Change HOME, because default is usually root dir, and
* Jenkins user may not have write permissions in that dir.
*/
HOME = "${WORKSPACE}"
}
steps {
sh 'env | sort'
sh 'npm install'
}
}
}
}
You can use scripted pipelines, where you can put multiple stage steps inside a docker step, e.g.
node {
checkout scm
docker.image('node:10-alpine').inside {
stage('Build') {
sh 'npm run build'
}
stage('Test') {
sh 'npm run test'
}
}
}
(code untested)
For Declarative pipeline, one solution can be to use Dockerfile in the root of the project. For e.g.
Dockerfile
FROM node:10-alpine
// Further Instructions
Jenkinsfile
pipeline{
agent {
dockerfile true
}
stage('Build') {
steps{
sh 'npm run build'
}
}
stage('Test') {
steps{
sh 'npm run 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