0

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.

1 Answer 1

2

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: 
'[email protected]']]])
        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://[email protected]']]])
        }
      }
    }
    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}'
      }
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.