jenkins pipeline, unstash from a sub job - jenkins

I have a separate build pipeline that uses jenkinsfile to build the code.
I trigger it from a deploy pipeline and want to get build results.
The reason for this is that devs can define build steps but deploy is out of there control.
Here's a sample code in jenkins job builder:
- job:
name: Build and Deploy
project-type: pipeline
dsl: |
node {
stage('Build') {
# that job does stash inside a jenkinsfile
build job: "Build"
sh 'cp -rv "../Build/dist/" ./' # this is a workaround
stash includes: 'dist/*.zip', name: 'archive'
}
stage('Deploy') {
unstash 'archive'
sh "..."
}
}
So how can I unstash code stash-ed in a sub-job?
P.S.: there's also a workaround with artefacts:
In a sub-job:
archiveArtifacts artifacts: '*.zip', fingerprint: true
main DSL:
dsl: |
node {
def build_job_number = 0
def JENKINS = "http://127.0.0.1:8080"
stage('Build') {
def build_job = build job: "Build"
build_job_number = build_job.getNumber()
}
stage('Deploy') {
sh "wget -c --http-user=${USER} --http-password=${TOKEN} --auth-no-challenge ${JENKINS}/job/Build/${build_job_number}/artifact/name.zip"
sh "..."
}
}
The issue here is that API token is required.

If you go with archiveArtifacts you can use copyArtifacts to complement it.
As far as I know stash/unstash only work within the same job, so your other option would be to tick the Preserve stashes from completed builds in the pipeline's configuration so you can re-use them.

Related

Jenkins concurrent builds interfering each other

I have a declare pipeline, shown as follow:
pipeline{
agent any
stages{
stage("Pull Souce Code"){
steps{
checkout(...)
}
}
stage("Build and Push image"){
steps{
script{
docker.withRegistry(...){
def image = docker.build(...)
image.push()
}
}
}
}
}
}
The pipeline is running on the Jenkins Master(I don't build any jenkins slave).
When I run this task concurrently, sometimes the Dockerfile referenced does not match the GitLab project.
I notice the current pipeline have 2 Jenkins workspace:
/var/lib/jenkins/workspace/job_name and /var/lib/jenkins/workspace/job_name#2
Their Git files don't match their Dockerfile files.
I have to disable concurrent build now. What should I do?

Modify env.BRANCH_NAME variable with branch-env.BRANCH_NAME in jenkinsfile for a multibranch pipeline project

I have created a multibranch pipeline project and thus created jenkinsfile and put that in dev branch.
In one of the stage, I have to run mvn sonar:sonar -Dsonar.scm.branch=branch-${env.BRANCH_NAME} but it's giving error as bad substitution branch-${env.BRANCH_NAME}.
I need branch-${env.BRANCH_NAME} as a branch-name so that at sonar i can see branch-dev at branches section in sonar dashboard.
if i use mvn sonar:sonar -Dsonar.scm.branch=env.BRANCH_NAME then it provides output but it will act as short-lived branch in sonar. but at sonar we want branch as long-lived branch.
!/usr/bin/env groovy
pipeline {
agent { label 'ol73_slave-jdk8u192-git' }
options {
timestamps()
timeout(time: 2, unit: 'HOURS')
buildDiscarder(logRotator(numToKeepStr: '10'))
disableConcurrentBuilds()
}
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Unit Test and Code Scan') {
steps {
echo "*****JUnit Tests, JaCoCo Code Coverage, & SonarQube Code Qualiy Scan*****"
withMaven(jdk: 'jdk8_u192', maven: 'maven-3.3.9', mavenSettingsConfig: '79ecf9bd-8cbc-4d5e-b7d1-200241e16b52') {
sh '''
cd DARC
mvn clean package sonar:sonar -Dsonar.host.url=***** -Dsonar.login=******* -Dsonar.exclusions=file:**/src/test/** -B -Pcoverage -Dsonar.branch.name=branch-${env.BRANCH_NAME}
'''
}
}
}
}
}
In groovy Strings are encapsulated in single quotes '' and GStrings in double quotes ""
In order to do interpolation you need to be using GStrings. In your example, this should simply be
sh """
cd DARC
mvn clean package sonar:sonar -Dsonar.host.url=***** -Dsonar.login=******* -Dsonar.exclusions=file:**/src/test/** -B -Pcoverage -Dsonar.branch.name=branch-${env.BRANCH_NAME}
"""

