Run Jenkin jobs in parallel - jenkins

I have 3 different jobs (Build, Undeploy and Deploy), which want to execute Build and Undeploy in parallel and after that Deploy.
From search got to know that Build Flow Plugin got deprecated.
Please suggest a plugin.
Thanks

You can write Jenkins file with the below format:-
pipeline {
stages {
agent { node { label 'master' } }
stage('Build/Undeploy') {
parallel {
stage('Build') {
agent { node { label 'Build' } }
steps {
script {
//Call your build script
}
}
}
stage('Undeploy') {
agent { node { label 'Undeploy' } }
steps {
script {
//Call your undeploy script
}
}
}
}
}
stage('Deploy'){
agent { node { label 'Deploy' } }
steps {
script {
//Call your deploy script
}
}
}
}
}

Related

Jenkins declarative pipeline not reading stages or steps

I am new to the jenkins world and not sure why it's not reading the steps in my declarative pipeline.
I am sure that it's something simple but I can't seem to see where the error is.
import cloud.GitClient
import cloud.SlackClient
def call(body) {
def parameters = [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = parameters
body()
pipeline {
agent {
kubernetes {
cloud parameters['buildCloudId']
}
}
stages {
stage('Inform Build Start') {
steps {
script{
slackClient.warn('Starting build!')
}
}
}
stage('Build Docker Images') {
steps {
checkout scm
container('docker') {
withCredentialsee(usernamePassword(credentialsId: parameters.docker.credentialsId, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')) {
sh """
echo "\$PASSWORD" | docker login -u \$USERNAME --password-stdin hub.containers
./build.sh
"""
}
}
}
post {
failure {
script {
slackClient.failed("Docker backend build *FAILED*! <${env.BUILD_URL}console|See log> for details.")
}
}
success {
script {
slackClient.success("Docker backend build *COMPLETED*! <${env.BUILD_URL}console|See log> for details.")
}
}
}
}
}
}
}
In jenkins, everything shows green, but logs shows that nothing was ran at all.

Using variable in another stage in declarative Jenkinsfile

I'm writing a declarative Jenkinsfile which looks like this. In the stage "build" I define the variable customImage which I would like to use in the stage "Push".
Unfortunately I cannot get this to work.
pipeline {
agent any
stages {
stage("Build") {
steps {
script {
def commitHash = GIT_COMMIT.take(7)
echo "Building Docker image for commit hash: " + commitHash
def customImage = docker.build("myimage:${commitHash}")
}
}
}
stage("Push") {
steps {
echo "Pushing Docker image to registry..."
script {
docker.withRegistry(REGISTRY_SERVER, REGISTRY_CREDENTIALS) {
$customImage.push()
}
}
}
}
}
}
You just have to define the variable at a scope, where you can access it later, i.e.
def customImage
pipeline {
agent any
stages {
stage("Build") {
steps {
script {
def commitHash = GIT_COMMIT.take(7)
echo "Building Docker image for commit hash: " + commitHash
customImage = docker.build("myimage:${commitHash}")
}
}
}
stage("Push") {
steps {
echo "Pushing Docker image to registry..."
script {
docker.withRegistry(REGISTRY_SERVER, REGISTRY_CREDENTIALS) {
customImage.push()
}
}
}
}
}
}

Dynamic Stage Creation in Jenkinsfile?

I have 3 builds that could happen in parallel that are configured the exact same way except for the name of the command. I thought it'd be nice to automate this, and have tried for hours, several different ways, none of which have worked. How can I accomplish dynamically creating parallel stages?
final String androidDockerImage = 'thyrlian/android-sdk:2.4'
pipeline {
agent none
options {
timeout(time: 30, unit: 'MINUTES')
}
environment {
IS_CI = 'true'
}
stages {
stage('CI Build') {
parallel {
stage('Code Style') {
agent { docker { image androidDockerImage } }
steps {
sh './scripts/sign_android_sdk_agreement.sh'
sh './gradlew codeStyle'
}
}
stage('Lint') {
agent { docker { image androidDockerImage } }
steps {
sh './scripts/sign_android_sdk_agreement.sh'
sh './gradlew lint --stacktrace'
}
}
script {
['Internal Debug', 'Internal Release', 'Production Release'].forEach { variantName ->
final String variant = variantName.replace(" ", "")
stage($variantName) {
agent { docker { image androidDockerImage } }
steps {
sh './scripts/sign_android_sdk_agreement.sh'
sh "./gradlew clean target-android:assemble$variant --stacktrace"
}
post {
always {
junit "**/TEST-*.xml"
}
}
}
}
}
}
}
}
}

Group stages in parallel

I want to have a parallel pipeline like
parallel (
"stream1" {
}
"stream2" {`
}
"stream3" {`
}
)
where I can add stages(two in each stream) and show them as in the attachment.
Currently it works as the below
How can I make the stages to be visible in blue ocean?
It is not possible at the moment.
There is a feature request to add this functionality, which you could vote for:
https://issues.jenkins-ci.org/browse/JENKINS-38442?page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel
Yes this is possible with Declarative Pipeline 1.3 and above. Following sample code will work for example you have shared.
stages {
stage("Build") {
steps {
echo "Executing Build"
}
}
stage ("Parallel Builds") {
parallel {
stage("stream1") {
stages {
stage("JUnit") {
steps {
echo "Executing JUnit"
}
}
stage("Firefox") {
steps {
echo "Executing Firefox"
}
}
}
}
stage("stream2") {
stages {
stage("DBUnit") {
steps {
echo "Executing DBUnit"
}
}
stage("Edge") {
steps {
echo "Executing Edge"
}
}
}
}
stage("stream3") {
stages {
stage("Jasmine") {
steps {
echo "Executing Jasmine"
}
}
stage("Safari") {
steps {
echo "Executing Safari"
}
}
}
}
}
}
stage("Dev") {
steps {
echo "Executing Dev"
}
}
}
For Official documentation please refer to: enter link description here

Nested docker agents in a Jenkinspipeline

I have the following setup:
Jenkinsmaster, no docker installed
Jenkinsslave, docker is installed, label dockerslave
When I run the following pipeline:
pipeline {
agent { node { label 'dockerslave' } }
stages {
stage('Example Build') {
agent { docker { image 'maven:3-alpine' } }
steps {
echo 'Hello, Maven'
sh 'mvn --version'
}
}
stage('Example Test') {
agent { docker { image 'openjdk:8-jre' } }
steps {
echo 'Hello, JDK'
sh 'java -version'
}
}
}
}
I get the following logoutput:
[Pipeline] node
Running on dockerslave in /home/jenkins/workspace/docker-
declarative
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Example Build)
[Pipeline] node
Still waiting to schedule task
There are no nodes with the label ?latest?
The job doesn't proceed and hangs.
What is the problem here?
The problem was the missing:
reuseNode true
The fixed example:
pipeline {
agent {
node { label 'dockerslave' } }
stages {
stage('Example Build') {
agent {
docker {
reuseNode true
image 'maven:3-alpine'
}
}
steps {
echo 'Hello, Maven'
sh 'mvn --version'
}
}
stage('Example Test') {
agent {
docker {
reuseNode true
image 'openjdk:8-jre'
}
}
steps {
echo 'Hello, JDK'
sh 'java -version'
}
}
}
}

Resources