From 27a5f778c01bd5cb00bd8669f9c14bae413b5454 Mon Sep 17 00:00:00 2001 From: sunlei Date: Sat, 16 May 2026 02:50:02 +0800 Subject: [PATCH] =?UTF-8?q?ci:=20=E4=BD=BF=E7=94=A8=20Agent=20=E5=B7=A5?= =?UTF-8?q?=E4=BD=9C=E7=9B=AE=E5=BD=95=E4=BF=9D=E5=AD=98=E7=8E=AF=E5=A2=83?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jenkinsfile | 6 +++--- ci/jenkins-agent/README.md | 22 +++++++++++++--------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 28729b8..f813e7f 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -41,7 +41,7 @@ pipeline { string(name: 'IMAGE_TAG', defaultValue: '', description: '镜像标签,为空时使用 分支名-BUILD_NUMBER;PR 使用源分支名') string(name: 'CONTAINER_NAME', defaultValue: 'kt-template-online-api', description: '业务容器名称') string(name: 'CONTAINER_PORT', defaultValue: '48085', description: '宿主机映射端口,容器内固定使用 48085') - string(name: 'CONTAINER_ENV_FILE', defaultValue: '/nas-env/kt-template-online-api/.env.production', description: 'Agent 容器内可读取的业务 env 文件路径') + string(name: 'CONTAINER_ENV_FILE', defaultValue: '/home/jenkins/agent/env/kt-template-online-api/.env.production', description: 'Agent workdir 内可读取的业务 env 文件路径') string(name: 'CONTAINER_NETWORK', defaultValue: '', description: '业务容器加入的 Docker 网络,为空则使用 Docker 默认网络') string(name: 'CONTAINER_EXTRA_ARGS', defaultValue: '', description: 'docker run 额外参数,例如 -v /host/data:/app/data') } @@ -204,8 +204,8 @@ pipeline { set -e if [ ! -f '${containerEnvFile}' ]; then echo "Container env file not found: ${containerEnvFile}" - echo "Mount the NAS env directory into the Agent, for example:" - echo "/vol1/docker/kt-template-online-api:/nas-env/kt-template-online-api:ro" + echo "Put .env.production under the existing Agent workdir volume, for example:" + echo "/home/jenkins/agent/env/kt-template-online-api/.env.production" exit 1 fi diff --git a/ci/jenkins-agent/README.md b/ci/jenkins-agent/README.md index eb3358a..d04fc90 100644 --- a/ci/jenkins-agent/README.md +++ b/ci/jenkins-agent/README.md @@ -72,7 +72,6 @@ docker run -d \ -e JENKINS_SECRET=替换成节点页面里的secret \ -e JENKINS_AGENT_WORKDIR=/home/jenkins/agent \ -v /var/run/docker.sock:/var/run/docker.sock \ - -v /vol1/docker/kt-template-online-api:/nas-env/kt-template-online-api:ro \ -v kt-node-agent-workdir:/home/jenkins/agent \ kt-jenkins-agent:node22 ``` @@ -90,21 +89,26 @@ docker run -d \ Jenkinsfile 的 `Docker Run` 阶段默认会读取 Agent 容器内的: ```text -/nas-env/kt-template-online-api/.env.production +/home/jenkins/agent/env/kt-template-online-api/.env.production ``` -这个路径来自上面启动 Agent 时的只读挂载: +这个路径在已有的 `kt-node-agent-workdir` volume 里,不需要为了 env 文件重新创建 Agent 容器。先在 Agent 容器内创建目录: ```bash --v /vol1/docker/kt-template-online-api:/nas-env/kt-template-online-api:ro +docker exec kt-node-agent sh -lc 'mkdir -p /home/jenkins/agent/env/kt-template-online-api' ``` -所以真实生产环境变量文件放在 NAS 本地即可,不需要上传 Git: +如果 NAS 上已有 env 文件,可以复制进 Agent workdir: ```bash -mkdir -p /vol1/docker/kt-template-online-api -vi /vol1/docker/kt-template-online-api/.env.production -chmod 600 /vol1/docker/kt-template-online-api/.env.production +docker cp /你的NAS路径/.env.production kt-node-agent:/home/jenkins/agent/env/kt-template-online-api/.env.production +docker exec kt-node-agent sh -lc 'chmod 600 /home/jenkins/agent/env/kt-template-online-api/.env.production' +``` + +复制后确认 Jenkinsfile 能读取到: + +```bash +docker exec kt-node-agent sh -lc 'ls -l /home/jenkins/agent/env/kt-template-online-api/.env.production' ``` 多分支流水线构建时保持默认参数即可: @@ -113,7 +117,7 @@ chmod 600 /vol1/docker/kt-template-online-api/.env.production RUN_DOCKER_CONTAINER=true CONTAINER_NAME=kt-template-online-api CONTAINER_PORT=48085 -CONTAINER_ENV_FILE=/nas-env/kt-template-online-api/.env.production +CONTAINER_ENV_FILE=/home/jenkins/agent/env/kt-template-online-api/.env.production ``` 如果业务容器需要加入某个 Docker 网络,在 Jenkins 参数 `CONTAINER_NETWORK` 填网络名;如果需要挂载上传目录、日志目录等,在 `CONTAINER_EXTRA_ARGS` 填额外的 `docker run` 参数。