简体   繁体   中英

Build with declarative pipeline does not work Jenkins, but works with Scripted pipeline

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 ?

First you can use this to clean your WS after every build, this will clean the workspace up nicely:

     post { 
      always { 
          cleanWs()
      }
  }

Regarding the my_job@2. Are you running concurrent builds? If not then i would log in to the jenkins machine and delete the workspaces related to this job and have the post block referred to earlier in your declarative pipeline.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM