【问题标题】:How can I use different script in .gitlab-ci.yml depending on pushed branched如何根据推送的分支在 .gitlab-ci.yml 中使用不同的脚本
【发布时间】:2020-10-01 12:19:05
【问题描述】:

我在gitlab-ci.yml 文件中有以下设置用于构建

build_app:
  image: node:10
  stage: build
  artifacts:
    expire_in: 5 minute
    paths:
      - ./dist
  script:
#    - mkdir app
#    - cp package*.json ./app
    - npm ci
#    - COPY . . ???
    - npm run build --prod

如您所见,我的构建始终带有 --prod 标志,当我在测试服务器上进行测试时,它也以 prod 数据开头

当我推送到master 分支时,如何使脚本使用ng build --prod,当我推送到dev 分支时使用ng build --configuration=stage

【问题讨论】:

标签: angular git gitlab gitlab-ci


【解决方案1】:

只需编写一个 shell 脚本来检查导致 ci/cd 运行的当前分支:

- case "$CI_COMMIT_BRANCH" in 
  "master") ng build --prod; ;;
  "dev") ng build --configuration=stage; ;;
  esac

或者用一个简单的 if else 可能更具可读性:

- if [ "$CI_COMMIT_BRANCH" = "master" ]; then
       ng build --prod;
  elif [ "$CI_COMMIT_BRANCH" = "dev" ]; then
       ng build --configuration=stage;
  fi

【讨论】:

    【解决方案2】:

    每个存储库只有一个 yaml 文件。但是您可以仅使用关键字来定义脚本的哪个部分在哪个分支上运行(有关更多详细信息,请参阅文档)。 这应该是这样的:

    deploy_master:

    阶段: -构建

    构建: 阶段:构建 脚本: -“这是你的命令” 只要: - 大师

    deploy_dev:

    阶段: -构建

    构建: 阶段:构建 脚本: -“这是你的命令” 只要: - 开发

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-10
      • 2021-11-11
      • 2016-02-21
      • 2020-12-01
      • 2020-09-13
      • 1970-01-01
      相关资源
      最近更新 更多