7

I am not quite sure how the gitlab CI workflow should look like to achieve this:

  1. In my gitlab repository every feature will be developed in an own branch. At least the branch will be merged to master.
  2. I am using npm package grunt-bump to bump up the version of package.json

What I want to do with gitlab CI:

  1. For a merge to master I want to do some tests (stage test)
  2. If test stage has passed successfully, the merge should be done and grunt bump should be executed
  3. This will bump up the version value and will do a new commit to master. This commit is always tagged like "v0.0.2" and has a message like "Release v0.0.2". Only for this commit I want to go for build stage which will build and deploy the application.

Summary

So grunt bump should only executed if on master and after successful tests and merging. Only for the resulting commit (Release vx.x.x) the build and deploy job should be done...

Maybe there is a smarter workflow then this idea. Basicly I want to bump version value and tag the commit after merging and successful tests...

My attempt for YAML-file

stages:
  - test
  - build
  - deploy

lint:
  image: testing:latest
  stage: test
  tags:
    - testing
  script:
    - /node_modules/.bin/eslint --ext .js --ext .jsx .

bump:
  stage: build
  tags: 
    - deploy
  script:
    - grunt bump
  only:
    - master
    - /^Merge .*$/

build:
  stage: build
  tags:
    - deploy
  script:
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
    - docker build -t $CI_REGISTRY_IMAGE:latest .
    - docker push $CI_REGISTRY_IMAGE:latest
  only:
    - master
    - tags
    - /^Release .*$/

production:
  stage: deploy
  script:
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
    - docker pull $CI_REGISTRY_IMAGE:latest
    - cd /home/ubuntu
    - docker-compose up -d
  only:
    - master
    - tags
    - /^Release .*$/
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
user3142695
  • 15,844
  • 47
  • 176
  • 332

2 Answers2

12

It is possible to execute jobs only/except based on the predefined gitlab variable $CI_COMMIT_MESSAGE. See gitlab ci reference. Check also the docu on how variable expressions are handled.

commit_message_has_release_job:
  only:
    variables:
      - $CI_COMMIT_MESSAGE =~ /^Release .*$/
  [...]

It is also possible to filter for merge_requests.

merge_request_job:
  only:
    - merge_requests
  [...]

Combining multiple cases of only/except may not work as you might expect it. The list does not work connected with AND, but OR. Also, it is not 100% sure that a leading only followed by an except works as you expect it. So a simple workaround would be something like this:

merge_reqeuest_to_master_job:
  only:
    - merge_requests
  before_script:
    - if [[ $CI_MERGE_REQUEST_TARGET_BRANCH_NAME != "master" ]]; exit 0 ; fi

Since the variable $CI_MERGE_REQUEST_TARGET_BRANCH_NAME is just set, if the pipeline is for a merge request, you could just do:

merge_reqeuest_to_master_job:
  only:
    variables:
      - $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master"

However, the feature "CI variable expression conjunction/disjunction" is implemented since 12.0.

merge_reqeuest_to_master_job:
  only:
    - merge_requests

To your other remarks:

  • If a single job of a stage fails, by default no jobs from the following stages are started automatically (if you do not allow_failure: true or when: on_failure)
Ali Torki
  • 1,929
  • 16
  • 26
fuma
  • 5,067
  • 4
  • 35
  • 39
0

Yes its possible to trigger pipeline with the specific commit message, for that we can use regexp

For me it works with the following,

install_nodemodules:
stage: install
script:
    - npm install
artifacts:
    paths:
      - node_modules/
only:
    variables:
         - $CI_COMMIT_MESSAGE =~ /Release/

In each stage you need to specify this condition to execute the stage when the Commit message equals to Release