所以我有一个 Jenkins 实例,我需要使用 Jenkinsfile Pipeline 自动标记 Docker 镜像,该管道自动使用提交哈希标记镜像,然后将其推送到 Docker 存储库。 Jenkins 配置正确,但我的管道仍然失败。首先,我尝试使用以下命令,该命令返回我的存储库的当前提交哈希。
git rev-parse --short=10 HEAD
然后我注意到这返回了不止一行,所以我开始使用这个:
git rev-parse --short=10 HEAD | tail -n +2
这会返回类似于以下内容的提交哈希:
338fcaa318b17c40dacf81dcf7a5826e3e3f0160
我的目标是使用 Jenkinsfile 使用此哈希标记我的 docker 镜像:
pipeline {
agent any
environment {
tag = sh(returnStdout: true, script: "git rev-parse -short=10 HEAD | tail -n +2")
}
stages {
stage('Build core lodestone image') {
steps {
// TODO: proper tagging
sh "docker build -f Dockerfile.lodestone -t mirantiseng/lodestone:${var.tag} ."
withCredentials([usernamePassword(credentialsId: 'common-dockerhub-up', usernameVariable: 'HUB_USER', passwordVariable: 'HUB_PASS')]) {
sh "docker login -u ${HUB_USER} -p ${HUB_PASS} && docker push mirantiseng/lodestone:${var.tag}"
}
}
}
stage('Build core lodestone-comment image') {
steps {
// TODO: proper tagging
sh "docker build -f Dockerfile -t mirantiseng/lodestone-comment:${var.tag} ."
withCredentials([usernamePassword(credentialsId: 'common-dockerhub-up', usernameVariable: 'HUB_USER', passwordVariable: 'HUB_PASS')]) {
sh "docker login -u ${HUB_USER} -p ${HUB_PASS} && docker push mirantiseng/lodestone-comment:${var.tag}"
}
}
}
stage('Build core lodestone-mover image') {
steps {
// TODO: proper tagging
sh "docker build -f Dockerfile.mover -t mirantiseng/lodestone-mover:${var.tag} ."
withCredentials([usernamePassword(credentialsId: 'common-dockerhub-up', usernameVariable: 'HUB_USER', passwordVariable: 'HUB_PASS')]) {
sh "docker login -u ${HUB_USER} -p ${HUB_PASS} && docker push mirantiseng/lodestone-mover:${var.tag}"
}
}
}
}
}
如果我取出:${var.tag}
,构建就可以工作。和下面的 block ,但它只是推送最新的。这使得工作文件看起来像这样:
pipeline {
agent any
stages {
stage('Build core lodestone image') {
steps {
// TODO: proper tagging
sh "docker build -f Dockerfile.lodestone -t mirantiseng/lodestone ."
withCredentials([usernamePassword(credentialsId: 'common-dockerhub-up', usernameVariable: 'HUB_USER', passwordVariable: 'HUB_PASS')]) {
sh "docker login -u ${HUB_USER} -p ${HUB_PASS} && docker push mirantiseng/lodestone"
}
}
}
stage('Build core lodestone-comment image') {
steps {
// TODO: proper tagging
sh "docker build -f Dockerfile -t mirantiseng/lodestone-comment ."
withCredentials([usernamePassword(credentialsId: 'common-dockerhub-up', usernameVariable: 'HUB_USER', passwordVariable: 'HUB_PASS')]) {
sh "docker login -u ${HUB_USER} -p ${HUB_PASS} && docker push mirantiseng/lodestone-comment"
}
}
}
stage('Build core lodestone-mover image') {
steps {
// TODO: proper tagging
sh "docker build -f Dockerfile.mover -t mirantiseng/lodestone-mover ."
withCredentials([usernamePassword(credentialsId: 'common-dockerhub-up', usernameVariable: 'HUB_USER', passwordVariable: 'HUB_PASS')]) {
sh "docker login -u ${HUB_USER} -p ${HUB_PASS} && docker push mirantiseng/lodestone-mover"
}
}
}
}
}
但我需要标记 docker 镜像。我阅读了 Jenkinsfile,它说我可以使用 environment {<something>}
创建一个环境变量设置全局变量。我有一个名为 tag
的变量我想实现用提交哈希来标记 docker 镜像。我怎样才能做到这一点?
最佳答案
您可以使用像这样的$GIT_COMMIT
sh "docker build -f Dockerfile.mover -t mirantiseng/lodestone-mover:${GIT_COMMIT} ."
关于docker - 如何通过 Jenkinsfile 管道使用 Git 提交哈希自动标记 Docker 镜像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68908314/