我在jenkins中定义了一些秘密凭证,现在我在jenkins管道中使用它,如下所示:
pipeline {
agent {
node {
label 'jenkins-master'
}
}
environment {
GITHUB_USERNAME = credentials('github-username')
GITHUB_PASSWORD = credentials('github-password')
GITHUB_PASSWORD1 = credentials('github-password-1')
df = credentials('123')
}
stages {
stage('checkout-source') {
steps {
git credentialsId: 'gitlab-project-auth',
url: 'https://github.com/jiangxiaoqiang/jiangxiaoqiang.github.io.git'
}
}
stage('publish') {
steps{
sh "git config --global user.email \"jiangtingqiang@gmail.com\""
sh "git config --global user.name \"jiangxiaoqiang\""
sh "git add -A"
sh "git diff-index --quiet HEAD || git commit -m \"[docs] scheduled auto commit task\" || git push"
sh "echo ${GITHUB_USERNAME}"
sh "echo ${GITHUB_PASSWORD}"
sh "echo ${GITHUB_PASSWORD1}"
sh "echo ${df}"
sh "git push https://${GITHUB_USERNAME}:${GITHUB_PASSWORD}@github.com/jiangxiaoqiang/jiangxiaoqiang.github.io.git"
}
}
}
}
但是似乎只有第一个可行,这是构建日志输出:
> git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url https://github.com/****/xiaoqiang-blog-source.git # timeout=10
Fetching upstream changes from https://github.com/****/xiaoqiang-blog-source.git
> git --version # timeout=10
> git --version # 'git version 2.11.0'
> git fetch --tags --progress -- https://github.com/****/xiaoqiang-blog-source.git +refs/heads/*:refs/remotes/origin/* # timeout=10
> git rev-parse refs/remotes/origin/master^{commit} # timeout=10
> git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision d24abcbc136a3f050b9c1aa365bf30dcc6b77bb9 (refs/remotes/origin/master)
> git config core.sparsecheckout # timeout=10
> git checkout -f d24abcbc136a3f050b9c1aa365bf30dcc6b77bb9 # timeout=10
> git branch -a -v --no-abbrev # timeout=10
> git branch -D master # timeout=10
> git checkout -b master d24abcbc136a3f050b9c1aa365bf30dcc6b77bb9 # timeout=10
Commit message: "[docs] add jenkinsfiles"
> git rev-list --no-walk d24abcbc136a3f050b9c1aa365bf30dcc6b77bb9 # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (publish)
[Pipeline] sh
+ git config --global user.email jiangtingqiang@gmail.com
[Pipeline] sh
+ git config --global user.name ****
[Pipeline] sh
+ git add -A
[Pipeline] sh
+ git diff-index --quiet HEAD
[Pipeline] sh
+ echo ****
****
[Pipeline] sh
+ echo
[Pipeline] sh
+ git push https://****:@github.com/****/xiaoqiang-blog-source.git
remote: Invalid username or password.
fatal: Authentication failed for 'https://****:@github.com/****/xiaoqiang-blog-source.git/'
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withCredentials
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 128
Finished: FAILURE
我在配置凭据中缺少什么吗?
答案 0 :(得分:1)
您不能按原样使用jenkins凭证。
请查看this stackoverflow答案,说明如何从bash脚本运行推送。
答案 1 :(得分:1)
这里我没有回答如何使用git。我的答案仅关于詹金斯人的机密用法。
您使用了哪些秘密?密文?
使用机密-用户名和密码时,您可以像这样分别调用用户名和密码:
agent {
node {
label 'jenkins-master'
}
}
environment {
GITHUB_CRED = credentials('github-cred')
}
stages {
stage('publish') {
steps{
sh "echo ${GITHUB_CRED_USR}"
sh "echo ${GITHUB_CRED_PSW}"
sh "git push https://${GITHUB_CRED_USR}:${GITHUB_CRED_PSW}@github.com/jiangxiaoqiang/jiangxiaoqiang.github.io.git"
}
}
}
}
它也使您知道在全局或项目范围内将凭据存储在何处。