【问题标题】:How to do Git & Docker tagging from a Jenkins multibranch pipeline如何从 Jenkins 多分支管道进行 Git 和 Docker 标记
【发布时间】:2019-08-23 13:20:52
【问题描述】:

我有一个 Jenkinsfile(多分支管道),每当有人向 Jenkins 中的任何分支提交某些内容时,它就会运行。

我的想法是它触发构建,每次测试通过时,它都会用新标签标记 git 存储库和 docker 映像。

目前,每次构建都会构建一个名为 application:latest 的 Docker 镜像。最好在 Git 存储库和 Docker 镜像中实现一些标记系统。

这样我的 Github 存储库就有 0.0.1、0.0.2、0.0.3 作为标签。并且 Docker 映像也作为 application:0.0.1 推送到 Docker hub。 然后还有最新的标记构建,不应仅称为 application:0.0.3,还应称为 application:latest。

有什么想法可以在 Github 中使用 Jenkins 实现这样的系统吗?

这是我当前的 Jenkinsfile:

pipeline {
  agent any
  options {
    buildDiscarder(logRotator(numToKeepStr: '10', artifactNumToKeepStr: '10'))
  }
  environment {
    DOCKER_CREDS = credentials('dockeruser-dockerhub-password')
  }
  stages {
    stage('Git clone') {
      /*
      This is needed for local development, because Jenkins uses locally pasted pipeline code in a textarea box and doesn't know where the Git repo is.
      This also means we have no multibranch, but that's no problem for local development.
      */
      steps {
        git url: 'https://github.com/gituser/denpal', branch: 'feature/Jenkinsfile'
      }
    }
    stage('Docker login') {
      steps {
        sh """
        docker login --username dockeruser --password $DOCKER_CREDS
        """
      }
    }
    stage('Docker-compose') {
      steps {
        sh '''
        docker-compose config -q
        COMPOSE_PROJECT_NAME=denpal docker-compose down
        COMPOSE_PROJECT_NAME=denpal docker-compose up -d --build "$@"
        '''
      }
    }
    stage('Docker push images') {
      steps {
        sh """
        docker tag denpal:latest dockername/denpal:latest
        docker push dockername/denpal:latest
        docker tag denpal_nginx:latest dockername/denpal_nginx:latest
        docker push dockername/denpal_nginx:latest
        docker tag denpal_php:latest dockername/denpal_php:latest
        docker push dockername/denpal_php:latest
        """
      }
    }
    stage('Verification tests') {
      steps {
        sh """
        docker-compose exec -T cli drush status
        """
        /*
        make this work, syntax error, """-issue?
        if [ $? -eq 0 ]; then
          echo "OK!"
        else
          echo "FAIL"
        fi
        */
      }
    }
  }
}

【问题讨论】:

  • 为什么不重新标记图像,然后使用latest 标签再次推送?此外,可以使用 Docker Pipeline 插件显着清理此管道。

标签: git docker jenkins github


【解决方案1】:

您使用的是 maven 还是 gradle ?因为我有同样的问题,我通过添加一个从模板生成 dockerfile 的脚本来修复版本 uppgrad。 你可以check my github project for the groovy script

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多