简体   繁体   English

在 Dockerfile 中构建 angular 项目

[英]Build angular project in Dockerfile

i have Angular/Node Project in Gitlab.. I Have Dockerfile我在 Gitlab 中有 Angular/Node 项目。我有 Dockerfile

FROM node:12 as builder    
WORKDIR /app
COPY package.json package-lock.json ./
ENV CI=1
RUN npm ci    
COPY . .
RUN npm run build-web --output-path=/dist 

this Dockerfile is for Master branch, i want if i push code in develop branch, want to define in Dockerfile how to build, for example if i push in develop, in dockerfile want be这个 Dockerfile 用于分支,我想如果我在开发分支中推送代码,想在 Dockerfile 中定义如何构建,例如如果我推入开发,在 Z60F5AD192689E6AD807E86EB0E2B6 中

RUN npm run build-dev

How i can do this?我怎么能这样做? can i use if else statement in dockerfile?我可以在 dockerfile 中使用 if else 语句吗? or yaml或 yaml

Technically you should use single artefact and have it take the configuration at runtime.从技术上讲,您应该使用单个工件并让它在运行时进行配置。 We do that using angular & environments with a envsubs script on the launch.我们在启动时使用 angular 和带有 envsubs 脚本的环境来做到这一点。

You could also check this SO question which makes a good example of it.您还可以查看这个SO question,这是一个很好的例子。

We are using:我们正在使用:

Inside index.html you add script source to /assets/env.js, which gets pulled on page load from static files inside container (assets) and in container runtime script (ENTRYPOINT) just execute:在 index.html 中,您将脚本源添加到 /assets/env.js,从容器(资产)和容器运行时脚本(ENTRYPOINT)中的 static 文件中提取页面加载,只需执行:

#!/bin/sh

# window env substitutions
envsubst < /usr/share/nginx/html/de-DE/assets/data/env.template.js > /usr/share/nginx/html/de-DE/assets/data/env.js
envsubst < /usr/share/nginx/html/en-US/assets/data/env.template.js > /usr/share/nginx/html/en-US/assets/data/env.js

exec "$@"

which then fills env.js with env.template.js (which has $PLACEHOLDER signs, which are overriden by env variables of container).然后用 env.template.js 填充 env.js(它有 $PLACEHOLDER 标志,被容器的 env 变量覆盖)。

and then load it into window object.然后将其加载到 window object 中。

And to even make it type safe we created:为了使其类型安全,我们创建了:

export interface RuntimeEnvironmentInterface {
  baseUrl: string;
}

which then gets global override on the Window object:然后在 Window object 上获得全局覆盖:

declare global {
  interface Window {
    envVariables: RuntimeEnvironmentInterface;
  }
}

And inside application's environment settings (environment.prod.ts for angular):并在应用程序的环境设置内部(用于角度的 environment.prod.ts):

export const environment: EnvironmentInterface = {
  production: true,
  api: window.envVariables.baseUrl + '/api/v1',
};

We use it in production for almost two years now and it works as a charm (knocking on the wood)我们在生产中使用它将近两年了,它就像一个魅力(敲木头)

workflow:
  rules:
    - if: $CI_COMMIT_BRANCH == "develop"
      variables:
        SSH_IP: "ad@1.2.3.4.5"
        ENVIRONMENT: "develop"
    - if: $CI_COMMIT_BRANCH == "master"
      variables:
        SSH_IP: "ad@1.2.3.4.5"
        ENVIRONMENT: "production"

I achieve this by defining a variable in my CI file that checks for which environment i'm trying to build, based on the branch i'm committing from (You could change this to fit your situation).我通过在我的 CI 文件中定义一个变量来实现这一点,该变量根据我提交的分支检查我正在尝试构建的环境(您可以更改它以适应您的情况)。

A bit unrelated if you specifically require the image locally, but might be the solution you're looking for if you're trying to then deploy the image somewhere.如果您在本地特别需要图像,则有点无关紧要,但如果您尝试在某处部署图像,则可能是您正在寻找的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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