如何从Jenkins多分支管道进行Git和Docker标记

时间:2019-04-02 09:13:07

标签: git docker jenkins github

我有一个Jenkinsfile(多分支管道),每当有人向Jenkins中的任何分支提交内容时,该Jenkinsfile都应该运行。

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

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

因此我的Github存储库具有标签0.0.1、0.0.2、0.0.3。而且Docker映像也作为application:0.0.1被推送到Docker中心。 然后,最新的带标签的版本不仅应被称为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
        */
      }
    }
  }
}

1 个答案:

答案 0 :(得分:0)

您正在使用Maven或Gradle吗?因为我有同样的问题,并且我通过添加从模板生成dockerfile的脚本来修复了uppgrad版本。 您可以check my github project for the groovy script