【问题标题】:Gitlab CI - Specifying stages in before_scriptGitlab CI - 在 before_script 中指定阶段
【发布时间】:2019-01-07 12:25:55
【问题描述】:

我想运行我的 test_integration 和构建阶段所需的脚本。有没有办法在之前的脚本中指定这个,所以我不必写两次。

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'

【问题讨论】:

    标签: gitlab gitlab-ci


    【解决方案1】:

    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'
    

    编辑:还有另一种方法,除了使用锚点,您还可以使用extends 语法:

    .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
    

    【讨论】:

    • 不知道“模板”功能。感谢您的信息!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 2019-09-21
    • 2016-11-19
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    • 2021-10-29
    相关资源
    最近更新 更多