Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do jobs only for merge / specific commit message

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 .*$/
like image 738
user3142695 Avatar asked Apr 19 '17 20:04

user3142695


People also ask

Can I merge specific commit from another branch?

Go to either the git log or the GitHub UI and grab the unique commit hashes for each of the commits that you want. "Cherry pick" the commits you want into this branch. Run this command: git cherry-pick super-long-hash-here . That will pull just this commit into your current branch.

How do you raise merge request for specific commit in Gitlab?

You can, however, create a new branch from your master branch, cherry-pick the single commit, and create a merge request for that branch, containing only the one commit. If you do not need the other commits any more, you can also consider an interactive rebase, to remove the unwanted commits from the branch.

Does a merge count as a commit?

The merge commit is considered a single commit (I'd expect 13 commits now, unless you're not counting merge commits or one of the 5 and 7 is actually the same commit), but it brings all of the histories it merges together.


1 Answers

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)
like image 64
fuma Avatar answered Nov 15 '22 01:11

fuma