在 Jenkins 管道中的 docker 容器内使用文件参数

问题描述 投票:0回答:1

我有一个管道,可以在 docker 容器内运行一些 python 脚本。我希望将 json 文件传递到容器内部,以便脚本可以使用它。为了方便讨论,假设我希望将文件存储在文件夹

[pipeline root dir]/json
中,名称为
somefile.json
(因此其完整相关路径为
[pipeline root dir]/json/somefile.json
)。

管道使用 git 存储库在每次运行时提取项目(即我在管道的常规设置中的

GitHub Project
下定义了它):

How the pipline is defined to read from the git repo, anonymized for privacy

在管道的 GUI 设置页面中,我添加了一个

File Parameter
选项,它将保存用户在运行开始时从计算机上传的文件:
json/somefile.json
:

The File Parameter input in the settings

这就是我的常规脚本的样子(出于隐私原因进行了简化和更改):

pipeline {
  agent {
    label 'ubuntu:22.04'
  }

  stages {
    stage('main') {
      steps {
        script {
          docker.withRegistry("${SOME_DOCKER_REGISTRY}", "${SOME_TOKEN}") {
            docker.image("${DOCKER_CONTAINER_NAME}").inside {
              sh 'ls json'
            }
          }
        }
      }
    }
  }
}

但是,当我运行管道时,命令

ls json
(我希望存储我的
somefile.json
)返回空结果:

The json dir where I expect somefile.json to be is empty

对于此事,我将不胜感激。

jenkins pipeline
1个回答
0
投票

两种选择:

  1. reuseNode true
    - 确保安装了原始工作区,其中包含参数文件。

  2. Stash/unstash - 将文件存储在容器外部,然后在容器内部取消存储。

© www.soinside.com 2019 - 2024. All rights reserved.