简体   繁体   中英

Use Jenkins variable in Dockerfile command

I am new to Docker and Jenkins. I have to build and deploy Nest Js app in jenkins. When I run the Jenkins job I have to select the 'DEPLOY_PROFILE' which is equals to 'dev' and 'qa' as follows.

在此处输入图像描述

This is my Dockerfile,

    FROM node:16-alpine
    WORKDIR /app
    ADD package.json /app/package.json
    RUN npm config set registry http://registry.npmjs.org
    RUN npm install
    ADD . /app
    EXPOSE 3000
    CMD ["npm", "run", "start"]

I need to pass the 'DEPLOY_PROFILE' variable which is equals to 'dev' or 'qa' to the Dockerfile. Then final docker command should be look like npm run start:dev or npm run start:qa

I have tried using

CMD ["npm", "run", "start", `:${DEPLOY_PROFILE}`]

and

CMD ["npm", "run", "start", `:${env.DEPLOY_PROFILE}`]

But nothing gave me the luck. Any help may highly appreciated!

You can use an environment variable for that. In your dockerfile, declare an argument (passed into docker build) and an environment variable like this:

ARG DEPLOY_PROFILE
ENV PROFILE=${deploy_profile}

Then use the environment variable like this:

CMD npm run start $PROFILE

Then call buildah (or whatever you are using) like this:

 buildah bud --format=docker --build-arg deploy_profile="$DEPLOY_PROFILE"

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