Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'
like image 595
Kay Avatar asked Jan 07 '19 12:01

Kay


People also ask

What is Before_script in GitLab CI?

These are scripts that you choose to be run before the job is executed or after the job is executed. These can also be defined at the top level of the YAML file (where jobs are defined) and they'll apply to all jobs in the . gitlab-ci. yml file.

Do GitLab stages run in parallel?

GitLab provides a method to make clones of a job and run them in parallel for faster execution using the parallel: keyword. While parallel jobs may not help in reducing the consumption of CI minutes, they definitely help increase work productivity.

What is stage in GitLab CI?

History of stages in GitLab CI/CD By default, stages are ordered as: build , test , and deploy - so all stages execute in a logical order that matches a development workflow. The first step is to build the code, and if that works, the next step is to test it. If the tests pass, then you deploy the application.

Which file is used to specify the jobs within the GitLab CI pipeline?

yml` file | GitLab.


1 Answers

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 GitLab's anchor's feature, 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
like image 129
Rekovni Avatar answered Sep 22 '22 17:09

Rekovni