Failing Jenkins build when Checkmarx job fails

I have configured a Checkmarx job in Jenkins and I wanted to integrate the Job with the actual build job of a repository.
In my Jenkinsfile I've configured this as a stage and the job gets executed.
The question is how to I listen to failures on the Checkmarx job and accordingly change the status of my build job? Here's a snippet from my JenkinsFile
agent any
stages {
stage('Build') {
steps {
echo 'Building'
...
........
...........
}
}
stage('Checkmarx') {
when {
branch 'master'
}
steps {
echo 'Kicking off checkmarx job..'
build job: 'checkmarx', wait: false
}
}
If you would just make it wait for the downstream job it would fail with it
steps {
echo 'Kicking off checkmarx job..'
build job: 'checkmarx', wait: true
}

How to copy Jenkins config files in a jenkins pipeline to Web server

I have some files.properties in Jenkins config File that I need to copy to a server during the jenkins pipeline.
pipeline code is more a less as showed, just to get an idea.
How can I add a step that copy this config file from jenkins on a destination server after las step after step DEPLOY WAR TO SERVER in pipeline like for example : "sh Scp file.properties jenkins#destinationserver:/destination/path/file.properties"
code {
stage ('Code Checkout') {
git branch: 'master',
credentialsId: 'b346fbxxxxxxxxxxxxxxxxxxx',
url: 'https://xxxxxxx#bitbucket.org/gr/code.git'
}
stage ('Check Branch') {
sh 'git branch'
}
stage('Compile and Build WAR') {
sh 'mvn clean compile war:war'
stage ('Deploy WAR to server') {
sh "scp .war jenkins#serverIp:/var/lib/tomcat/.war"
}
This is quite easy. You need to install the Config File Provider Plugin and then you can generate the appropriate line by visiting htts://localhost/jenkins/pipeline-syntax/. From there in the dropdown you can choose configFileProvider and fill the rest of the form.
The end result will be something like this:
configFileProvider(
[configFile(fileId: 'maven-settings-or-a-UUID-to-your-config-file', variable: 'MAVEN_SETTINGS')]) {
sh 'mvn -s $MAVEN_SETTINGS clean package'
}

Reuse artifacts at a later stage in the same Jenkins project

I have a Jenkins pipeline whose Build step has an archiveArtifacts command.
After the Build step there is Unit test, Integration test and Deploy.
In Deploy step, I want to use one of the artifacts. I thought I could find it in the same place the Build step generated it, but apparently the archiveArtifacts has deleted them.
As a workaround I can copy the artifact before it is archived, but it doesn't look elegant to me. Is there any better way?
As I understand it, archiveArtifacts is more for saving artifacts for use by something (or someone) after the build has finished. I would recommend looking at using "stash" and "unstash" for transferring files between stages or nodes.
You just go...
stash include: 'globdescribingfiles', name: 'stashnameusedlatertounstash'
and when you want to later retrieve that artifact...
unstash 'stashnameusedlatertounstash'
and the stashed files will be put into the current working directory.
Here's the example of that given in the Jenkinsfile docs (https://jenkins.io/doc/book/pipeline/jenkinsfile/#using-multiple-agents):
pipeline {
agent none
stages {
stage('Build') {
agent any
steps {
checkout scm
sh 'make'
stash includes: '**/target/*.jar', name: 'app'
}
}
stage('Test on Linux') {
agent {
label 'linux'
}
steps {
unstash 'app'
sh 'make check'
}
post {
always {
junit '**/target/*.xml'
}
}
}
stage('Test on Windows') {
agent {
label 'windows'
}
steps {
unstash 'app'
bat 'make check'
}
post {
always {
junit '**/target/*.xml'
}
}
}
}
}

Resources