简体   繁体   中英

How can I use different script in .gitlab-ci.yml depending on pushed branched

I have following setting in gitlab-ci.yml file for build

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

As you can see my build is always with --prod flag and when I test on test server it also starts with prod data

How can I make script to use ng build --prod when I push to master branch and ng build --configuration=stage when I push to dev branch ?

There is only one yaml file per repository. But you could define what part of the script is run on what branch by using the key word only (for more details, go to documentation). This should look like this:

deploy_master:

stages: -build

build: stage: build script: -"HERE YOUR COMMAND" only: - master

deploy_dev:

stages: -build

build: stage: build script: -"HERE YOUR COMMAND" only: - dev

Just write a shell script to check the current branch that caused the ci/cd to run:

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

or maybe more readable with a simple if else:

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

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