0
votes

I'm trying to have 2 types of tests run depending on why the pipeline is being run. On commits I'd like to run my build stage, and on merge requests I'd like to run my build and test stages. When I do a commit it runs only build but it also only runs build on Merge Requests.

I've tried specifying only:merge_requests in my .gitlab-ci.yml but it still won't register the stage.

stages:
  - build
  - deploy
  - test
  - deploy_prod

build:
  image: "python:3.6.1"
  stage: build
  variables:
    GIT_SUBMODULE_STRATEGY: 'recursive'
  script:
    - pip install --upgrade pip
    - pip install -r lender_v2/requirements.txt
    - pip install -r lender_v2/reggora_models/requirements.txt
    - export FLASK_CONFIG=localhost
    - cd lender_v2/smoke_tests && python3 -m unittest discover -p "smoke_*.py" -q --failfast

test:
  image: "python:3.6.1"
  stage: test
  variables:
    GIT_SUBMODULE_STRATEGY: 'recursive'
  script:
    - pip install --upgrade pip
    - pip install -r lender_v2/requirements.txt
    - pip install -r lender_v2/reggora_models/requirements.txt
    - export FLASK_CONFIG=localhost
    - cd lender_v2/test_project && python3 -m unittest discover ../test_project -p "test_*.py" -q --failfast
  only:
    - merge_requests

I thought this should run the test stage on merge_requests but it won't run at any time.

2

2 Answers

0
votes

Which GitLab and GitLab runner versions are you using?

only: merge_requests was introduced in Gitlab 11.6.

Also, note the information from the Docs:

As of GitLab 11.10, pipelines for merge requests require GitLab Runner 11.9 or higher due to the recent refspecs changes. Anything lower will cause the pipeline to fail.

0
votes

Gitlab documentation recommends using 'rules' instead of 'only'. You can accomplish only merge_requests by doing the following:

stages:
  - build
  - deploy
  - test
  - deploy_prod

build:
  image: "python:3.6.1"
  stage: build
  variables:
    GIT_SUBMODULE_STRATEGY: 'recursive'
  script:
    - pip install --upgrade pip
    - pip install -r lender_v2/requirements.txt
    - pip install -r lender_v2/reggora_models/requirements.txt
    - export FLASK_CONFIG=localhost
    - cd lender_v2/smoke_tests && python3 -m unittest discover -p "smoke_*.py" -q --failfast
  rules:
    - if: '$CI_PIPELINE_SOURCE == "push"'

test:
  image: "python:3.6.1"
  stage: test
  variables:
    GIT_SUBMODULE_STRATEGY: 'recursive'
  script:
    - pip install --upgrade pip
    - pip install -r lender_v2/requirements.txt
    - pip install -r lender_v2/reggora_models/requirements.txt
    - export FLASK_CONFIG=localhost
    - cd lender_v2/test_project && python3 -m unittest discover ../test_project -p "test_*.py" -q --failfast
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'

https://docs.gitlab.com/ee/ci/yaml/#workflowrules