I want to implement a workflow in gitlab that runs when a pr is raised to the main
branch. I used the below script but it is not working as expected. Also, I want (I want it to compare the current feature branch with the main branch for changes, is it possible?).
lint_files:
before_script:
- export CI_DEFAULT_BRANCH=$(git remote show origin | grep 'HEAD branch' | cut -d' ' -f5)
- apt-get update && apt-get install -y git
image: python:3.10
tags:
- gitlab-org-docker
stage: merge_request_checks
script:
- echo 'linting models'
- echo "source default name - $CI_DEFAULT_BRANCH"
- git fetch --depth=1 origin $CI_MERGE_REQUEST_TARGET_BRANCH_NAME:$CI_MERGE_REQUEST_TARGET_BRANCH_NAME
- git checkout $CI_MERGE_REQUEST_TARGET_BRANCH_NAME
- pip install sqlfluff==1.3.2
- CHANGED_FILES=$(git diff --name-only ${CI_DEFAULT_BRANCH} ${CI_COMMIT_REF_NAME} | grep '^models/')
- |
for file in $CHANGED_FILES
do
echo "Linting $file"
sqlfluff lint "$file"
done
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
changes:
paths:
- 'models/*'
- if: '$CI_COMMIT_REF_NAME == "production"'
when: never
I want this job to be triggered when a merge request is raised to main
branch and there are changes in models/
folder. I have issues with both of these conditions. I am unable to trigger the job for prs to main. And the changes
is checking for the current commit but not the whole feature branch. Can anyone help me to achieve the required functionality.
Also, the above script fails with the error which needs to be fixed in addition to above two conditions:
fatal: ambiguous argument 'main': unknown revision or path not in the working tree. Use '--' to separate paths from revisions, like this: 'git <command> [<revision>...] -- [<file>...]'
I think the syntax is different.
Try changing the rules like the following:
rules:
- if: '$CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "main" && $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME =~ /^feature-/'
changes:
- my-folder/**/*