使用Hugo框架为不同分支部署GitLab页面?

问题描述 投票:0回答:1

我正在使用 Hugo 框架来部署 GitLab 页面。正如另一篇文章中所述,似乎可以为多个分支发布页面。

我尝试集成帖子中提供的代码,但不起作用。

我原来的

gitlab-ci.yml
看起来像:

image: registry.gitlab.com/pages/hugo/hugo_extended:latest

variables:
  GIT_SUBMODULE_STRATEGY: recursive

before_script:
  - apk add --update --no-cache git

test:
  script:
  - hugo --gc --minify
  except:
  - master

pages:
  script:
  - hugo --gc --minify
  artifacts:
    paths:
    - public
  only:
  - master

使用hugo时是否可以为每个分支部署页面?我需要改变什么才能实现这一目标?

git gitlab-ci pipeline hugo gitlab-pages
1个回答
0
投票

下面的解决方案,来自 GitLab Pages per Branch:提供预览页面的不折不扣的黑客技术,可与 Hugo 或任何生成框架配合使用。

然后您就可以拥有以下功能:

  • main
    内容暴露在
    $CI_PAGES_URL
    上,路径可通过
    $CURRENT_CONTENT_PATH
  • 配置
  • 每个分支的预览内容公开在
    $CI_PAGES_URL/preview
    上,并通过主页轻松导航到分支内容
  • 根预览文件夹的路径可使用
    $EPHEMERAL_BRANCHES_PATH
    变量进行配置,以通过混淆隐藏预览内容
  • 生成的页面与环境相关联,以利用分支删除时的自动清理功能
  • 为了避免干扰已有的环境,页面环境放置在
    pages
    文件夹下
  • 如果当前缓存中尚未生成
    main
    内容,或者缓存已被删除,则会触发错误,避免误删除
  • 可以使用任何缓存路径作为输入手动触发删除作业,以清理过时的数据
  • 可以安全地将代码添加到现有项目管道中,而不会导致现有作业出现问题
  • 如果您已有自己的
    workflow:rules
    ,则可以将其删除,或进行更新以匹配您的流程
  • 作业必须命名为
    pages
    并且工件必须是要部署到 GitLab Pages 的
    public
    文件夹(或者您可以使用 pages:publish 关键字)
workflow:
  rules: # disable tag pipelines and duplicate MR pipelines
    - if: $CI_COMMIT_BRANCH

variables:
  EPHEMERAL_BRANCHES_PATH: preview # subpath to ephemeral branches content for preview, anything will work

pages:
  stage: build
  image: alpine:3.18
  cache:
    key: gitlab-pages
    paths: [public]
  before_script:
    # default available 'tree' app in alpine image does not work as intended
    - apk add tree
    # CURRENT_CONTENT_PATH is defined in rules, different between main branch and ephemeral branches
    - mkdir -p public/$CURRENT_CONTENT_PATH && ls public/$CURRENT_CONTENT_PATH/..
    - | # avoid deleting main branch content when cache has been erased
      if [ "$CI_COMMIT_BRANCH" != "$CI_DEFAULT_BRANCH" ] && [ ! -d public/$CI_DEFAULT_BRANCH ]; then
        echo -e "💥\e[91;1m Unable to retrieve $CI_DEFAULT_BRANCH generated files from cache ; please regenerate $CI_DEFAULT_BRANCH files first\e[0m"
        exit 1
      fi
    - rm -rf public/$CURRENT_CONTENT_PATH || true # remove last version of current branch
  script:
    - ./generate-my-html.sh --output build-docs || true # insert here your code that generates documentation
    - mv --verbose build-docs public/$CURRENT_CONTENT_PATH
    - cd public/$EPHEMERAL_BRANCHES_PATH
    - tree -d -H '.' -L 1 --noreport --charset utf-8 -T "Versions" -o index.html # generate a root HTML listing all previews for easier access
  environment:
    name: pages/$CI_COMMIT_BRANCH
    action: start
    url: $CI_PAGES_URL/$CURRENT_CONTENT_PATH
    on_stop: pages-clean-preview
  rules:
    # 'main branch' is exposed at GitLab Pages root
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      variables:
        CURRENT_CONTENT_PATH: "."
    # other (short-lived) branches generation are exposed in 'EPHEMERAL_BRANCHES_PATH/branch-name-sanitized' sub path
    - variables:
        CURRENT_CONTENT_PATH: $EPHEMERAL_BRANCHES_PATH/$CI_COMMIT_REF_SLUG
  artifacts:
    paths: [public]
    expire_in: 1h

pages-clean-preview:
  stage: build
  image: alpine:3.18
  cache:
    key: gitlab-pages
    paths: [public]
  variables:
    GIT_STRATEGY: none # git files not available after branch deletion
    FOLDER_TO_DELETE: preview/$CI_COMMIT_BRANCH # an indirection to allow arbirtraty deletion when launching this job
  script:
    - rm -rf public/$FOLDER_TO_DELETE
  environment:
    name: pages/$CI_COMMIT_BRANCH
    action: stop
  rules:
    - if: $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH
      when: manual
      allow_failure: true
© www.soinside.com 2019 - 2024. All rights reserved.