【问题标题】:Jenkins multi-agent build switching directories behind the scenes preventing AWS CodeDeploy plugin from zipping built filesJenkins 多代理在幕后构建切换目录,防止 AWS CodeDeploy 插件压缩构建文件
【发布时间】:2026-01-03 23:30:01
【问题描述】:

我已将我的问题缩小到一个非常具体的原因,但不确定如何解决它。

在 docker 容器中使用 Jenkins-configuration-as-code 设置的 Jenkins 服务器运行一个 pipelineJob 脚本,该脚本设置一个 github 存储库的侦听器,并且每当有新的提交时,都会从存储库中提取一个 jenkinsfile 并使用它来运行使用声明性管道插件的工作。在作业结束时,使用 AWS CodeDeploy 插件执行构建后操作,将构建的应用程序推送到自动缩放组。

以前,应用程序的前端和后端有两个单独的作业,分别部署每个部分,一切都很好。整个系统端到端工作,无需任何人工交互。

不过,现在,在使用多个 docker 代理将两个存储库及其声明性管道合并为一个后,该作业仍然构建,如果我检查 Jenkins 服务器,它说它是成功的,但如果我检查 ec2 实例 CodeDeploy 推送更新我发现的只是原始源文件,根本没有构建文件。

经过一番挖掘和敲门后,我发现 Jenkins 在后台运行 git clone 和 /var/jenkins_home/workspace/FOLDER 中的 codedeploy 插件,但使用 golang docker 代理构建后端,使用 node docker 代理构建前端/var/jenkins_home/workspace/FOLDER@2。这意味着当 CodeDeploy 将其全部压缩时,它会丢失实际构建的所有内容。

我的问题是如何停止/修复它?

管道 Jenkins 文件:

pipeline {
    agent any
    
    stages {
        stage('Clone git') {
            steps { git 'https://github.com/REPO' }
        }

        stage('Backend') {
            agent { docker { 
                image 'golang:latest' 
                args '-v "$PWD"/backend:/test -w /test'
            } }

            environment { GOCACHE = "${WORKSPACE}" }

            steps { 
                dir('backend') {
                    sh 'make install' 
                    sh 'make build'
                }
            }
        }

        stage('Frontend') {
            agent { docker { image 'node:latest' } }

            steps { 
                dir('frontend') {
                    sh 'make install'
                    sh 'make build' 
                }
            }
        }
        
    }
    
    post ('Deploy') {
        success {
            step([
                $class: 'AWSCodeDeployPublisher', 
                applicationName: 'server_app', 
                awsAccessKey: '', 
                awsSecretKey: '', 
                deploymentGroupAppspec: false, 
                deploymentGroupName: 'server_group', 
                deploymentMethod: 'deploy', 
                excludes: '', 
                iamRoleArn: '', 
                includes: '**', 
                proxyHost: '', 
                proxyPort: 0,
                region: 'us-west-2', 
                s3bucket: 'server-bucket', 
                s3prefix: '', 
                subdirectory: '', 
                versionFileName: '', 
                waitForCompletion: false
            ])
        }
    }
}

作业控制台输出(注意它从FOLDER 切换到FOLDER@2 的位置):

Checking out git https://github.com/REPO into /var/jenkins_home/workspace/FOLDER@script to read pipeline.jenkinsfile
The recommended git tool is: NONE
No credentials specified
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://github.com/REPO # timeout=10
Fetching upstream changes from https://github.com/REPO
 > git --version # timeout=10
 > git --version # 'git version 2.11.0'
 > git fetch --tags --progress -- https://github.com/REPO +refs/heads/*:refs/remotes/origin/* # timeout=10
Seen branch in repository origin/master
Seen 1 remote branch
 > git show-ref --tags -d # timeout=10
