Kay
Kay

Reputation: 19640

Gitlab CI - Specifying stages in before_script

i want to run a script that is needed for my test_integration and build stage. Is there a way to specify this in the before script so i don't have to write it out twice.

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

this does not seem to work i get the following error in gitlab ci linter.

Status: syntax is incorrect

Error: before_script config should be an array of strings

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

Upvotes: 37

Views: 57627

Answers (1)

Rekovni
Rekovni

Reputation: 7344

The before_script syntax does not support a stages section. You could use before_script as you have done without the stages section, however the before_script stage would run for every single job in the pipeline.

Instead, what you could do is use YAML's anchor's feature (supported by Gitlab), which allows you to duplicate content across the .gitlab-ci file.

So in your scenario, it would look something like:

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'

Edit: there is another way, instead of using anchors, you could also use extends syntax:

.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

Upvotes: 84

Related Questions