I am getting sources from git
, next my step is replace of needed variables in these sources, next step build these sources in the docker container. How it works with scripted pipeline.
node('jenkinsslave') {
stage('Remove old sources'){
sh 'rm -rf /var/lib/jenkins/workspace/$JOB_NAME/*'
}
stage('Get sources'){
checkout scm
}
stage('Replace variables'){
sh """
/scripts/replace_variables.sh "/var/lib/jenkins/workspace/$JOB_NAME"
"""
}
stage('Run in container')
docker.image('maven').inside('-u root:root') {
sh "mvn clean install"
}
stage('Chown to user Jenkins'){
sh "sudo chown -R jenkins:jenkins /var/lib/jenkins/workspace/$JOB_NAME/"
}
}
It works, without any probles. So, i tried to rewrite this to declarative pipeline. Look please.
pipeline {
agent {label 'jenkinsslave'}
stages {
stage('Remove old sources'){
steps {
echo 'remove old resources'
sh 'rm -rf /var/lib/jenkins/workspace/$JOB_NAME/*'
}
}
stage('Checkout SCM') {
steps {
echo '> Checking out the source control ...'
checkout scm
}
}
stage('Replace variables') {
steps {
echo '> Replace needed variables ...'
sh """
/scripts/replace_variables.sh "/var/lib/jenkins/workspace/$JOB_NAME"
"""
}
}
stage('Build') {
agent {
docker {
image 'maven:latest'
args '-u root:root'
}
}
steps {
sh "mvn clean install"
}
}
stage('Chown to user Jenkins'){
steps {
echo 'Chown to user Jenkins'
sh "sudo chown -R jenkins:jenkins /var/lib/jenkins/workspace/$JOB_NAME/"
}
}
}
}
In the second variant, yes, it builds sources, but in the second workspace. And of course, my step with replace of needed variables does not work.
For example, i have workspace, my_job
. But it creates, my_job@2
. Also, i tried to add
options {
skipDefaultCheckout(true)
}
Result was the same. How i can fix it ?