In jenkins I have a choice:
choice(name: 'SERVICE', choices: ['SERVICE1', 'SERVICE2', 'SERVICE3', 'SERVICE4', 'SERVICE5', 'SERVICE6'], description: 'service')
Is there a way to set a variables depending of the above choice?
Something like this:
IF SERVICE == SERVICE1 then SERVICE_ID == SERVICE1_ID
IF SERVICE == SERVICE2 then SERVICE_ID == SERVICE2_ID
I'm struggling to find a plugin for this but I don't mind hardcoding in into jenkinsfile like above.
Done it
pipeline {
agent any
parameters {
choice(name: 'SERVICE', choices: ['Service1', 'Service2', 'Service3', 'Service4', 'Service5'], description: 'service')
}
stages {
stage('Stage 1') {
steps {
echo "This is Stage 1"
script {
if (env.SERVICE == 'Service1') {
echo 'You selected Service1'
} else {
echo 'You selected Some other service'
}
}
}
}
stage('Stage 2') {
steps {
echo "This is Stage 2"
script {
if (env.SERVICE == 'Service1') {env.SERVICE_ID = 'Service1_ID'}
if (env.SERVICE == 'Service2') {env.SERVICE_ID = 'Service2_ID'}
if (env.SERVICE == 'Service3') {env.SERVICE_ID = 'Service3_ID'}
if (env.SERVICE == 'Service4') {env.SERVICE_ID = 'Service4_ID'}
if (env.SERVICE == 'Service5') {env.SERVICE_ID = 'Service5_ID'}
echo "Service is ${env.SERVICE}"
echo "Service ID is ${env.SERVICE_ID}"
// Here goes some script you want to run
// For example:
// container('docker') {
// sh """
// some bash command with ${env.SERVICE_ID}
// """
// }
}
}
}
stage('Stage 3') {
steps {
echo "This is Stage 3"
}
}
stage ('Stage 4') {
steps {
echo "This is Stage 4"
}
}
}
}
Related
GITLAB_VERSION: GitLab Enterprise Edition 13.9.3-ee
JENKINS_VERSION: 2.263.4
I have crated a jenkins pipeline which is being triggered by change in gitlab, but its not updating gitlab status.
pipeline {
agent any
stages {
stage('cloning from gitlab'){
steps{
git credentialsId: '7d13ef14-ee65-497b-8fba-7519f5012e81', url: 'git#git.MYDOMAIN.com:root/popoq.git'
}
}
stage('build') {
steps {
echo 'Notify GitLab'
updateGitlabCommitStatus name: 'Jenkins-build', state: 'pending'
echo 'build step goes here'
}
}
stage('echoing') {
steps{
echo "bla blaa bla"
}
}
stage(test) {
steps {
echo 'Notify GitLab'
echo 'test step goes here'
updateGitlabCommitStatus name: 'Jenkins-build', state: 'success'
}
}
}
}
its not showing any pipline in gitlab, any suggestions?
I think you miss a "gitlabBuilds" command in an "option" block declaring the steps you will have in your build.
options {
gitLabConnection('xxx-gitlab')
gitlabBuilds(builds: ['step1', 'step2', 'step3'])
}
Then you can reference those steps with the "updateGitlabCommitStatus" but you'd better use the "gitlabCommitStatus" command like this:
pipeline {
agent any
options {
gitLabConnection('xxx-gitlab')
gitlabBuilds(builds: ['step1', 'step2', 'step3'])
}
stages {
stage('step1'){
steps{
gitlabCommitStatus(name:'step1') {
git credentialsId: '7d13ef14-e', url: 'xxxxxxx'
}
} // end steps
} // end stage
stage('step2'){
steps{
gitlabCommitStatus(name:'step2') {
.......
}
} // end steps
} // end stage
}
pipeline {
agent {
label 'agent_gradle'
}
options {
gitLabConnection('Gitlab Jenkins integration API connection test')
gitlabBuilds(builds: ['step1', 'step2'])
}
stages {
stage('Build') {
steps {
gitlabCommitStatus(name: 'step1') {
container(name: 'gradle') {
echo 'Building the application...'
}
}
}
}
stage('Test') {
steps {
gitlabCommitStatus(name: 'step2') {
container(name: 'gradle') {
echo 'Testing the application...'
}
}
}
}
}
}
Here, I need to execute both Parallel test 1 and Parallel test 2 at the same time.
When I tried to put a parallel block on top of these, it throws an error since it mentioned like this in the official site Note: that a stage must have one and only one of steps, stages, or parallel.
pipeline {
agent any
stages {
stage('Parallel Test 1') {
parallel {
stage('Block 1 - Stage 1') {
steps {
echo "Block 1 - Stage 1"
build(job: 'jenkins_job_1')
}
}
stage('Block 1 - Stage 2') {
steps {
echo "Block 1 - Stage 2"
build(job: 'jenkins_job_2')
}
}
}
}
stage('Parallel Test 2') {
parallel {
stage('Block 2 - Stage 1') {
steps {
echo "Block 2 - Stage 1"
build(job: "jenkins_job_3")
}
}
stage('Block 2 - Stage 2') {
steps {
echo "Block 2 - Stage 2"
build(job: "jenkins_job_4")
}
}
}
}
}
}
You don't have to put each call to a parallel-job inside a stage, so you can do it as such:
pipeline {
agent any
stages {
stage('single run') {
parallel {
stage('Parallel Test 1') {
steps {
script {
def group1 = [:]
group1["test_1"] = {
echo "test_1"
sh(script: "date -u")
build(job: 'jenkins_job_1')
}
group1["test_2"] = {
echo "test_2"
sh(script: "date -u")
build(job: 'jenkins_job_2')
}
parallel group1
}
}
}
stage('Parallel Test 2') {
steps {
script {
def group2 = [:]
group2["test_3"] = {
echo "test_3"
sh(script: "date -u")
build(job: 'jenkins_job_3')
}
group2["test_4"] = {
echo "test_4"
sh(script: "date -u")
build(job: 'jenkins_job_4')
}
parallel group2
}
}
}
}
}
}
}
I have declared a variable TENTATIVE_VERSION in my script, and I need to define/modify it with the value coming from executing a script (or from the script itself in other stage), how can I do this? my current script is something like this:
pipeline {
agent {
label 'machine1'
}
stages {
stage('Non-Parallel Stage') {
agent{label "machine2"}
steps {
script {
TENTATIVE_VERSION="1.0" // working
// TENTATIVE_VERSION="sh echo 123" //not working
}
}
}
stage('Parallel Stage') {
parallel {
stage('A') {
agent {label 'machine3'}
steps {
echo "On other machine"
echo "${TENTATIVE_VERSION}"
build job: 'otherJob', parameters: [[$class: 'StringParameterValue', name: 'VERSION', value: "${TENTATIVE_VERSION}"],
[$class: 'StringParameterValue', name: 'RELEASE', value: '1']]
}
}
stage('B') {
agent {label "machine4"}
steps {
script {
STATUS_S = "OK"
}
echo "On a machine"
}
}
stage('C') {
agent {label "machine5"}
steps {
script {
STATUS_R = "OK"
}
echo "On a machine"
}
}
}
}
}
Try following:
pipeline {
agent {
label 'machine1'
}
stages {
stage('Non-Parallel Stage') {
agent{label "machine2"}
steps {
script {
TENTATIVE_VERSION = sh(returnStdout: true, script: "echo 123").trim()
}
}
}
}
}
I try to use the post steps with the Jenkins kubernetes plugin. Does anyone has an idea?
java.lang.NoSuchMethodError: No such DSL method 'post' found among steps
My pipeline:
podTemplate(
label: 'jenkins-pipeline',
cloud: 'minikube',
volumes: [
hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'),
]) {
node('jenkins-pipeline') {
stage('test') {
container('maven') {
println 'do some testing stuff'
}
}
post {
always {
println "test"
}
}
}
}
As of this writing, Post is only supported in declarative pipelines.
You could have a look at their declarative example if you absolutely must use post.
pipeline {
agent {
kubernetes {
//cloud 'kubernetes'
label 'mypod'
containerTemplate {
name 'maven'
image 'maven:3.3.9-jdk-8-alpine'
ttyEnabled true
command 'cat'
}
}
}
stages {
stage('Run maven') {
steps {
container('maven') {
sh 'mvn -version'
}
}
}
}
}
This example shows how to use the post step using the Kubernetes plugin:
pipeline {
agent {
kubernetes {
label "my-test-pipeline-${BUILD_NUMBER}"
containerTemplate {
name "my-container"
image "alpine:3.15.0"
command "sleep"
args "99d"
}
}
}
stages {
stage('Stage 1') {
steps {
container('my-container') {
sh '''
set -e
echo "Hello world!"
sleep 10
echo "I waited"
echo "forcing a fail"
exit 1
'''
}
}
}
}
post {
unsuccessful {
container('my-container') {
sh '''
set +e
echo "Cleaning up stuff here"
'''
}
}
}
}
Here is my Jenkins pipeline that i am trying to execute. I am following this tutorial:
pipeline {
agent any
stages {
stage('one') {
parallel "first" : {
echo "hello"
},
"second": {
echo "world"
}
}
stage('two') {
parallel "first" : {
echo "hello"
},
"second": {
echo "world"
}
}
}
}
But the job fails with following message.
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 4: Unknown stage section "parallel". Starting with version 0.5, steps in a stage must be in a steps block. # line 4, column 9.
stage('one') {
^
WorkflowScript: 12: Unknown stage section "parallel". Starting with version 0.5, steps in a stage must be in a steps block. # line 12, column 9.
stage('two') {
^
WorkflowScript: 4: Nothing to execute within stage "one" # line 4, column 9.
stage('one') {
^
WorkflowScript: 12: Nothing to execute within stage "two" # line 12, column 9.
stage('two') {
^
4 errors
Can someone please help me out why this is failing.
You need to add a steps block after your stage declaration.
pipeline {
agent any
stages {
stage('Example Stage 1') {
steps {
parallel(
"step 1": { echo "hello" },
"step 2": { echo "world" },
"step 3": { echo "world" }
)
}
}
stage('Example Stage 2') {
steps {
parallel(
"step 1": { echo "hello" },
"step 2": { echo "world" },
"step 3": { echo "world" }
)
}
}
}
}
To Make your Stages Parallel use this, both solutions show up very similar in Blue Ocean :
pipeline {
agent any
stages {
stage('Example Stage') {
parallel {
stage('Stage 1') {
steps { sh 'echo stage 1 passed' }
}
stage('Stage 2') {
steps { sh 'echo stage 2 passed' }
}
stage('Stage 3') {
steps { sh 'echo stage 3 passed' }
}
}
}
}
}
You need to upgrade the Declarative Pipeline plugin on your Jenkins to Version 1.2 (Sept 21, 2017) or above
In declarative pipeline, in case if you want to add stage, inside steps, this nesting is also possible.
If the steps are same in that case you can declare step as a function.
def steps = ['first', 'second']
def generateSteps(stepLabel) {
return {
step("${stepLabel}") {
echo "Running on ${stepLabel}"
}
}
}
def parallelStepMap = steps.collectEntries {
["${it}" : generateSteps(it)]
}
pipeline {
agent any
stages {
stage('non-parallel stage') {
steps {
echo 'This stage will be executed first.'
}
}
stage('parallel stage') {
steps {
script {
parallel parallelStepMap
}
}
}
}
}
General need is to execute different stages into different nodes.
in such case above function can be modified to execute on different agent nodes as follows.
def agents = ['master', 'agent1', 'agent2']
def generateStage(nodeLabel) {
return {
stage("Runs on ${nodeLabel}") {
node(nodeLabel) {
echo "Running on ${nodeLabel}"
}
}
}
}
def parallelStagesMap = agents.collectEntries {
["${it}" : generateStage(it)]
}
pipeline {
agent none
stages {
stage('non-parallel stage') {
steps {
echo 'This stage will be executed first.'
}
}
stage('parallel stage') {
steps {
script {
parallel parallelStagesMap
}
}
}
}
}
With nodelabel defined inside the function and agent none during start of pipeline, the stages will be executed into different nodes.