【问题标题】:How to create a tag for specific branch using Jenkinsfile如何使用 Jenkinsfile 为特定分支创建标签
【发布时间】:2021-09-30 06:31:16
【问题描述】:

我有一个场景,我需要仅在从 master 分支构建标签时构建和推送 docker 文件。

stage('Tag') {
      when {
        expression { sh([returnStdout: true, script: 'echo $TAG_NAME | tr -d \'\n\'']) }
      }
      steps {
        script {
          echo "tag"
        }
      }
    }

上面的代码可以工作。如果我们为开发分支或测试分支创建一个标签,即使条件得到满足。谁能帮我解决这个问题。

【问题讨论】:

  • 您能否澄清一下问题所在?

标签: github jenkins jenkins-pipeline jenkins-groovy


【解决方案1】:

如果有任何帮助,这是我用来在 repos 上标记分支的顺序 Jenkinsfile 的一个例外。这是一个两阶段的过程 - 找到分支的当前提交,然后标记:

        steps.withCredentials([steps.usernamePassword(credentialsId: httpCredentials,
                usernameVariable: 'GITHUB_USERNAME', passwordVariable: 'GITHUB_TOKEN')]) {
            // Need to do this as a two stage activity (need commit sha) but *warning* this api assumes ref is a branch
            def json_info = steps.sh(script: "curl -X GET -H \"Authorization: token \$GITHUB_TOKEN\" -H \"Accept: application/vnd.github.v3+json\" ${httpUrlForHost}/repos/${slugForRepo}/branches/${ref}",
                    returnStdout: true)
            def branch_info = steps.readJSON text: json_info
            def sha = branch_info?.commit?.sha
            if (!sha) {
                steps.error("Unexpected sha for branch ${ref} (${json_info})")
            }
            def jsonPayload = "{\"ref\":\"refs/tags/${new_tag}\",\"sha\":\"${sha}\"}"

            steps.sh "curl -X POST -H \"Authorization: token \$GITHUB_TOKEN\" -H \"Accept: application/vnd.github.v3+json\" -d '${jsonPayload}' ${httpUrlForHost}/repos/${slugForRepo}/git/refs"
        }

【讨论】:

    猜你喜欢
    • 2015-09-02
    • 1970-01-01
    • 2012-03-01
    • 1970-01-01
    • 2012-07-03
    • 1970-01-01
    • 1970-01-01
    • 2015-03-21
    • 2021-03-15
    相关资源
    最近更新 更多