如何在Jenkinsfile中标记当前的git变更集?

3phpmpom  于 2022-11-01  发布在  Jenkins
关注(0)|答案(5)|浏览(301)

我想标记当前的git变更集,并从Jenkinsfile中推送标记。如果标记已经存在,则必须替换它。
我希望使用此逻辑来标记使用snapshot标记传递的构建,这将是一个移动的标记。
我该怎么做?

i86rm4rw

i86rm4rw1#

这是我能够实现这一点的方法,但如果你知道一个更好的方法,我更愿意听到它。


# !groovy

stage 'build'
node {

    repositoryCommiterEmail = 'ci@example.com'
    repositoryCommiterUsername = 'examle.com'

    checkout scm

    sh "echo done"

    if (env.BRANCH_NAME == 'master') {
        stage 'tagging'

        sh("git config user.email ${repositoryCommiterEmail}")
        sh("git config user.name '${repositoryCommiterUsername}'")

        sh "git remote set-url origin git@github.com:..."

        // deletes current snapshot tag
        sh "git tag -d snapshot || true"
        // tags current changeset
        sh "git tag -a snapshot -m \"passed CI\""
        // deletes tag on remote in order not to fail pushing the new one
        sh "git push origin :refs/tags/snapshot"
        // pushes the tags
        sh "git push --tags"
    }
}
blmhpbnm

blmhpbnm2#

我想分享my Jenkins Pipeline Setup和我的解决方案,通过SSH发布变更/标签到git repo(Git Publish Support还在开发中)。请查看更多信息,任何改进的想法都是受欢迎的。
简而言之,您只需将文件git_push_ssh.groovy添加到项目中,并从Jenkinsfile中调用方法pushSSH(),如下所示:

env.BRANCH_NAME = "mycoolbranch"// BRANCH_NAME is predefined in multibranch pipeline job
env.J_GIT_CONFIG = "true"
env.J_USERNAME = "Jenkins CI"
env.J_EMAIL = "jenkins-ci@example.com"
env.J_CREDS_IDS = '02aa92ec-593e-4a90-ac85-3f43a06cfae3' // Use credentials id from Jenkins
def gitLib = load "git_push_ssh.groovy"
...
gitLib.pushSSH(commitMsg: "Jenkins build #${env.BUILD_NUMBER}", tagName: "build-${env.BUILD_NUMBER}", files: "changelog.txt someotherfile.txt");
gwbalxhn

gwbalxhn3#

对于那些无法使用上述方法的人,我直接使用了sshagent插件,它确实起到了作用:

stage('tag build'){
checkout([
    $class: 'GitSCM', branches: [[name: '*/master']],
    userRemoteConfigs: [[credentialsId: 'git',
    url: 'ssh://<ssh URL>']],
    extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'targeted-dir']]
])

sshagent(credentials: ['<credentials ID.']){
  dir('targeted-dir'){
    sh("git config user.email '<email>")
    sh("git config user.name '<user>.com'")

    // deletes current snapshot tag
    sh ("git tag -d ${PARAM_VERSION_NUMBER} || true")
    // tags current changeset
    sh ("git tag -a ${PARAM_VERSION_NUMBER} -m \"versioning ${PARAM_VERSION_NUMBER}\"")
    // deletes tag on remote in order not to fail pushing the new one
    sh ("git push origin :refs/tags/snapshot")
    // pushes the tags
    sh ("git push --tags")
    }
}

}

gk7wooem

gk7wooem4#

要使其在Blue Ocean(使用https连接)中正常工作,请使用以下命令:

sshagent(credentials: ["406ef572-9598-45ee-8d39-9c9a227a9227"]) {
                def repository = "git@" + env.GIT_URL.replaceFirst(".+://", "").replaceFirst("/", ":")
                sh("git remote set-url origin $repository")
                sh("git tag --force build-${env.BRANCH_NAME}")
                sh("git push --force origin build-${env.BRANCH_NAME}")
            }
dgtucam1

dgtucam15#

基于SSH代理的方法在我们的Windows build服务器上不起作用,但是,我们仍然希望使用存储在Jenkins的凭据管理器中的凭据。由于我们的Git凭据是一个SSH密钥,我们意识到我们可以让Jenkins直接为我们提供密钥文件:

stage("Tag") {
    steps {
        script {
            def tagName = "..."
            withCredentials([sshUserPrivateKey(credentialsId: "idOfYourCredentials", keyFileVariable: "KEY")]) {
                def key = KEY.replaceAll("\\\\", "/") // Only needed on Windows
                env.GIT_SSH_COMMAND = "ssh -i ${key}"
                bat "git tag \"${tagName}\""
                bat "git push --tags"
            }
        }
    }
}

相关问题