Search code examples
node.jsdockerjenkinsgroovyjenkins-pipeline

how to write the correct pipline jenkins docker grovy node


I am rewriting my pipline in node, I need to understand how to perform a step with a gait in node now an error is coming from stage('Deploy')

node {
checkout scm


def customImage = docker.build("python-web-tests:${env.BUILD_ID}")

customImage.inside {
    sh "python ${env.CMD_PARAMS}"
}
stage('Deploy') {
        post {
        always {
            allure([
                includeProperties: false,
                jdk: '',
                properties: [],
                reportBuildPolicy: 'ALWAYS',
                results: [[path: 'report']]
            ])

            cleanWs()
        }
   }
}

and this is the old pipeline

pipeline {
agent {label "slave_first"}

stages {
 stage("Создание контейнера image") {
    steps {
    catchError {
       script {
              docker.build("python-web-tests:${env.BUILD_ID}", "-f Dockerfile .")
         }
      }
   }
}
    stage("Running and debugging the test") {
        steps {
            sh 'ls'
            sh 'docker run --rm -e REGION=${REGION} -e DATA=${DATA} -e BUILD_DESCRIPTION=${BUILD_URL} -v ${WORKSPACE}:/tmp python-web-tests:${BUILD_ID} /bin/bash -c "python ${CMD_PARAMS} || exit_code=$?; chmod -R 777 /tmp; exit $exit_code"'
        }
    }
}

post {
    always {
        allure([
            includeProperties: false,
            jdk: '',
            properties: [],
            reportBuildPolicy: 'ALWAYS',
            results: [[path: 'report']]
        ])

        cleanWs()
    }
}
}

I tried to transfer the method of creating an allure report, but nothing worked, I use the version above, almost everything turned out, you can still add environment variables to the build, for example, those that are specified -e DATA=${DATA} how do I add it


Solution

  • I don't recommend to switch from declarative to scriptive pipeline. You are losing possibility to use multiple tooling connected with declarative approach like syntax checkers.

    If you still want to use scriptive approach try this:

    node('slave_first') {
       stage('Build') {
          checkout scm
          def customImage = docker.build("python-web-tests:${env.BUILD_ID}")
          customImage.inside {
             sh "python ${env.CMD_PARAMS}"
          }
       }
       stage('Deploy') {
           allure([
               includeProperties: false,
               jdk: '',
               properties: [],
               reportBuildPolicy: 'ALWAYS',
               results: [[path: 'report']]])
           cleanWs()
       }
    }
    

    There is no post and always directive in scriptive pipelines. It's on your head to catch all exceptions and set status of the job. I guess you were using this page: https://www.jenkins.io/doc/book/pipeline/syntax/, but it's a mistake. This page only refers to declarative approach and in few cases you have hidden scriptive code as examples. Also i don't know if you have default agent label set in your Jenkins config, but by looking at your declarative one I think you missed 'slave_first' arg in node object.

    those that are specified -e DATA=${DATA} how do I add it

    That's a docker question not a Jenkins. If you want to launch docker image and then also have access to some reports located in this container you should mount workspace/file where those output files landed. You should also pass location of those files to allure. I suggest you to try this:

    • mount some subfolder in workspace to docker container
    • cat test report file if it's visible
    • add allure report with passing this file location to allure step