如何根据分支名称将Gitlab管道复制到目录

时间:2020-02-05 18:38:20

标签: gitlab-ci

此Gitlab管道作业将构建Gitbook文档并将其复制到某个位置:

build_gitbook:
  variables:
    DOC_DIR: '/var/www/doc'
  script:
    - gitbook build help _book
    - 'rm -rf $DOC_DIR/help'
    - 'mkdir -p $DOC_DIR/help'
    - 'cp -r _book/* $DOC_DIR/help'
  artifacts:
    paths:
      - _book
    expire_in: 10 mins

如何更改此工作,以便在分支为master时将文档复制到/ var / www / doc,而在所有其他分支上将文档复制到/ var / www / dev-doc?

2 个答案:

答案 0 :(得分:1)

如果您可以添加到CI个工作中,并且不受工作数量的限制,则可以使用only/except语法来更改.gitlab-ci.yml

⋮
build_gitbook_master:
    variables:
        DOC_DIR: '/var/www/doc'
    only:
        - master
    script:
        ⋮
        <Your Scripts>
        ⋮
    artifacts:
        ⋮

build_gitbook_others:
    variables:
        DOC_DIR: '/var/www/dev-doc'
    except:
        - master
    script:
        ⋮
        <Your Scripts>
        ⋮
    artifacts:
        ⋮
⋮

您也可以选择GitLab的rules作为替代方法。

答案 1 :(得分:0)

可以这样做。主要脚本位于模板部分。

# The template part that contains the build script it self.
.build_gitbook: &gitbook_build_script
  script:
    - gitbook build common_operations_infos _book
    - 'rm -rf $DOC_DIR/help'
    - 'mkdir -p $DOC_DIR/help'
    - 'cp -r _book/* $DOC_DIR/help'
  artifacts:
    paths:
      - _book
    expire_in: 10 mins

# Builds gitbook on the master branch
build_gitbook_master:
  <<: *gitbook_build_script
  variables:
    DOC_DIR: '/var/www/doc'
  only:
    - master

# Builds gitbook on other branch
build_gitbook_others:
  <<: *gitbook_build_script
  variables:
    DOC_DIR: '/var/www/dev-doc'
  except:
    - master

这仍然不是完美的解决方案,因为这需要两项工作。如果它们以手动模式运行(时间:手动),则开发人员必须根据分支机构选择正确的作业。