Gitlab CI-在before_script中指定阶段

时间:2019-01-07 12:25:55

标签: gitlab gitlab-ci

我想运行我的test_integration和build阶段所需的脚本。有没有一种方法可以在before脚本中指定它,所以我不必写两次。

before_script:
  stage: ['test_integration', 'build']

这似乎不起作用,我在gitlab ci linter中收到以下错误。

  

状态:语法不正确

     

错误:before_script配置应为字符串数组

.gitlab-ci.yml

stages:
  - security
  - quality
  - test
  - build
  - deploy

image: node:10.15.0

before_script:
  stage: ['test_integration', 'build']
  script:
  - apt-get update
  - apt-get -y install apt-transport-https ca-certificates curl gnupg2 software-properties-common
  - curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -
  - add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
  - apt-get update
  - apt-get -y install docker-ce
  - curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  - chmod +x /usr/local/bin/docker-compose
  - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY


services:
  - mongo
  - docker:dind

security:
  stage: security
  script:
  - npm audit

quality:
  stage: quality
  script:
  - npm install
  - npm run-script lint

test_unit:
  stage: test
  script:
  - npm install
  - npm run-script unit-test

test_integration:
  stage: test
  script:
  - docker-compose -f CI/backend-service/docker-compose.yml up -d
  - npm install
  - npm run-script integration-test

build:
  stage: build
  script:
  - npm install
  - export VERSION=`git describe --tags --always`
  - docker build -t $CI_REGISTRY_IMAGE:$VERSION .
  - docker push $CI_REGISTRY_IMAGE

deploy:
  stage: deploy
  script: echo 'deploy'

1 个答案:

答案 0 :(得分:4)

before_script语法不支持stages部分。您可以像没有before_script部分那样使用stages,但是before_script阶段将为管道中的每个作业运行。

相反,您可以使用GitLab的anchor's功能,该功能使您可以在.gitlab-ci文件中复制内容。

所以在您的情况下,它看起来像:

stages:
  - security
  - quality
  - test
  - build
  - deploy

image: node:10.15.0

.before_script_template: &build_test-integration
  before_script:
    - apt-get update
    - apt-get -y install apt-transport-https ca-certificates curl gnupg2 software-properties-common
    - curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -
    - add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
    - apt-get update
    - apt-get -y install docker-ce
    - curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    - chmod +x /usr/local/bin/docker-compose
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY


services:
  - mongo
  - docker:dind

security:
  stage: security
  script:
  - npm audit

quality:
  stage: quality
  script:
  - npm install
  - npm run-script lint

test_unit:
  stage: test
  script:
  - npm install
  - npm run-script unit-test

test_integration:
  stage: test
  <<: *build_test-integration
  script:
  - docker-compose -f CI/backend-service/docker-compose.yml up -d
  - npm install
  - npm run-script integration-test

build:
  stage: build
  <<: *build_test-integration
  script:
  - npm install
  - export VERSION=`git describe --tags --always`
  - docker build -t $CI_REGISTRY_IMAGE:$VERSION .
  - docker push $CI_REGISTRY_IMAGE

deploy:
  stage: deploy
  script: echo 'deploy'