Jenkins docker在阶段完成后删除生成的文件

时间:2020-09-10 03:34:58

标签: docker jenkins jenkins-pipeline

我正在构建一个jenkins管道,试图将zeeplin / jupyter笔记本jsons转换为md文件。我需要安装nb2md才能将json转换为md文件,因此我将docker image用于转换文件的阶段之一,但是一旦该阶段完成,docker就会删除所有文件。我想拿这个文件并在下一阶段提交到git,因为文件不见了,我无法做到这一点。我也将docker的git commit步骤移到了docker,但是后来我在docker上找不到git。以下是我的詹金斯文件:

void setBuildStatus(String message, String state) {
  step([
      $class: "GitHubCommitStatusSetter",
      reposSource: [$class: "ManuallyEnteredRepositorySource", url: “$git_url”],
      contextSource: [$class: "ManuallyEnteredCommitContextSource", context: "ci/jenkins/build-status"],
      errorHandlers: [[$class: "ChangingBuildStatusErrorHandler", result: "UNSTABLE"]],
      statusResultSource: [ $class: "ConditionalStatusResultSource", results: [[$class: "AnyBuildResult", message: message, state: state]] ]
  ]);
}
pipeline {
    agent none
    environment {
        PATH = '/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/bin'
    }
    parameters {
        string(name: 'files_list', defaultValue: '')
    }
    stages {
       stage('Convert Zeppelin/Jupyter json to md files'){
           agent {
              docker {
                image 'python:3-alpine'
                args  '--network=host'
                }
            }
            steps {
                withEnv(["HOME=${env.WORKSPACE}"]) {
                    sh '''pip3 install --target=/home/jenkins/workspace -U nb2md'''
                }
                withCredentials([
                  sshUserPrivateKey(
                    credentialsId: "github-sec-dms-dataops-ssh-key",
                    keyFileVariable: 'keyfile')
            ]){
                sh'''
                #!/bin/bash
                dir=$(pwd)
                sh $dir/jupyter-zeppelin-json-to-md-files.sh
            '''
            }
          }
        }
        stage ('Git commit'){
            agent any
            steps {
             withCredentials([
                  sshUserPrivateKey(
                    credentialsId: "github-sec-dms-dataops-ssh-key",
                    keyFileVariable: 'keyfile')
            ]){
                sh '''
                #!/bin/sh
                 eval `ssh-agent -s`
                 ssh-add $keyfile
                 git config --global user.email “user_id”
                 git config --global user.name "git"
                 git add .
                 echo "Committing files to git hub ... "
                 git commit -am "Committing markdown files.."
                 echo "Pushing code to github ..."
                 git push -u origin HEAD
                 git diff --exit-code'
                 '''}
           }
        }
    }
    post {
        success {
            node('jnlp') {
                setBuildStatus("Build succeeded", "SUCCESS");
            }
        }
        failure {
            node('jnlp') {
                setBuildStatus("Build failed", "FAILURE");
            }
        }
    }
}


如何将第一阶段生成的文件用于第二阶段?

1 个答案:

答案 0 :(得分:2)

使用archiveArtifacts在步骤之间保留文件。在docker步骤上使用args安装主机文件夹。

docker {
    image 'python:3-alpine'
    args  '--network=host -v <host_folder>:<container_folder>'
}

post {
    always {
        archiveArtifacts artifacts: 'generatedFile.txt', onlyIfSuccessful: true
    }
}