简体   繁体   中英

How to do Git & Docker tagging from a Jenkins multibranch pipeline

I have a Jenkinsfile (multi-branch pipeline) that's supposed to run every time somebody commits something to any branch in Jenkins.

My idea is that it triggers a build and every time the tests pass, it will tag the git repository and docker image with a new tag.

Currently every build, builds a Docker image called application:latest. It would be nice to implement some tagging system in both the Git repository and the Docker images.

So that my Github repo has 0.0.1, 0.0.2, 0.0.3 as tags. And that the Docker image is also pushed as application:0.0.1 to the Docker hub. Then also the latest tagged build, should not just be called application:0.0.3, but also application:latest.

Any ideas how I can implement such a system with Jenkins in Github?

This is my current 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
        */
      }
    }
  }
}

are you using maven or gradle ? because i had the same problem and i fixed the version uppgrad by adding a script that generates the dockerfile from a template. you can check my github project for the groovy script

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM