40

There are 3 stages - build, test and deploy in .gitlab-ci.yml.

A nightly regression test stage needs to be run nightly.

Here's the relevant .gitlab-ci.yml code:

stages:
  - build
  - test
  - deploy

build_project:
  stage: build
  script:
    - cd ./some-dir
    - build-script.sh
  except:
  - tags

#Run this only when say variable 'NIGHTLY_TEST == True'. But HOW?
nightly_regression_test_project:
  stage: test
  script:
    - cd ./some-dir
    - execute test-script

Tagging daily to only run the test stage is not preferable.

Any other ideas?

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
deepdive
  • 9,720
  • 3
  • 30
  • 38
  • I [posted](https://stackoverflow.com/a/59503715/3439096) an answer with screenshots and example. Hope that helps – Frak Dec 27 '19 at 16:51
  • 1
    This is a question about pipeline "scheduling", not really about pipeline stage "conditional " execution. – OneMoreNerd Oct 15 '20 at 11:28

5 Answers5

51

except and only can specify variables that will trigger them.

You can use the following in your .gitlab-ci.yml:

build1:
  stage: build
  script:
    - echo "Only when NIGHTLY_TEST is false"
  except:
    variables:
      - $NIGHTLY_TEST 
    
test1:
  stage: test
  script: 
    - echo "Only when NIGHTLY_TEST is true"
  only:
    variables:
      - $NIGHTLY_TEST 
Josh Correia
  • 3,807
  • 3
  • 33
  • 50
avolkmann
  • 2,962
  • 2
  • 19
  • 27
18

There's not currently a way to run a job depending on the environmental variables (you can always open a feature request!). You could use a simple Bash command to immediately exit if the environment variable doesn't exist, though.

Something like:

stages:
  - build
  - test
  - deploy

build_project:
  stage: build
  script:
    - cd ./some-dir
    - build-script.sh
  except:
  - tags

# Run this only when NIGHTLY_TEST environment variable exists.
nightly_regression_test_project:
  stage: test
  script:
    - [ -z "$NIGHTLY_TEST" ] && exit 1;
    - cd ./some-dir
    - execute test-script

If the variable doesn't exist the tests that follow it won't run. Otherwise, they will.

Hope that helps!

Connor Shea
  • 795
  • 7
  • 22
  • 6
    If you don't want the `nightly_regression_test_project` job failing to fail the whole pipeline, you can use `exit 0` instead of `exit 1`: the job will terminate, but the pipeline will continue – Matt Rice Mar 14 '19 at 18:52
  • 1
    need to quote "[ -z '$NIGHTLY_TEST' ] && exit 1;" – rh0x Sep 02 '19 at 10:18
  • I am getting "bad indentation of a sequence entry" error in gitlab editor – A Yashwanth Mar 06 '22 at 11:21
10

In case anyone is looking for this now, gitlab has now implemented a scheduled build feature with variable overwriting (incredibly handy). Documentation found here.

For anyone interested in the instructions for this feature when this answer was given, here it goes:

Using Pipeline Schedules

In order to schedule a pipeline:

  1. Navigate to your project's Pipelines -> Schedules and click the New Schedule button.
  2. Fill in the form
  3. Hit Save pipeline schedule for the changes to take effect.

My favorite feature of this is the scheduled pipeline variables.

The variables documentation can be found here, but the most useful information for me was the priority, which I will retype here:

Priority of Variables

The variables can be overwritten and they take precedence over each other in this order:

  1. Trigger variables or scheduled pipeline variables (take precedence over all)
  2. Project-level secret variables or protected secret variables
  3. Group-level secret variables or protected secret variables
  4. YAML-defined job-level variables
  5. YAML-defined global variables
  6. Deployment variables
  7. Predefined variables (are the lowest in the chain)

Hope this helps. I'm delighted that they added this feature.

trueCamelType
  • 2,198
  • 5
  • 39
  • 76
  • 1
    If you can actually put something together in answer itself, it would be handy for reference then going to the doc – deepdive Aug 11 '17 at 01:55
  • I thought about doing that, but I was worried that the instructions would change in the next month or so. I'll go back and add instructions for the current method, and hope that they don't change it :) – trueCamelType Aug 11 '17 at 01:56
  • 2
    The documentation is not helpful. It shows how to set a variable and it shows how to use `only` and `except` for schedules, but it doesn't show how to use `only` and `except` WITH variables. What syntax should be used? – Frak Dec 27 '19 at 16:38
6

Select CI/CD -> Schedules on the left frame of your project:

enter image description here

Create a new Schedule:

enter image description here

Add your NIGHTLY_TEST variable and set it to True:

enter image description here

Add the only and variables section to your gitlab-ci.yml file:

    nightly_regression_test_project:
      stage: test
      script:
        - cd ./some-dir
        - execute test-script
      only:
        variables:
          - $NIGHTLY_TEST == "True"

-- UPDATE -- Using the new rules based logic, you can do this instead:

    nightly_regression_test_project:
      stage: test
      script:
        - cd ./some-dir
        - execute test-script
      rules:
        - if: $NIGHTLY_TEST == "True"
          when: always
tomodachi
  • 251
  • 3
  • 9
Frak
  • 832
  • 1
  • 11
  • 32
-5

I just implemented this "feature" by following the example found here Use crontab and curl(I use Linux, because why not?) to set off a trigger to run your nightly tests.

30 0 * * * curl --request POST --form token=TOKEN --form ref=master https://gitlab.example.com/api/v3/projects/9/trigger/builds