简体   繁体   中英

Insert a config file into a declarative Jenkins pipeline Docker container from Dockerfile

I am trying to insert a config file that is a collection of secrets into a Docker container as part of a Jenkinsfile that is currently using the declarative syntax.

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

Steps completed when building:

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

I am not sure if I'm trying to achieve this in a strange or unusual way. Much of the material I've found discussing building docker images using a pipeline is with the intention of saving them to a private repository, which indicates that I might be doing something odd. The error indicates that the operation can't be carried out on a container that is not running.

I would suggest following:

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"]

I assume this is still not a perfect code, but may be a starting point.

If you need to embed the file itself in your docker image, you can pass the file as an argument during docker build.

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"
      }
    }
  }
}

In Docker file:

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"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM