如何在合并请求到主分支时运行GitLab流水线。

huangapple go评论53阅读模式
英文:

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 &#39;HEAD branch&#39; | cut -d&#39; &#39; -f5)
    - apt-get update &amp;&amp; apt-get install -y git
  image: python:3.10
  tags:
    - gitlab-org-docker
  stage: merge_request_checks
  script: 
    - echo &#39;linting models&#39;
    - echo &quot;source default name - $CI_DEFAULT_BRANCH&quot;
    - 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 &#39;^models/&#39;)
    - |
      for file in $CHANGED_FILES
      do
        echo &quot;Linting $file&quot;
        sqlfluff lint &quot;$file&quot;
      done
  rules:
    - if: $CI_PIPELINE_SOURCE == &quot;merge_request_event&quot;
      changes: 
        paths:
          - &#39;models/*&#39;
    - if: &#39;$CI_COMMIT_REF_NAME == &quot;production&quot;&#39;
      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 &#39;main&#39;: unknown revision or path not in the working tree.
Use &#39;--&#39; to separate paths from revisions, like this:
&#39;git &lt;command&gt; [&lt;revision&gt;...] -- [&lt;file&gt;...]&#39;

答案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: &#39;$CI_MERGE_REQUEST_TARGET_BRANCH_NAME == &quot;main&quot; &amp;&amp; $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME =~ /^feature-/&#39;
      changes:
        - my-folder/**/*

huangapple
  • 本文由 发表于 2023年4月17日 13:22:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76031933.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定