Answers: <p>I have found a solution so I'll leave it here for others.</p>
  
  <p>using docker </p>
  
  <pre><code>#!/usr/bin/env groovy
  
  pipeline{
  agent buildsvr
  
    stages{
      stage('Checkout and build Docker Source'){
        steps{
          checkout([
            $class: 'GitSCM',
            branches: [[name: '*/master']],
            doGenerateSubmoduleConfigurations: false,
            submoduleCfg: [],
            userRemoteConfigs: [[
              credentialsId: 'asdf787c-5fd4-4dfb-81ad-873jhf107223',
              url: 
  'git@mydockersourcerepo.git']]])
          sh 'pushd docker &amp;&amp; docker build -t ${image}:latest . &amp;&amp; popd '
        }
      }
      stage('Checkout Target Source'){
        steps{
          script{
            checkout([
              $class: 'GitSCM',
              branches: [[name: 'mybranch']],
              doGenerateSubmoduleConfigurations: false,
              extensions: [[
                $class: 'SubmoduleOption',
                disableSubmodules: false,
                parentCredentials: false,
                recursiveSubmodules: true, reference: '',
                trackingSubmodules: true
                relativeTargetDir: 'source']],
              submoduleCfg: [],
              userRemoteConfigs: [[
                credentialsId: 'Bitbucket-Jenkins',
                url: 'ssh://git@sourcerepo.git']]])
          }
        }
      }
      stage('Build Source Target'){
        steps{
         script{
            sh 'docker-compose up'
          }
        }
      }
    }
    post{
      success{
        script{
          sh 'docker tag ${image} 10.90.239.223:5000:/${image} &amp;&amp; docker push 
  10.90.239.223:5000:/${image}'
        }
      }
    }
  }
  </code></pre>
  
Answers: CC BY-SA 3.0
Answers: 2018-02-26T22:59:32.107Z
Answers: 2018-02-26T22:59:32.107Z
Answers: scott
Body: <p>I'm trying to figure out the proper way to get build environments into a CI pipeline.</p>
  
  <p>What I want to achieve is:</p>
  
  <ol>
  <li>someone pushes to the docker source repo (not registry, a repo with the docker folder)</li>
  <li>jenkins kicks off a build of that docker image</li>
  <li>if successful docker then goes and runs a software build using the newly built docker image</li>
  <li>if successful the docker image is tagged and pushed to the docker registry</li>
  </ol>
  
  <p>All I have so far is a Jenkins file that has four stages</p>
  
  <ol>
  <li>checks out out docker source repo</li>
  <li>builds docker image from source</li>
  <li>Checks out software source code (code that builds in the docker environment)</li>
  <li>builds software source code in docker container</li>
  <li>If successful tags and pushes new image to docker registery</li>
  </ol>
  
  <p>My main concern is everything I'm doing is going to be in the 'script' blocks, except the git checkouts, and it feels super hacky to be building and kicking off docker builds with jenkins using bash...</p>
  
  <p>are there any tools/already defined procedures to do things like this?  Is this the best way to do it?  I am looking for a best practice approach as this will be put into production and I want it to be as resilient and long lasting as possible.</p>
  
Tags: <docker><jenkins><jenkins-pipeline><docker-compose>
Title: Using Jenkins to control docker image build flow
UserDisplayName: scott
ThreadText: # Using Jenkins to control docker image build flow
  
  (Asked by: username_0 on 2018-02-24)
  
  I'm trying to figure out the proper way to get build environments into a CI pipeline.
  
  What I want to achieve is:
  
  1. someone pushes to the docker source repo (not registry, a repo with the docker folder)
  2. jenkins kicks off a build of that docker image
  3. if successful docker then goes and runs a software build using the newly built docker image
  4. if successful the docker image is tagged and pushed to the docker registry
  
  All I have so far is a Jenkins file that has four stages
  
  1. checks out out docker source repo
  2. builds docker image from source
  3. Checks out software source code (code that builds in the docker environment)
  4. builds software source code in docker container
  5. If successful tags and pushes new image to docker registery
  
  My main concern is everything I'm doing is going to be in the 'script' blocks, except the git checkouts, and it feels super hacky to be building and kicking off docker builds with jenkins using bash...
  
  are there any tools/already defined procedures to do things like this? Is this the best way to do it? I am looking for a best practice approach as this will be put into production and I want it to be as resilient and long lasting as possible.
  
  **Tags:** docker, jenkins, jenkins-pipeline, docker-compose
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2018-02-26. Score: 2 | Accepted answer
  
  I have found a solution so I'll leave it here for others.
  
  using docker
  
  ```
  #!/usr/bin/env groovy
  
  pipeline{
  agent buildsvr
  
    stages{
      stage('Checkout and build Docker Source'){
        steps{
          checkout([
            $class: 'GitSCM',
            branches: [[name: '*/master']],
            doGenerateSubmoduleConfigurations: false,
            submoduleCfg: [],
            userRemoteConfigs: [[
              credentialsId: 'asdf787c-5fd4-4dfb-81ad-873jhf107223',
              url: 
  'git@mydockersourcerepo.git']]])
          sh 'pushd docker && docker build -t ${image}:latest . && popd '
        }
      }
      stage('Checkout Target Source'){
        steps{
          script{
            checkout([
              $class: 'GitSCM',
              branches: [[name: 'mybranch']],
              doGenerateSubmoduleConfigurations: false,
              extensions: [[
                $class: 'SubmoduleOption',
                disableSubmodules: false,
                parentCredentials: false,
                recursiveSubmodules: true, reference: '',
                trackingSubmodules: true
                relativeTargetDir: 'source']],
              submoduleCfg: [],
              userRemoteConfigs: [[
                credentialsId: 'Bitbucket-Jenkins',
                url: 'ssh://git@sourcerepo.git']]])
          }
        }
      }
      stage('Build Source Target'){
        steps{
         script{
            sh 'docker-compose up'
          }
        }
      }
    }
    post{
      success{
        script{
          sh 'docker tag ${image} 10.90.239.223:5000:/${image} && docker push 
  10.90.239.223:5000:/${image}'
        }
      }
    }
  }
  
  ```
  
  
  ---
  
  