在没有更改推送到GitHub时阻止jenkins管道阶段失败

时间:2018-07-16 09:09:57

标签: jenkins groovy jenkins-pipeline stage

帮助我停止一个阶段失败,使后续阶段失败,从而使整个构建失败。问题一直到没有更改提交到GitHub的阶段。代码如下:

pipeline {
agent { label 'master' }

  environment {
    // 'This value is exported to all commands in this stage'
    blah
    blah..
    blah...
  } 
stages {
    stage('Check Environment') {
        steps {
            sh "echo 'Build_Id:  ' $BUILD_ID"
            sh "echo 'Job_Name:  '  $JOB_NAME"
            sh "echo 'Build_Name:  '  $BUILD_NAME"
            sh "echo 'Current Branch Name:  '  $BRANCH"
        }
    }

    stage('Checkout Source Code') {
        steps {
            git branch: 'develop', url: 'git@github.com:something-some/otherthing.git'
       }
    }

    stage('Snapshot - Version Upgrade') {
           environment {
                VERSION = readMavenPom().getVersion()
            }
        steps {
            script {
                sh '''
                #!/bin/bash -xe
                echo $VERSION
                mvn build-helper:parse-version versions:set versions:commit '-DnewVersion=${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.incrementalVersion}'
                git status
                git add .
                MVN_VERSION=$(mvn -q -Dexec.executable="echo" -Dexec.args='${project.version}' --non-recursive org.codehaus.mojo:exec-maven-plugin:1.3.1:exec)
                git commit -m "Jenkins version upgrade - ${MVN_VERSION}"
                git push --set-upstream origin $BRANCH
                    '''
            }
        }
    }

    stage('Test Release - Deploy Packages To Artifactory') {
        steps {
            script {
                sh '''
                    blah
                    more blah..
                    '''
            }
        }
    }

如果阶段中没有任何更改(“快照-版本升级”)-阶段失败,如何阻止失败,请帮忙。

这是失败的日志片段:

[INFO] 
[INFO] --- versions-maven-plugin:2.5:commit (default-cli) @ Integrityv1 ---
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] Some project Parent ................................ SUCCESS [  0.010     s]
[INFO] Child Proj1......................................... SUCCESS [  0.007 s]
[INFO] Child Proj2......................................... SUCCESS [  0.007 s]
[INFO] Child Proj3......................................... SUCCESS [  0.007 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 05:07 min
[INFO] Finished at: 2018-07-13T13:25:37+01:00
[INFO] Final Memory: 27M/304M
[INFO] ------------------------------------------------------------------------
+ git status
On branch develop
nothing to commit, working directory clean
+ git add .
+ mvn -q -Dexec.executable=echo -Dexec.args=${project.version} --non-        recursive org.codehaus.mojo:exec-maven-plugin:1.3.1:exec
+ MVN_VERSION=1.0.2
+ git commit -m Jenkins version upgrade - 1.0.2
On branch develop
nothing to commit, working directory clean
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Test Release - Deploy Packages To Artifactory)
Stage "Test Release - Deploy Packages To Artifactory" skipped due to earlier     failure(s)
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Publish to Windows Share)
Stage "Publish to Windows Share" skipped due to earlier failure(s)
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Delete Target)
Stage "Delete Target" skipped due to earlier failure(s)

1 个答案:

答案 0 :(得分:1)

只有更改,您才能执行commitpush

if [ ! -z "$(git status --porcelain)" ]; then
    git commit -m "Jenkins version upgrade - ${MVN_VERSION}"
    git push --set-upstream origin $BRANCH
fi

如果您不熟悉--porcelain,请参见documentation on git-status