我正在尝试将一个秘密集合的配置文件插入到Docker容器中,作为当前使用声明性语法的Jenkinsfile的一部分。
Jenkinsfile:
pipeline {
agent {
dockerfile true
}
environment {
CONFIG = credentials('name-of-Jenkins-secret-file')
}
stages {
stage('Build') {
steps {
sh "echo ${CONFIG} > /usr/src/scripts/config/default.json"
}
}
}
}
Dockerfile:
FROM node:carbon
WORKDIR /usr/src/scripts
COPY package*.json ./
RUN npm install
COPY . .
ENTRYPOINT node ./run.js
构建时完成的步骤:
Record trace of a Docker image used in FROM <1s
docker build -t 47a61bffd47bc4f049a967a05f05889983734f32 -f "Dockerfile" "."— Shell Script 1s
Dockerfile— Read file from workspace <1s
Checks if running on a Unix-like node <1s
General SCM <1s
Shell Script
[repo_branch-4ZXYH5FGLRRVXQLNJJ2SCGKMMGIMGWH5VUJEHPFZ6CDVODQSHRHQ] Running shell script
Error response from daemon: Container 0daa4e0d56818544fd228607d7c6318963f80ad99c0f8781a425163c4fb4fc2f is not running
script returned exit code -2
STATUS: FAILED
我不确定我是否想以奇怪或不寻常的方式实现这一目标。我发现使用管道构建docker镜像的大部分材料都是为了将它们保存到私有存储库,这表明我可能会做一些奇怪的事情。该错误表示无法对未运行的容器执行操作。
答案 0 :(得分:1)
我建议以下内容:
Jenkinsfile
pipeline {
agent {
dockerfile true
}
environment {
CONFIG = credentials('name-of-Jenkins-secret-file')
}
stages {
stage('Copy Script') {
steps {
sh "echo ${CONFIG} > /usr/src/scripts/config/default.json"
}
}
stage('Run Server') {
steps {
sh "cd /usr/src/scripts && node ./run.js"
}
}
}
}
Dockerfile:
FROM node:carbon
WORKDIR /usr/src/scripts
COPY package*.json ./
RUN npm install
ENTRYPOINT ["/bin/bash"]
我认为这仍然不是完美的代码,但可能是一个起点。
答案 1 :(得分:0)
如果您需要将文件本身嵌入到docker映像中,则可以在docker构建期间将文件作为参数传递。
pipeline {
agent {
dockerfile true
}
environment {
CONFIG = credentials('name-of-Jenkins-secret-file')
}
stages {
stage('Copy Script') {
steps {
#copy file to workspace
cp ${CONFIG} app.cfg
docker build -t your_image_name . -- --build-arg CONFIG_FILE=app.cfg
}
}
stage('Run Server') {
steps {
sh "cd /usr/src/scripts && node ./run.js"
}
}
}
}
在Docker文件中:
FROM node:carbon
# this is the arg need to pass in docker build
ARG CONFIG_FILE
WORKDIR /usr/src/scripts
# copy the config file to your image
COPY $CONFIG_FILE /app/config
COPY package*.json ./
RUN npm install
ENTRYPOINT ["/bin/bash"]