把Jenkins的args放到dockerfile中

时间:2018-04-05 07:37:20

标签: docker jenkins dockerfile jenkins-pipeline

我们在angular2项目中有dockerfile,我们使用jenkins在服务器上构建/推送/拉/

这是dockerfile的内容

# Stage 0, based on Node.js, to build and compile Angular
FROM node:latest as node
WORKDIR /app
COPY package.json /app/
RUN npm install
COPY ./ /app/
ARG env=prod
RUN npm run build -- --prod --environment $env 

# Stage 1, based on Nginx, to have only the compiled app, ready for production with Nginx
FROM nginx:1.13
COPY --from=node /app/dist/ /usr/share/nginx/html
COPY ./nginx-custom.conf /etc/nginx/conf.d/default.conf

当我使用这个args(来自powershell或cmd)构建并推送它时,效果很好 但是在部署时需要把这个arg从jenkins slave中取出来,这可能吗?

  

- build-arg env = dev。

docker build -t admione-front-test:latest --build-arg env=test .

这里是jenkins构建和部署脚本

node {

   stage ('build') {
      build job: 'pathToProject' //path to workspace (project files)
   }

   stage ('deploy') {

      sshagent(credentials: ['jenkins-credential']) {
         String commandToRun = 'docker stop project || true && docker rm project || true ';
         commandToRun = commandToRun + ' && docker pull registry/project:latest ';
         commandToRun = commandToRun + ' && docker run -d -p 81:80 --restart unless-stopped  --name project registry/project:latest '
                 sh "ssh -o StrictHostKeyChecking=no root /bin/bash -c '\"${commandToRun}\"'"
            }
        }
 }

0 个答案:

没有答案