简体   繁体   English

仅在特定分支上的 GitLab CI 管道

[英]GitLab CI Pipeline on specific branch only

I'm trying to implement GitLab CI Pipelines to build and deploy an Angular app.我正在尝试实施 GitLab CI Pipelines 来构建和部署 Angular 应用程序。 In our project we have two general branches: master (for production only) and develop .在我们的项目中,我们有两个通用分支: master (仅用于生产)和develop For development we create feature/some-feature branches from develop branch.对于开发,我们从develop分支创建feature/some-feature分支。 When development finished, we create merge request from feature/some-feature to develop .开发完成后,我们创建从feature/some-featuredevelop的合并请求。 When merge request approved and merged into develop branch I want to run a Pipeline in order to build application and deploy the build on some environment.当合并请求被批准并合并到develop分支时,我想运行一个管道来构建应用程序并将构建部署在某个环境中。

I use the following setup in .gitlab-ci.yml:我在 .gitlab-ci.yml 中使用以下设置:

image: node:7.5-configured

stages:
    - build
    - deploy

build_job:
    stage: build
    only:
        - develop
    script:
        - /bin/bash <some script here>

...

The problem is that Pipeline executed every time I push into any feature/some-feature branch.问题是每次我推入任何feature/some-feature分支时都会执行管道。 What's wrong with my setup?我的设置有什么问题? How can I force the Pipeline to be executed only when push performed into develop branch directly?当直接推送到develop分支时,如何强制执行管道?

Solution It was my mistake - I had two different .gitlab-ci.yml files in develop branch and feature/some-feature branch.解决方案这是我的错误 - 我在develop分支和feature/some-feature分支中有两个不同的 .gitlab-ci.yml 文件。

这是我的错误——我在develop分支和feature/some-feature分支中有两个不同的 .gitlab-ci.yml 文件,这就是为所有分支执行管道的原因。

Although it has been passed much time on this discussion I would like to suggest an opinion, which I am using most of the time.尽管这个讨论已经过去了很多时间,但我想提出一个意见,我大部分时间都在使用它。

rules:
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "master"'
      when: on_success
      changes:
        - folder/some_folder/**/* 
    - if: '$CI_PIPELINE_SOURCE == "web" && $CI_COMMIT_BRANCH == "development"'
      when: manual
      changes:
        - folder/some_other_folder/**/* 

This structure solved my various problems, I hope it helps you to!这个结构解决了我的各种问题,希望对你有帮助!

Regards.问候。

I would suggest to move the 我建议移动

only:
  - develop

tag after your script tag. script标记后面的标记。

您应该在需要运行 CI 的分支中添加.gitlab-ci.yml文件。

You can also define the rule at the global level instead of job if you want如果需要,您还可以在全局级别而不是作业中定义规则

Here is the example for that这是一个例子

image: python:3.8

stages:
 - test

workflow:
  rules:
    - if: $CI_COMMIT_BRANCH == "development" || ($CI_PIPELINE_SOURCE == 'merge_request_event' && ( $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "development" || $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master" ))

unit_test:
  stage: test
  services:
    - postgres:13.4
  before_script:
    - export PYTHONPATH=$PWD
   script:
    - export AWS_DEFAULT_REGION=us-west-2
    - python -c "import sys;print(sys.path)"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM