如何 通过 Jenkinsfile 管道 使用 Git 提交 哈希 自动 标记 Docker 映像

whhtz7ly  于 2022-11-21  发布在  Jenkins
关注(0)|答案(2)|浏览(178)

我有一个Jenkins示例,我需要使用Jenkinsfile管道来自动标记Docker Image,Jenkinsfile管道会自动用提交哈希值标记图像,然后将其推送到Docker Repository。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}和下面的块,编译就可以工作,但它只会推送最新的。

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图像。我该如何完成呢?

brgchamk

brgchamk1#

你可以用$GIT_COMMIT这样的

sh "docker build -f Dockerfile.mover -t mirantiseng/lodestone-mover:${GIT_COMMIT} ."
weylhg0b

weylhg0b2#

所以我所要做的就是改变tag的定义方式:

pipeline {
    agent any
    environment {
        tag = sh(returnStdout: true, script: "git rev-parse --short=10 HEAD").trim()
    }

    stages {
        stage('Build core lodestone image') {
            steps {
                // TODO: proper tagging
                sh "docker build -f Dockerfile.lodestone -t mirantiseng/lodestone:${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:${tag}"
                }
            }
        }
        stage('Build core lodestone-comment image') {
            steps {
                // TODO: proper tagging
                sh "docker build -f Dockerfile -t mirantiseng/lodestone-comment:${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:${tag}"
                }
            }
        }
        stage('Build core lodestone-mover image') {
            steps {
                // TODO: proper tagging
                sh "docker build -f Dockerfile.mover -t mirantiseng/lodestone-mover:${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:${tag}"
                }
            }
        }
    }
}

主要的变化是

tag = sh(returnStdout: true, script: "git rev-parse --short=10 HEAD").trim()`

相关问题