Gitlab CI-在before_脚本中指定阶段

Gitlab CI-在before_脚本中指定阶段,gitlab,gitlab-ci,Gitlab,Gitlab Ci,我想运行测试集成和构建阶段所需的脚本。有没有办法在before脚本中指定它,这样我就不必写两遍了 before_script: stage: ['test_integration', 'build'] 这似乎不起作用,我在gitlab ci linter中遇到以下错误 状态:语法不正确 错误:before_脚本配置应该是字符串数组 .gitlab ci.yml stages: - security - quality - test - build - deploy i

我想运行测试集成和构建阶段所需的脚本。有没有办法在before脚本中指定它,这样我就不必写两遍了

before_script:
  stage: ['test_integration', 'build']
这似乎不起作用,我在gitlab ci linter中遇到以下错误

状态:语法不正确

错误:before_脚本配置应该是字符串数组

.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'

before_脚本
语法不支持
stages
部分。您可以使用
before\u script
,就像您在没有
stages
部分的情况下所做的那样,但是
before\u script
阶段将为管道中的每个作业运行

相反,您可以使用GitLab的功能,它允许您跨
.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'
.before_script_template:
  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

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

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

etc