Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have a gitlab CI job to be triggred only manual on branches and always automatically on master?

Tags:

gitlab-ci

I have a pages job that I want to run manual on branches, but automatically triggered on master:

pages:
  stage: deploy
  cache:
    paths:
      - public
  script:
    - scripts/pages.sh
  artifacts:
    paths:
      - public
    expire_in: 2 days

So I want a combination of:

  only:
    - master
  when: always
  only:
    - branches
  except:
    - master
  when: manual

Is that possible?

like image 240
Kasper Avatar asked Mar 28 '19 13:03

Kasper


People also ask

Is it possible to override GitLab CI default variables?

gitlab-ci. yml file. The variables are used by the runner any time the pipeline runs. You can also override variable values manually for a specific pipeline.

How do I trigger a specific job in GitLab?

Trigger one particular job, i.e. job3, variables have been used with the CURL command. “variables[TRIGGER_JOB]=job3” makes sure only job3 is run as in job3 we put the condition rules: if: '$TRIGGER_JOB == “job4”'

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.


2 Answers

This should be possible to do if you use GitLab CI rules. This is an example where the shell is powershell and it shows the current time and branch/tag name:

pages:
  script:
    - mkdir public
    - date > public\index.html
    - $CI_COMMIT_REF_NAME >> public\index.html
  artifacts:
    paths:
      - public
  rules:
    - if: '$CI_COMMIT_BRANCH == "master"'
      when: always
    - if: '$CI_COMMIT_BRANCH == null'
      when: never
    - when: manual

GitLab matches each individual rule from top to bottom. If the branch is named 'master', the job gets marked with when: always. If the branch name is null, this is a tag, and the job is marked with never. If this is not a branch named master, nor a tag, this is a normal branch, and the job is marked with manual.

As Aleksey Tsalolikhin described, you can remove this rule:

    - if: '$CI_COMMIT_BRANCH == null'
      when: never

You will then get the option to run the pipeline for your tags as well, like this:

Rules for manual job for both tags and branches, except master branch

If this is what you want or not, that is up to you.

like image 57
MrBerta Avatar answered Sep 26 '22 15:09

MrBerta


I've tweaked the answer by MrBerta -- the third command was missing the echo command.

I also changed the slashes from backslashes to regular forward slashes so I can use the Linux shell rather than Powershell.

It now works.

Here is the gitlab-ci.yml file -- with credit to MrBerta.

pages:
  script:
    - mkdir public
    - date > public/index.html
    - echo $CI_COMMIT_REF_NAME >> public/index.html
  artifacts:
    paths:
      - public
  rules:
    - if: '$CI_COMMIT_BRANCH == "master"'
      when: always
    - if: '$CI_COMMIT_BRANCH == null'
      when: never
    - when: manual

I tried pushing to master, and my GitLab Pages content was updated as expected; and I tried pushing to a feature branch, and the manual "Play" button came up in the CI/CD pipeline UI.

When I pushed a tag (with detached head, i.e., not on any branch), I could not test it -- GitLab CI did not run a pipeline automatically, and when I tried to Run Pipeline, and picked my tag, GitLab threw an error: "The form contains the following error: No stages / jobs for this pipeline."

So, I would simplify this to:

pages:
  script:
    - mkdir public
    - date > public/index.html
    - echo $CI_COMMIT_REF_NAME >> public/index.html
  artifacts:
    paths:
      - public
  rules:
    - if: '$CI_COMMIT_BRANCH == "master"'
      when: always
    - when: manual

This pages job runs manually on branches (and tags but I couldn't test it), but automatically triggered on master, as the original poster requested.

like image 27
Aleksey Tsalolikhin Avatar answered Sep 26 '22 15:09

Aleksey Tsalolikhin