简体   繁体   English

如何在jenkins管道的docker容器中运行python脚本

[英]How to run a python script in a docker container in jenkins pipeline

I have a docker image in dockerhub that I want to add as an agent in my jenkins pipeline script. 我在dockerhub中有一个Docker映像,我想作为代理添加到我的jenkins管道脚本中。 As a part of the image, I perform git clone to fetch a repository from github that has multiple python scripts which corresponds to multiple stages in my jenkins pipeline. 作为图像的一部分,我执行git clone从github获取一个存储库,该存储库具有多个python脚本,这些脚本对应于我的jenkins管道中的多个阶段。 I tried searching everywhere but I'm not able to find relevant information that talks about how to access the files inside a docker container in jenkins pipeline. 我尝试在任何地方搜索,但是找不到有关如何访问jenkins管道中docker容器内文件的相关信息。

I'm running jenkins on a VM and it has docker installed. 我在VM上运行jenkins,并且已安装docker。 The pipeline performs a build on a docker container. 管道在docker容器上执行构建。 Since there are many steps involved in every single stage of the pipeline, I tried to use python API as much as possible. 由于管道的每个阶段都涉及很多步骤,因此我尝试尽可能多地使用python API。

This is how my dockerfile looks like and the image builds successfully and I'm able to host it in dockerhub. 这就是我的dockerfile的样子,并且图像成功构建,并且能够将其托管在dockerhub中。 When I run the container, I'm able to see "jenkins_pipeline_scripts" directory which contains all the necessary python scripts for the pipeline stages. 运行容器时,我可以看到“ jenkins_pipeline_scripts”目录,其中包含管道阶段所需的所有python脚本。

FROM ros:melodic-ros-core-stretch

RUN apt-get update && apt-get -y install python-pip

RUN git clone <private-repo with token>

This is how my current jenkins pipeline script looks like. 这就是我当前的jenkins管道脚本的样子。

pipeline {
    agent {
        docker {
            image '<image name>'
            registryUrl 'https://registry.hub.docker.com'
            registryCredentialsId 'docker-credentials'
            args '--network host -u root:root'
        }
    }

    stages {
        stage('Test') {
            steps {
                sh 'python jenkins_pipeline_scripts/scripts/test.py'   
            }
        }
    }
}

This is the error I'm getting when I execute the job. 这是我执行作业时遇到的错误。

$ docker top 05587cd75db5c4282b86b2f1ded2c43a0f4eae161d6c7d7c03d065b0d45e1 -eo pid,comm
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] sh
+ python jenkins_pipeline_scripts/scripts/test.py
python: can't open file 'jenkins_pipeline_scripts/scripts/test.py': [Errno 2] No such file or directory

When Jenkins Pipeline to launch the agent container, it will change the container's WORKDIR via -w option and mount the Jenkins job's workspace folder via -v option. 当Jenkins Pipeline启动代理容器时,它将通过-w选项更改容器的WORKDIR ,并通过-v选项安装Jenkins作业的工作空间文件夹。

As a result of both options, the Jenkins job's workspace folder will becomes your container's WORKDIR . 由于这两个选项,Jenkins作业的工作空间文件夹将成为容器的WORKDIR

Following is my jenkins job console output: 以下是我的詹金斯工作控制台输出:

docker run -t -d -u 29001:100 
-w /bld/workspace/test/agent-poc 
-v /bld/workspace/test/agent-poc:/bld/workspace/test/agent-poc:rw,z 
-v /bld/workspace/test/agent-poc@tmp:/bld/workspace/test/agent-poc@tmp:rw,z 
-e ******** -e ******** -e ******** -e ******** 
docker.hub.com/busybox cat

You clone the code when build the image and they are not inside the WORKDIR , thus reports no such file error. 您在构建映像时克隆了代码,它们不在WORKDIR ,因此不会报告此类文件错误。

Two approaches to fix your issue. 两种解决问题的方法。

1) cd your code folder at firstly, you should know that path. 1)首先将您的代码文件夹cd,您应该知道该路径。

    stage('Test') {
        steps {
            sh '''
              cd <your code folder in container>
              python jenkins_pipeline_scripts/scripts/test.py
            '''   
        }
    } 

2) move git clone code repo from Dockerfile into pipeline stage 2)将git clone代码仓库从Dockerfile移到管道阶段

As I explained at begin, your job's workspace will become container's WORKDIR , thus you can clone your code into jenkins job workspace via pipeline step, then you no need to cd <your code folder in container> . 正如我在开始时所解释的那样,您的作业的工作空间将成为容器的WORKDIR ,因此您可以通过管道步骤将代码克隆到jenkins作业工作空间中,而无需cd <your code folder in container>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM