我是Jenkinsfile
的新手,我正在尝试将我需要的工作传递给Jenkinsfile。我已经成功创建了所需的所有步骤,但是无法像以前使用界面和“ git Publisher”插件那样向我的git存储库添加标签。
这就是我现在所拥有的:
stage('Tag') {
steps {
script {
env.POM_VERSION = readMavenPom().getVersion()
}
// creating the name of the tag
sh '''#!/bin/bash -xe
currentDate=$(date +"%Y-%m-%d_%Hh%Mm%Ss")
customTagName="${CUSTOMER_NAME}--${POM_VERSION}--${currentDate}"
echo CUSTOM_TAG_NAME=${customTagName} >> ${PROPERTIES_FILE_NAME}
'''
script {
def PROPERTIES = readProperties file: "${PROPERTIES_FILE_NAME}"
env.CUSTOM_TAG_NAME = PROPERTIES.CUSTOM_TAG_NAME
}
withCredentials([usernamePassword(credentialsId: "${GIT_CREDENTIALS_ID}", passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
sh "git tag -a ${CUSTOM_TAG_NAME} -m 'Jenkinsfile push tag'"
sh "git push https://${GIT_USERNAME}:${GIT_PASSWORD}@git.repohere.com/scm/reponamehere.git ${CUSTOM_TAG_NAME}"
}
}
}
但是后来我在日志中遇到了这个问题:
[管道] sh + git push'https:// ****:****@git.repohere.com/scm/reponamehere.git'名称--4.36.0--2020-04-10_22h00m50s
致命:无法访问'https:// ****:****@git.repohere.com/scm/reponamehere.git/':无法解析主机:****;未知错误 [管道]} [管道] // withCredentials
我已经坚持了几个小时,如何添加和推送带有在jenkins config中添加的凭据的标签。谢谢!
答案 0 :(得分:0)
首先在管道的环境指令中已经声明了变量${GIT_CREDENTIALS_ID}
吗?如果未先声明,或直接使用变量的值,如下所示:
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'MyID', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD']]) {
sh("git tag -a some_tag -m 'Jenkins'")
sh("git push https://${env.GIT_USERNAME}:${env.GIT_PASSWORD}@<REPO> --tags")
}
如果您使用sshagent创建标签并推送,则无需在jenkinsfile中的GIT存储库网址中传递密码。
pipeline {
agent any
stages {
stage("Tag and Push") {
when { branch 'master' }
environment {
GIT_TAG = "jenkins-$BUILD_NUMBER"
}
steps {
sh('''
git config user.name 'my-ci-user'
git config user.email 'my-ci-user@users.noreply.github.example.com'
git tag -a \$GIT_TAG -m "[Jenkins CI] New Tag"
''')
sshagent(['my-ssh-credentials-id']) {
sh("""
#!/usr/bin/env bash
set +x
export GIT_SSH_COMMAND="ssh -oStrictHostKeyChecking=no"
git push origin \$GIT_TAG
""")
}
}
}
}
}
答案 1 :(得分:0)
这是将 Jenkins 凭据用于 Git 的另一个示例
withCredentials([usernamePassword(
credentialsId: gitCred,
passwordVariable: 'GIT_PASSWORD',
usernameVariable: 'GIT_USERNAME')]) {
sh '''
git config --global credential.username $GIT_USERNAME
git config --global credential.helper '!f() { echo password=$GIT_PASSWORD; }; f'
'''
}