1

we have dockerfile in our angular2 project and we build/push/pull it on server with jenkins

here is content of 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

when I build and push it with this args (from powershell or cmd) it works great but need to put this arg from jenkins slave when deploying, is this possible?

--build-arg env=dev .

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

here is jenkins build and deploy script

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}\"'"
            }
        }
 }
Adam Bremen
  • 685
  • 1
  • 7
  • 18
  • 2
    --build-arg env=test will not be avaible once contianer up you need to assign that value to ENV env=${env} as args vanish after docker build while ENV persist in docker container. then access master ENV from salve – Adiii Apr 05 '18 at 07:45
  • https://stackoverflow.com/questions/28070515/retrieving-jenkins-node-variables-from-master – Adiii Apr 05 '18 at 07:46
  • @Adiii thanks, but we don't use Groovy plugin, is there another way? – Adam Bremen Apr 05 '18 at 08:04
  • Then you need to start slave with same ENV or another option is here https://stackoverflow.com/questions/12670572/passing-system-env-variable-into-jenkins-slave – Adiii Apr 05 '18 at 08:39

0 Answers0