英文:
How to run gitlab pipelines on merge request to main
问题
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 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 the main
branch and there are changes in the 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 achieve the required functionality?
Also, the above script fails with the error that needs to be fixed, in addition to the 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 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>...]'
答案1
得分: 1
我认为语法不同。
尝试将规则更改为以下内容:
规则:
- 如果:'$CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "main" && $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME =~ /^feature-/'
更改:- my-folder/**/*
英文:
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/**/*
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论