Search code examples
gitlabgitlab-cicicd

How to group CI and CD jobs on Gitlab Pipelines


I want to run CI jobs on merge request creation and run CI jobs again and CD jobs when the merge request is merged.

.gitlab-ci.yml:

stages:
  - build # Docker build
    rules:
      - if: $CI_PIPELINE_SOURCE == "merge_request_event"
  - test # Docker run container for test
    rules:
      - if: $CI_PIPELINE_SOURCE == "merge_request_event"
  - push # Docker push to AWS ECR
  - deploy # Docker run container

The build and test stages trigger a pipeline well when merge request is created but when merge request is merged push and deploy stage didn't triggered.

I tried with $CI_PIPELINE_SOURCE == "push" && $CI_MERGE_REQUEST_EVENT_TYPE == "merged_result"condition but doesn't work

What are the rules check conditions for this behaviour ? Any idea?


Solution

  • SOLVED

    Gitlab pipelines rules for build and test stages (Continuous Integration):

      rules:
        - if: $CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == 'develop'
          variables:
            ENVIRONMENT: 'dev'
        - if: ($CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == 'stage') || ($CI_COMMIT_BRANCH == "stage")
          variables:
            ENVIRONMENT: 'stage'
        - if: $CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == 'main' || ($CI_COMMIT_BRANCH == "main")
          variables:
            ENVIRONMENT: 'prod'
    

    Gitlab pipelines rules for push and deploy stages (Continuous Deployment):

    rules:
      - if: $CI_COMMIT_BRANCH == "stage"
        variables:
          ENVIRONMENT: 'stage'
      - if: $CI_COMMIT_BRANCH == "main"
        variables:
          ENVIRONMENT: 'stage'
    

    Take into account to set Develop, Stage and Main branches protected and prevent direct push to canonical branches and set Gitlab Settings -> Merge requests -> Merge checks -> Pipelines must succeed checked