首页 > 解决方案 > 如何使用 Jenkins 在 docker 容器中构建 github 项目?

问题描述

我想在 Jenkins 每次收到来自 github 的提交推送时触发 github 项目的构建,然后使用我指定的 docker 映像在 docker 容器内构建项目。

目前,我有如下 Jenkinsfile:

pipeline {
    agent {
        docker { image 'docker-image' }
    }
    stages {
        stage('Build') {
            steps {
                sh "source build/envsetup.sh"
                sh "lunch astar_parrot-tina"
                sh "make -j"
                sh "pack -d"
            }
        }
    }
}

并出现以下错误:

Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] node
Running on Jenkins in /root/.jenkins/workspace/tina-pipeline
[Pipeline] {
[Pipeline] sh
[tina-pipeline] Running shell script
+ docker inspect -f . docker-image
.
[Pipeline] withDockerContainer
Jenkins does not seem to be running inside a container
$ docker run -t -d -u 0:0 -w /root/.jenkins/workspace/tina-pipeline -v /root/.jenkins/workspace/tina-pipeline:/root/.jenkins/workspace/tina-pipeline:rw,z -v /root/.jenkins/workspace/tina-pipeline@tmp:/root/.jenkins/workspace/tina-pipeline@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 ******** zequanhannto/tina cat
$ docker top 459169f2aea2499f4c361291733308fafa74ec63e08b7a1b74814e0bb9077d51 -eo pid,comm
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] sh
[tina-pipeline] Running shell script
+ source build/envsetup.sh
/root/.jenkins/workspace/tina-pipeline@tmp/durable-386f0363/script.sh: 2: /root/.jenkins/workspace/tina-pipeline@tmp/durable-386f0363/script.sh: source: not found
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
$ docker stop --time=1 459169f2aea2499f4c361291733308fafa74ec63e08b7a1b74814e0bb9077d51
$ docker rm -f 459169f2aea2499f4c361291733308fafa74ec63e08b7a1b74814e0bb9077d51
[Pipeline] // withDockerContainer
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 127
Finished: FAILURE

我不明白为什么 Jenkins 不运行 docker 容器内的步骤。我在想所有的步骤都将在 docker 容器内执行。如何让 Jenkins 运行 docker 容器内的所有步骤?并且在执行这些步骤之前会不会将github项目的所有代码复制到docker容器中?

标签: dockerjenkins

解决方案


推荐阅读