I want to create environment variable as PREVIOUS_BUILD_NUMBER from BUILD_NUMBER. But I cant get expected value.
It turns BUILD_NUMBER.İf my build number 5 I want to get 2 from PREVIOUS_BUILD_NUMBER.
stage('Delete file') {
steps {
script {
dir('/opt/.../...') {
sh "echo 'PREVIOUS_BUILD_NUMBER=${{$BUILD_NUMBER}-3}'"
sh "rm -rf apk-${PREVIOUS_BUILD_NUMBER}"
}
}
}
}
Related
I'm trying to optimize my pipeline. I'm using the pipeline to generate and deploy some docs. At the end I clear my document root and write the newly generated docs into the document root. I'm doing this for several stages in parallel.
o-----o-----o--+--o--+---+--o--+-----o
| | | |
+--o--+ +--o--+
| | | |
+--o--+ +--o--+
this is the pipeline exerpt for the parallel stages
stage("clear nfs directory") {
steps {
parallel(
test: {
sh "rm -rf /mnt/nfs/test/docs/$pipelineParams.groupname"
sh "mkdir /mnt/nfs/test/docs/$pipelineParams.groupname"
},
rele: {
sh "rm -rf /mnt/nfs/rele/docs/$pipelineParams.groupname"
sh "mkdir /mnt/nfs/rele/docs/$pipelineParams.groupname"
},
prod: {
sh "rm -rf /mnt/nfs/prod/docs/$pipelineParams.groupname"
sh "mkdir /mnt/nfs/prod/docs/$pipelineParams.groupname"
}
)
}
}
stage("copy generated docs to nfs directory") {
steps {
parallel(
test: {
dir("target/public") {
sh "cp -r * /mnt/nfs/test/docs/$pipelineParams.groupname"
}
},
rele: {
dir("target/public") {
sh "cp -r * /mnt/nfs/rele/docs/$pipelineParams.groupname"
}
},
prod: {
dir("target/public") {
sh "cp -r * /mnt/nfs/prod/docs/$pipelineParams.groupname"
}
}
)
}
}
Since clear and write should depend on each other I would like to refactor the pipeline into a more sequential design (running multiple steps in sequence in less parallel steps)
o-----o-----o--+--o---o--+-----o
| |
+--o---o--+
| |
+--o---o--+
I'm not sure how to run multiple steps in the same parallel block ... can anyone give me a hint? Thanks guys
Please see below reference which will allow you to run multiple steps in same parallel block.
You would need to use sequential stages which will give below output :
o-----o-----o--+--o---o--+-----o
| |
+--o---o--+
| |
+--o---o--+
pipeline {
agent { label 'master' }
stages {
stage('Build and Test') {
parallel {
stage("Build and Test Linux") {
stages {
stage("Build (Linux)") {
agent any
steps {
echo "Inside for loop 1"
}
}
stage("Test (Linux)") {
agent any
steps {
echo "Inside for loop 2"
}
}
}
}
stage("Build and Test Windows") {
stages {
stage("Build (Windows)") {
agent any
steps {
echo "Inside for loop 3"
}
}
stage("Test (Windows)") {
agent any
steps {
echo "Inside for loop 4"
}
}
}
}
}
}
}
}
For more info see:-
https://www.jenkins.io/blog/2018/07/02/whats-new-declarative-piepline-13x-sequential-stages/
Below link gives reference example:
https://issues.jenkins.io/browse/JENKINS-55438
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'
}
}
}
}
Using new jenkins declarative pipeline syntax, I'd like to test the return status of a sh script execution. Is it possible without using script step?
Script pipeline (working) :
...
stage ('Check url') {
node {
timeout(15) {
waitUntil {
sleep 20
def r = sh script: "wget -q ${CHECK_URL} -O /dev/null", returnStatus: true
return (r == 0);
}
}
}
}
Declarative pipeline (try) :
...
stage('Check url'){
steps {
timeout(15) {
waitUntil {
sleep 20
sh script: "wget -q ${CHECK_URL} -O /dev/null", returnStatus: true == 0
}
}
}
}
log : java.lang.ClassCastException: body return value null is not boolean
Since it's not possible without script block, we get something like :
...
stage('Check url'){
steps {
script {
timeout(15) {
waitUntil {
sleep 20
def r = sh script: "wget -q ${CHECK_URL} -O /dev/null", returnStatus: true
return r == 0
}
}
}
}
}
I am trying to start a docker image from Jenkins.
(Not getting Docker to run from within Jenkins)
I think I'm really close but this part has still some issues.
Can please anyone help?
stage('build Dockerimage 1') {
steps{
apitestimage = docker.build('apitestimage', '--no-cache=true dockerbuild')
}
}
stage('start Dockerimage and Tests 2') {
steps{
apitestimage.inside {
sh 'cd testing && ctest'
}
}
}
Jenkins reports:
WorkflowScript: 21: Expected a step # line 21, column 15. apitestimage = docker.build('apitestimage', '--no-cache=true dockerbuild')
and also
WorkflowScript: 27: Method calls on objects not allowed outside "script" blocks. # line 27, column 13. apitestimage.inside {
From your error, it shows that you're missing a script block in your steps. You'll need a script block when using the DSL in steps.
stage('build Dockerimage 1') {
steps{
script {
def apitestimage = docker.build('apitestimage', '--no-cache=true dockerbuild')
}
}
}
stage('start Dockerimage and Tests 2') {
steps{
script {
apitestimage.inside {
sh 'cd testing && ctest'
}
}
}
}
References:
https://jenkins.io/doc/book/pipeline/syntax/#script
In my case, I did declare a variable using the stages name: def stages = [buildStage]
Changing this variable name to another name fixed my issue.
I am creating a sample jenkins pipeline, here is the code.
pipeline {
agent any
stages {
stage('test') {
steps {
sh 'echo hello'
}
}
stage('test1') {
steps {
sh 'echo $TEST'
}
}
stage('test3') {
if (env.BRANCH_NAME == 'master') {
echo 'I only execute on the master branch'
} else {
echo 'I execute elsewhere'
}
}
}
}
this pipeline fails with following error logs
Started by user admin
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 15: Not a valid stage section definition: "if (env.BRANCH_NAME == 'master') {
echo 'I only execute on the master branch'
} else {
echo 'I execute elsewhere'
}". Some extra configuration is required. # line 15, column 9.
stage('test3') {
^
WorkflowScript: 15: Nothing to execute within stage "test3" # line 15, column 9.
stage('test3') {
^
But when i execute the following example from this url, it executes successfully and print the else part.
node {
stage('Example') {
if (env.BRANCH_NAME == 'master') {
echo 'I only execute on the master branch'
} else {
echo 'I execute elsewhere'
}
}
}
The only difference i can see is that in the working example there is no stages but in my case it has.
What is wrong here, can anyone please suggest?
your first try is using declarative pipelines, and the second working one is using scripted pipelines. you need to enclose steps in a steps declaration, and you can't use if as a top-level step in declarative, so you need to wrap it in a script step. here's a working declarative version:
pipeline {
agent any
stages {
stage('test') {
steps {
sh 'echo hello'
}
}
stage('test1') {
steps {
sh 'echo $TEST'
}
}
stage('test3') {
steps {
script {
if (env.BRANCH_NAME == 'master') {
echo 'I only execute on the master branch'
} else {
echo 'I execute elsewhere'
}
}
}
}
}
}
you can simplify this and potentially avoid the if statement (as long as you don't need the else) by using "when". See "when directive" at https://jenkins.io/doc/book/pipeline/syntax/. you can also validate jenkinsfiles using the jenkins rest api. it's super sweet. have fun with declarative pipelines in jenkins!
It requires a bit of rearranging, but when does a good job to replace conditionals above. Here's the example from above written using the declarative syntax. Note that test3 stage is now two different stages. One that runs on the master branch and one that runs on anything else.
stage ('Test 3: Master') {
when { branch 'master' }
steps {
echo 'I only execute on the master branch.'
}
}
stage ('Test 3: Dev') {
when { not { branch 'master' } }
steps {
echo 'I execute on non-master branches.'
}
}
If you wanted to create a condition to execute only a stage based on expression you can use keyword when
stage ('test3'){
when { expression { return env.BRANCH_NAME == 'master'} }
steps {
echo 'I only execute on the master branch.'
}
}
}
With the expression key word you can add any condition.
e.g. if stage is dependent on generated file in workspace.
stage ('File Dependent stage'){
when { expression { return fileExists ('myfile') } }
steps {
echo "file exists"
}
}
}
if ( params.build_deploy == '1' ) {
println "build_deploy 是 ${params.build_deploy}"
jobB = build job: 'k8s-core-user_deploy', propagate: false, wait: true, parameters: [
string(name:'environment', value: "${params.environment}"),
string(name:'branch_name', value: "${params.branch_name}"),
string(name:'service_name', value: "${params.service_name}"),
]
println jobB.getResult()
}