You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Jenkins参数传入Angular2项目的Dockerfile中?

How to Pass Jenkins Parameters to Your Angular Project's Dockerfile

Great news—you're already using ARG in your Dockerfile, which is exactly the right foundation for passing in Jenkins parameters. Let's walk through the full setup step by step to tie everything together:

1. Enable Parameterized Builds in Jenkins

First, configure your Jenkins job to accept input parameters:

  • Head to your Jenkins job's configuration page, find the General section, and check the box for "This project is parameterized".
  • Add a String Parameter (you can switch to a dropdown later if needed for predefined environments). Name it BUILD_ENV (we'll align this with the Dockerfile for clarity) and set the default value to prod to match your existing Dockerfile default.

Your current Dockerfile has ARG env=prod, which works, but aligning the parameter name with Jenkins will make your setup more intuitive. Update it like this:

# Stage 0: Build Angular app with Node.js
FROM node:latest as node
WORKDIR /app
COPY package.json /app/
RUN npm install
COPY ./ /app/
# Use the same parameter name as Jenkins for consistency
ARG BUILD_ENV=prod
RUN npm run build -- --prod --environment $BUILD_ENV

# Stage 1: Serve compiled app with Nginx
FROM nginx:1.13
COPY --from=node /app/dist/ /usr/share/nginx/html
# Don't forget to complete your Nginx config copy here, e.g.:
# COPY ./nginx.conf /etc/nginx/nginx.conf

3. Pass the Jenkins Parameter to docker build

In your Jenkins build step (like an "Execute shell" step), use the --build-arg flag to pass the Jenkins parameter into the Docker build process:

# Build the image, passing the Jenkins BUILD_ENV parameter
docker build --build-arg BUILD_ENV=${BUILD_ENV} -t your-image-repo/your-app:${BUILD_ENV} .

# Push the tagged image (adjust the repo/tag to match your registry setup)
docker push your-image-repo/your-app:${BUILD_ENV}

If You're Using a Jenkins Pipeline (Jenkinsfile)

If your Jenkins setup uses a Pipeline script, here's how to implement the parameterized build:

pipeline {
    agent any
    parameters {
        string(
            name: 'BUILD_ENV',
            defaultValue: 'prod',
            description: 'Environment to build the Angular app for (prod/staging/dev etc.)'
        )
    }
    stages {
        stage('Build Docker Image') {
            steps {
                sh 'docker build --build-arg BUILD_ENV=${BUILD_ENV} -t your-image-repo/your-app:${BUILD_ENV} .'
            }
        }
        stage('Push to Registry') {
            steps {
                sh 'docker push your-image-repo/your-app:${BUILD_ENV}'
            }
        }
    }
}

Quick Note on ARG Scope

Remember that ARG only exists during the Docker build process. If you needed this variable to be available when the container runs, you'd use ENV to convert it—but since you're only using it for the Angular build, ARG is perfect here. It won't bloat your final Nginx image with unnecessary environment variables.

内容的提问来源于stack exchange,提问作者Adam Bremen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:01:58