簡體   English   中英

無法從 jenkins 管道在 docker 中運行 python 腳本

[英]Unable to run a python script inside docker from jenkins pipeline

我創建了一個可以使用 pip 安裝的 python 包(用於在 jenkins 中自動化構建階段)。 我還創建了一個 dockerfile ,它將從 github 克隆 python 包,執行 pip install 並導出可執行文件(入口點,但是你想調用它)安裝在容器中的路徑(例如:~/.local/斌)。 這就是我的 dockerfile 的樣子。

FROM ros:melodic-ros-core-stretch
RUN apt-get update && apt-get -y install python-pip
RUN git clone <private-repo-with-personal-access-token>
RUN pip install <package-name>
RUN export PATH=~/.local/bin:$PATH 

所以我構建了這個鏡像,運行了容器並輸入了一個打印 hello world 的可執行文件(入口點)。 工作得很好。 沒有任何問題。 從全局來看,我想在 jenkins 管道中調用這個可執行文件(入口點)。 我最初遇到了這個問題,然后我了解到 jenkins 管道在使用 docker 映像設置時,在容器頂部運行,但使用為 jenkins 分配的工作區,即 /var/lib/jenkins/workspace。 這就是我的 jenkins 管道腳本的樣子

pipeline {
    agent {
         docker {
              args '--network host -u root:root'
              image '<private-docker-hub-image>'
              registryCredentialsId 'docker-credentials'
              registryUrl 'https://registry.hub.docker.com'
         }
    }
    stages {
        stage('Test') {
            steps {
                sh 'test-build'
            }
        }
    }
}

這是我得到的錯誤。

Started by user Automated Build Environment
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/First_item
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] withDockerRegistry
$ docker login -u <docker username> -p ******** https://registry.hub.docker.com
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /var/lib/jenkins/workspace/First_item@tmp/251189be-62eb-4134-84ca-d70190ab080f/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[Pipeline] {
[Pipeline] sh
+ docker inspect -f . <private-dockerhub-image>

Error: No such object: <private-dockerhub-image>
[Pipeline] sh
+ docker inspect -f . registry.hub.docker.com/<private-dockerhub-image>
.
[Pipeline] withDockerContainer
Jenkins does not seem to be running inside a container
$ docker run -t -d -u 125:130 --network host -u root:root -v /var/lib/jenkins:/var/lib/jenkins -w /var/lib/jenkins/workspace/First_item -v /var/lib/jenkins/workspace/First_item:/var/lib/jenkins/workspace/First_item:rw,z -v /var/lib/jenkins/workspace/First_item@tmp:/var/lib/jenkins/workspace/First_item@tmp:rw,z -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** registry.hub.docker.com/<private-dockerhub-image> cat
$ docker top ab1f2ec9b503f9b916ee96943dc849acc90716f7543b4841ff3901b7a65aea54 -eo pid,comm
[Pipeline] {
[Pipeline] stage (hide)
[Pipeline] { (Test)
[Pipeline] sh
+ test-build
/var/lib/jenkins/workspace/First_item@tmp/durable-7cb108f7/script.sh: 1: /var/lib/jenkins/workspace/First_item@tmp/durable-7cb108f7/script.sh: test-build: not found
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
$ docker stop --time=1 ab1f2ec9b503f9b916ee96943dc849acc90716f7543b4841ff3901b7a65aea54
$ docker rm -f ab1f2ec9b503f9b916ee96943dc849acc90716f7543b4841ff3901b7a65aea54
[Pipeline] // withDockerContainer
[Pipeline] }
[Pipeline] // withDockerRegistry
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 127
Finished: FAILURE

我的問題是 rosdep、bloom-generate 和其他幾個腳本(知道這些腳本作為機器人操作系統中 ~/.local/bin 中另一個 pip 包的一部分安裝就足夠了)在管道中如何工作,而不是腳本我通過pip安裝的。 我真的很感激能幫助我指出我所缺少的東西。

謝謝你。 非常感謝幫助。

對不起。 解決方案非常簡單。 我犯的錯誤是我更新了 dockerfile 並創建了一個新映像,將其推送到 dockerhub,但忘記在 Jenkins master 運行的實例中修剪和更新最新副本。 所以基本上我的 dockerhub 只能說版本。 101 和我創建 dockerfile 和 docker 鏡像的本地機器有版本。 101. 但是 Jenkins master 在 v.52 中。 因此,即使我進行了更改,更新后的圖像也沒有被 Jenkins 大師拉取,你去吧! 這對我來說是一個愚蠢的錯誤。 我感謝你們在這里幫助我的每一個人。 和平!

您能否解釋一下“但在運行Jenkins master的實例中忘記修剪和更新最新副本”是什么意思。 我遇到同樣的問題,並在MacOS節點中運行我的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM