首页 > 解决方案 > 如何从 Jenkins 多分支管道进行 Git 和 Docker 标记

问题描述

我有一个 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
        */
      }
    }
  }
}

标签: gitdockerjenkinsgithub

解决方案


你在使用 maven 还是 gradle ?因为我有同样的问题,我通过添加一个从模板生成 dockerfile 的脚本来修复版本 uppgrad。你可以查看我的 github 项目中的 groovy 脚本


推荐阅读