【问题标题】:Stop jenkins pipeline stage from failing when there are no changes to push to GitHub当没有更改推送到 GitHub 时,阻止 jenkins 管道阶段失败
【发布时间】:2018-12-23 19:00:51
【问题描述】:

帮助我阻止一个阶段失败,阻止进一步的阶段失败,从而阻止整个构建。问题归结为没有任何更改可以提交到 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..
                    '''
            }
        }
    }

当stage没有变化时('Snapshot - Version Upgrade')-stage失败了,我该如何阻止它失败,请帮忙。

这是失败的日志中的 sn-p:

[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)

【问题讨论】:

  • 能否请您添加整个输出?
  • @AutomatedOwl 从日志中添加了一个 sn-p

标签: jenkins groovy jenkins-pipeline stage


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-23
    • 2022-10-27
    相关资源
    最近更新 更多