andrey
andrey

Reputation: 23

Excluding gitlab-ci.yml from merge-request

I have 2 branches: stage and master - and each of them has its own .gitlab-ci.yml with different scripts. How is it possible to exclude these files from merge-request, so that they remain unique for each of the branches?

i tried with gitattributes and merge strategy - but this method only works locally. Then I decided to create one common gitlab-ci.yml for all branches, but in this case it is necessary to set the conditions for the execution of different jobs for different branches. I tried in the following way:

runTests_job:
  stage: runTests

  tags:
  - ps
  
  rules:
  - if: $CI_COMMIT_BRANCH == "stage"
  
  script:
  - chcp 65001
  - Invoke-Sqlcmd -ConnectionString $CONNSTR -Query "EXEC dbo.test" 

but the job was still running in the master branch. Also tried:


runTests_job:
  stage: runTests

  tags:
  - ps
  
  only:
    refs:
      - stage
  
  script:
  - chcp 65001
  - Invoke-Sqlcmd -ConnectionString $CONNSTR -Query "EXEC dbo.test" 

but it didn't work. Maybe I made a mistake in the syntax somewhere?

Upvotes: 2

Views: 699

Answers (1)

Harshit Ruwali
Harshit Ruwali

Reputation: 1088

You can use make custom variables and conditions when the pipeline should run. You can check from which branch the commit is been made and the accordingly you can check and run the script.

variables:
  DEPLOY_VARIABLE: "default-deploy"

workflow:
  rules:
    - if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH # master
      variables:
        DEPLOY_VARIABLE: "deploy-master"  # Override DEPLOY_VARIABLE
    - when: always # Run pipeline in other cases

job1:
  rules:
    - if: $DEPLOY_VARIABLE == "deploy-master"
        # run the script
        script:
          - chcp 65001
          - Invoke-Sqlcmd -ConnectionString $CONNSTR -Query "EXEC dbo.test" 
    
job2:
  rules:
    - if: $DEPLOY_VARIABLE == "deploy-staging"
        # run the script
        script:
          - chcp 65001
          - Invoke-Sqlcmd -ConnectionString $CONNSTR -Query "EXEC dbo.test" 

Just a heads-up the syntax could be a bit wrong. But you can use the concept to proceed further.

You might also want to refer to the docs: https://docs.gitlab.com/ee/ci/yaml/

Upvotes: 2

Related Questions