How to run a gitlab-ci.yml job only on a tagged branch?

前端 未结 5 1916
不知归路
不知归路 2020-12-23 16:24

How do I run a .gitlab-ci.yml job only on a tagged Master branch?

job:
  script:
  - echo \"Do something\"
  only:
  - master
  - tags

The

相关标签:
5条回答
  • 2020-12-23 16:31

    My solutions was

    job:
      script:
      - echo "Do something"
      only: 
        refs:
          - master
          - tags
        variables:
          - $CI_COMMIT_BRANCH == "master"
    
    0 讨论(0)
  • 2020-12-23 16:36

    Thanks to others like Matt Alioto who posted about the open issue (which is labeled Product Vision 2019 so hopefully they knock it out this year).

    Specific to Carlson Cole's question, this would work:

    job_for_master_no_tags:
      stage: deploy
      script:
      - echo "Release to Staging"
      only:
      - master
    
    job_for_master_tags_only:
      stage: deploy
      script:
      - echo "Release to Production"
      only:
      - tags
      except:
      - /^(?!master).+@/    # Ruby RegEx for anything not starting with 'master'
    
    • To see how this RegEx works check out https://rubular.com/r/1en2eblDzRP5Ha
    • I tested this on GitLab version 11.7.0 and it works
      • Note: If you try to use - /^(?!master).+/ (without the @) it doesn't work - learned that the hard way
    0 讨论(0)
  • 2020-12-23 16:39

    This behavior is not yet supported by gitlab-ci, although there is an open issue to add it.

    In the meantime I've also heard anecdotal reports that

    only:
      - master
    only:
      - tags
    

    will get the job done (as well as anecdotal reports that it won't).

    0 讨论(0)
  • 2020-12-23 16:44

    I made it work and this is my working code snippet, all others were not working for me

    only:
     - tags  # please mention the 's' compared to Sergio Tomasello's solution
    except:
     - branches
    

    I use 11.4.3

    0 讨论(0)
  • 2020-12-23 16:50

    This behavior will be introduced in version 12.

    Open issue was recently update:

    Jason Lenny @jlenny changed title from {-Update .gitlab-ci.yml to support conjunction logic for build conditions-} to Conjunction logic for build conditions MVC · 2 days ago

    Jason Lenny @jlenny changed milestone to 12.0 · 2 days ago

    (fingers crossed)

    A solution is to use the except keyword to exclude all the branches, in conjunction with only to run on tags, in this way you run your pipeline only on tag in master branch:

      only:
        - tags
      except:
        - branches
    

    I'm using version 11.3.4

    0 讨论(0)
提交回复
热议问题