Checking out Revision 385e449faf2f2316e308d9cc2de07725cfaf3582 (origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 385e449faf2f2316e308d9cc2de07725cfaf3582 # timeout=10
Commit message: "simplify jenkinsfile"
 > git rev-list --no-walk 263260386f964b2d7bc8e9921f3b3bb7214ea09a # timeout=10
[Checks API] No suitable checks publisher found.
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/FOLDER
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
The recommended git tool is: NONE
No credentials specified
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://github.com/REPO # timeout=10
Fetching upstream changes from https://github.com/REPO
 > git --version # timeout=10
 > git --version # 'git version 2.11.0'
 > git fetch --tags --progress -- https://github.com/REPO +refs/heads/*:refs/remotes/origin/* # timeout=10
Seen branch in repository origin/master
Seen 1 remote branch
 > git show-ref --tags -d # timeout=10
Checking out Revision 385e449faf2f2316e308d9cc2de07725cfaf3582 (origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 385e449faf2f2316e308d9cc2de07725cfaf3582 # timeout=10
Commit message: "simplify jenkinsfile"
[Checks API] No suitable checks publisher found.
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Clone git)
[Pipeline] git
The recommended git tool is: NONE
No credentials specified
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://github.com/REPO # timeout=10
Fetching upstream changes from https://github.com/REPO
 > git --version # timeout=10
 > git --version # 'git version 2.11.0'
 > git fetch --tags --progress -- https://github.com/REPO +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
Checking out Revision 385e449faf2f2316e308d9cc2de07725cfaf3582 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 385e449faf2f2316e308d9cc2de07725cfaf3582 # timeout=10
 > git branch -a -v --no-abbrev # timeout=10
 > git branch -D master # timeout=10
 > git checkout -b master 385e449faf2f2316e308d9cc2de07725cfaf3582 # timeout=10
Commit message: "simplify jenkinsfile"
[Checks API] No suitable checks publisher found.
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Backend)
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/FOLDER@2
[Pipeline] {
[Pipeline] checkout
The recommended git tool is: NONE
No credentials specified
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://github.com/REPO # timeout=10
Fetching upstream changes from https://github.com/REPO
 > git --version # timeout=10
 > git --version # 'git version 2.11.0'
 > git fetch --tags --progress -- https://github.com/REPO+refs/heads/*:refs/remotes/origin/* # timeout=10
Seen branch in repository origin/master
Seen 1 remote branch
 > git show-ref --tags -d # timeout=10
Checking out Revision 385e449faf2f2316e308d9cc2de07725cfaf3582 (origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 385e449faf2f2316e308d9cc2de07725cfaf3582 # timeout=10
Commit message: "simplify jenkinsfile"
[Checks API] No suitable checks publisher found.
[Pipeline] withEnv
[Pipeline] {
[Pipeline] isUnix
[Pipeline] sh
+ docker inspect -f . golang:latest
.
[Pipeline] withDockerContainer
Jenkins seems to be running inside container 3ff4f947dfec2937398c540976187282866705cfe05a4a98255e57566e43f6a9
$ docker run -t -d -u 1000:1000 -v $PWD/backend:/test -w /test -w /var/jenkins_home/workspace/FOLDER@2 --volumes-from 3ff4f947dfec2937398c540976187282866705cfe05a4a98255e57566e43f6a9 -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** golang:latest cat
$ docker top 69a4b1b98f0a3824bbcb785d9464f5450f4432a2458399e328214981f46e992d -eo pid,comm
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] dir
Running in /var/jenkins_home/workspace/FOLDER@2/backend
[Pipeline] {
[Pipeline] sh
+ make install
go get "github.com/julienschmidt/httprouter"
[Pipeline] sh
+ make build
go build -o ../server main.go
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
$ docker stop --time=1 69a4b1b98f0a3824bbcb785d9464f5450f4432a2458399e328214981f46e992d
$ docker rm -f 69a4b1b98f0a3824bbcb785d9464f5450f4432a2458399e328214981f46e992d
[Pipeline] // withDockerContainer
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Frontend)
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/FOLDER@2
[Pipeline] {
[Pipeline] checkout
The recommended git tool is: NONE
No credentials specified
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://github.com/REPO # timeout=10
Fetching upstream changes from https://github.com/REPO
 > git --version # timeout=10
 > git --version # 'git version 2.11.0'
 > git fetch --tags --progress -- https://github.com/REPO +refs/heads/*:refs/remotes/origin/* # timeout=10
Seen branch in repository origin/master
Seen 1 remote branch
 > git show-ref --tags -d # timeout=10
Checking out Revision 385e449faf2f2316e308d9cc2de07725cfaf3582 (origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 385e449faf2f2316e308d9cc2de07725cfaf3582 # timeout=10
Commit message: "simplify jenkinsfile"
[Checks API] No suitable checks publisher found.
[Pipeline] withEnv
[Pipeline] {
[Pipeline] isUnix
[Pipeline] sh
+ docker inspect -f . node:latest
.
[Pipeline] withDockerContainer
Jenkins seems to be running inside container 3ff4f947dfec2937398c540976187282866705cfe05a4a98255e57566e43f6a9
$ docker run -t -d -u 1000:1000 -w /var/jenkins_home/workspace/FOLDER@2 --volumes-from 3ff4f947dfec2937398c540976187282866705cfe05a4a98255e57566e43f6a9 -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** node:latest cat
$ docker top ce7b3a6c0e4a3a511bcf52e10262618b8c095060bd42f155977f31374cb7dee7 -eo pid,comm
[Pipeline] {
[Pipeline] dir
Running in /var/jenkins_home/workspace/FOLDER@2/frontend
[Pipeline] {
[Pipeline] sh
+ make install
yarn install
yarn install v1.22.5
warning package.json: No license field
warning new@1.0.0: No license field
[1/4] Resolving packages...
success Already up-to-date.
Done in 0.84s.
[Pipeline] sh
+ make build
make: 'build' is up to date.
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
$ docker stop --time=1 ce7b3a6c0e4a3a511bcf52e10262618b8c095060bd42f155977f31374cb7dee7
$ docker rm -f ce7b3a6c0e4a3a511bcf52e10262618b8c095060bd42f155977f31374cb7dee7
[Pipeline] // withDockerContainer
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] step
Zipping files into /tmp/#2-8971329807001316273.zip
// deleting some aws account info that displays
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
[Checks API] No suitable checks publisher found.
Finished: SUCCESS

【问题讨论】:

    标签: docker jenkins aws-code-deploy


    【解决方案1】:

    知道了。 reuseNode 需要为每个 docker 代理设置为 true:

           agent { docker { 
                image 'golang:latest' 
                reuseNode true
            } }
    

    【讨论】: