简体   繁体   English

如何制作一个简单的 GitLab Ci/CD gitlab-ci.yml 文件来构建 Angular 项目?

[英]How to make a simple GitLab Ci/CD gitlab-ci.yml file to build an Angular project?

This is for an Angular project I have.这是针对我拥有的 Angular 项目的。

The project itself builds and runs just fine.该项目本身构建并运行得很好。 I have just been struggling with trying to figure out how to make my 1st CI/CD pipeline process gitlab-ci.yml file.我一直在努力弄清楚如何制作我的第一个 CI/CD 管道流程gitlab-ci.yml文件。 This has been going on for months.这种情况已经持续了几个月。

How the heck is this to be setup because I cannot find a basic example that just works.这是如何设置的,因为我找不到一个可以正常工作的基本示例。

image: node:14.17.0

stages:            # List of stages for jobs, and their order of execution
  - setup
  - build
  - cleanup

.pre:
  stage: setup,
  script:
    - mkdir -p dist
    - npm install -g @angular/cli@12.2.16
    - npm install  # or `npm install` or whatever you use to install deps
    - npm start
    - npm --version
  cache:
    paths:
      - node_modules
    policy: pull

build-job:
  stage: build
  script:          # ... your other build steps here
    - npm run build_def_mysetup
    - ls /builds
  cache:
    paths:
      - node_modules
    policy: pull
  artifacts:
    paths:
      - dist/

.post:
  stage: cleanup
  script:
    - echo "cleanup called"

The goal right now for this is to现在的目标是

  1. Install needed node_modules for the build为构建安装所需的 node_modules
  2. Build the Angular application构建 Angular 应用程序
  3. Eventually if build fails, notify via email the developer who last pushed the build最终如果构建失败,通过 email 通知上次推送构建的开发人员
  4. Eventually if build pass - do nothing最终,如果构建通过 - 什么也不做
  5. Eventually if build pass - Run unit tests最终,如果构建通过 - 运行单元测试
  6. Eventually if build pass and branch has release in name - tag the branch最终,如果构建过程和分支名称中有发布 - 标记分支

I say eventually because I cannot get #1 to work我说最终是因为我无法让#1 工作

Alright, i had a quick glance at your pipeline and tried it on an angular project.好的,我快速浏览了您的管道并在 angular 项目上进行了尝试。 First, by following the gitlab-ci documentation, you should not mix stages, with .pre and .post .首先,通过遵循gitlab-ci文档,您不应将阶段与.pre.post混合使用。 please take a look at Gitlab CI stages , in this matter.请看一下Gitlab CI 阶段,在这件事上。

Next, for the matter of artifacts, you do not need to specifically set path to artifacts, as they are kept between subsequent stages.接下来,关于工件,您不需要专门设置工件的路径,因为它们保留在后续阶段之间。

Now for the pipeline现在为管道

- -

  1. Install dependencies - node_modules安装依赖项 - node_modules

    • simply install npm packages只需安装 npm 包
    stage: install_dependencies image: node:14 script: - npm install
  2. Install angular安装 angular

    • the script may be varying, but the base idea is kept脚本可能会有所不同,但基本思想保持不变
    stage: install_angular image: node:14 script: - npm install -D typescript @angular/cli @angular/compiler
  3. Build angular app构建 angular 应用程序

    stage: build image: node:14 script: - npm run build
  4. Notify if the build fails构建失败时通知

    • This is a default behaviour of gitlab, when you run a pipeline and it fails, then you receive an email with the status, see image这是 gitlab 的默认行为,当您运行管道并且它失败时,您会收到带有状态的 email,参见图片管道失败
  5. Run unit tests + release运行单元测试+发布

    • the same steps as previous steps与前面的步骤相同的步骤

The full pipeline would look something like this (its a trivial example, it can be merged into one stage)完整的管道看起来像这样(这是一个简单的例子,它可以合并到一个阶段)

image: node:14.17.0

stages:            # List of stages for jobs, and their order of execution
 - install_dependencies
 - install_angular
 - build
 

install_dep:
  stage: install_dependencies
  image: node:14
  script:
  - npm install

install_ang:
  stage: install_angular
  image: node:14
  script:
 - npm install -D typescript @angular/cli @angular/compiler

build_ang:
  stage: build
  image: node:14
  script:
  - npm run build

This is what my current file has.这是我当前文件的内容。 I needed the artifact in here otherwise ng would not work.我需要这里的工件,否则 ng 将无法工作。

image: node:14.17.0 # This is the container for our Docker we will build in

cache:
  paths:
    - dist/
    - node_modules/

stages:            # List of stages for jobs, and their order of execution
  - install_dependencies
  - install_angular
  - build

install_dep:
  stage: install_dependencies
  image: node:14
  script: # Execute script commands just as you would on terminal command line
    - echo $CI_PROJECT_DIR
    - npm install
    - echo npm --version

  artifacts: # This is required for other stage to have access to what gets created
    paths:
      - node_modules/

install_ang:
  stage: install_angular
  image: node:14
  script:
    - npm install -D typescript @angular/cli@12.2.16 @angular/compiler@12.2.16
# Possible that the -D is optional could use -g or none

build:
  stage: build
  script:
    - npm run build_def_mybuild # this is defined in angular.json
    - ls dist/

This is as far as I"ve made it. I may need the following for doing tests.这是我所做的。我可能需要以下内容进行测试。

  artifacts: # This is required for other stage to have access to what gets created
    paths:
      - dist/

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

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