Home > Blockchain >  Jenkins Pipeline: running steps on different docker host
Jenkins Pipeline: running steps on different docker host

Time:06-10

I have just set up docker build slaves on my jenkins server according to this article to move away for static build nodes.

I have run into a minor issue. I use bitbucket and bcbsn to update the build status. I'd prefer not to install go and maintain that in all my build slaves, so I considerd using the docker image from bcbsn like this

docker run utrecht/bcbsn:2.0.4 -clientID ${clientID} -clientSecret ${clientSecret} -state FAILED -commit ${env.GIT_COMMIT} -name ${buildName} -repoSlug talentindicator -owner talentinsights -url ${env.JOB_URL} -key ${buildName}

I have read through this article on running docker inside docker and all the pros and cons, coming to the conclusion that I would prefer not to go this way.

Therefore I would like that to run either on the main node or just on the docker host I have set up for the build slaves. Is this possible? And if so, how to do it?

my own attempt

stages {
        stage ('Staging'){
            steps {
                withCredentials([usernamePassword(credentialsId: '6eab2fcc-7703-4d92-9e7d-120d66f771b3  ', usernameVariable: 'clientID', passwordVariable: 'clientSecret')]) {
                    docker.withServer('tcp://192.168.0.247:4243') {
                        docker.image('utrecht/bcbsn:2.0.4').withRun("-clientID ${clientID} -clientSecret ${clientSecret} -state INPROGRESS -commit ${env.GIT_COMMIT} -name ${buildName} -repoSlug talentindicator -owner talentinsights -url ${env.JOB_URL} -key ${buildName}") {
                            /* do nothing */
                        }
                    }
                }
            }
        }

this results in

Method calls on objects not allowed outside "script" blocks.

Snippets from my Pipeline where I use bcbsn:

in my initial build step:

stages {
        stage ('Staging'){
            steps {
                withCredentials([usernamePassword(credentialsId: '00000000-0000-0000-0000-000000000000', usernameVariable: 'clientID', passwordVariable: 'clientSecret')]) {
                    sh "docker run utrecht/bcbsn:2.0.4 -clientID ${clientID} -clientSecret ${clientSecret} -state INPROGRESS -commit ${env.GIT_COMMIT} -name ${buildName} -repoSlug talentindicator -owner talentinsights -url ${env.JOB_URL} -key ${buildName}"
                }
           }
     }

And in my post section:

post {
        //https://jenkins.io/doc/pipeline/tour/post/
        changed {
            script {
                hasChanged = true
            }
        }
        success {
            script {
                if(hasChanged){
                    echo '************** First success in a while! '
                }
            }
            withCredentials([usernamePassword(credentialsId: '00000000-0000-0000-0000-000000000000', usernameVariable: 'clientID', passwordVariable: 'clientSecret')]) {
                    sh "docker run utrecht/bcbsn:2.0.4 -clientID ${clientID} -clientSecret ${clientSecret} -state SUCCESSFUL -commit ${env.GIT_COMMIT} -name ${buildName} -repoSlug talentindicator -owner talentinsights -url ${env.JOB_URL} -key ${buildName}"
                }
        }
        failure {
            script {
                if(hasChanged){
                    echo '************** Build broke '
                }
            }
            echo "Setting build status in Bitbucket"
            withCredentials([usernamePassword(credentialsId: '00000000-0000-0000-0000-000000000000', usernameVariable: 'clientID', passwordVariable: 'clientSecret')]) {
                    sh "docker run utrecht/bcbsn:2.0.4 -clientID ${clientID} -clientSecret ${clientSecret} -state FAILED -commit ${env.GIT_COMMIT} -name ${buildName} -repoSlug talentindicator -owner talentinsights -url ${env.JOB_URL} -key ${buildName}"
                }
        }
    }

CodePudding user response:

If you want to use the global variable methods that interact with the Jenkins Pipeline plugin for Docker classes, then you need to nested those inside script blocks when using declarative pipeline syntax:

stage ('Staging'){
  steps {
    script {
      withCredentials([usernamePassword(credentialsId: '6eab2fcc-7703-4d92-9e7d-120d66f771b3  ', usernameVariable: 'clientID', passwordVariable: 'clientSecret')]) {
        docker.withServer('tcp://192.168.0.247:4243') {
          docker.image('utrecht/bcbsn:2.0.4').withRun("-clientID ${clientID} -clientSecret ${clientSecret} -state INPROGRESS -commit ${env.GIT_COMMIT} -name ${buildName} -repoSlug talentindicator -owner talentinsights -url ${env.JOB_URL} -key ${buildName}") {
            /* do nothing */
          }
        }
      }
    }
  }
}
  • Related