简体   繁体   English

为不同的分支部署 GitLab 页面

[英]Deploying GitLab pages for different branches

I am deploying my React app using GitLab Pages, and it works well.我正在使用 GitLab Pages 部署我的 React 应用程序,它运行良好。

Here is my gitlab-ci.yml :这是我的gitlab-ci.yml

# Using the node alpine image to build the React app
image: node:alpine

# Announce the URL as per CRA docs
# https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/template/README.md#advanced-configuration
variables:
  PUBLIC_URL: /
# Cache node modules - speeds up future builds
cache:
  paths:
  - client/node_modules

# Name the stages involved in the pipeline
stages:
  - deploy

# Job name for gitlab to recognise this results in assets for Gitlab Pages
# https://docs.gitlab.com/ee/user/project/pages/introduction.html#gitlab-pages-requirements
pages:
  stage: deploy
  script:
    - cd client
    - npm install # Install all dependencies
    - npm run build --prod # Build for prod
    - cp public/index.html public/404.html # Not necessary, but helps with https://medium.com/@pshrmn/demystifying-single-page-applications-3068d0555d46
    - mv public _public # CRA and gitlab pages both use the public folder. Only do this in a build pipeline.
    - mv build ../public # Move build files to public dir for Gitlab Pages
  artifacts:
    paths:
    - public # The built files for Gitlab Pages to serve
  only:
    - master # Only run on master branch

Now, I just created a dev version, based on my branch develop现在,我刚刚创建了一个开发版本,基于我的分支develop

I would like to have 2 versions of my React app with 2 different URLs.我想要我的 React 应用程序的 2 个版本和 2 个不同的 URL。 How can I do that?我怎样才能做到这一点?

For example right now, I have:例如现在,我有:

my-react-app.com linked to master branch my-react-app.com链接到master分支

How should I have我应该怎么拥有

dev.my-react-app.com or even my-react-app.gitlab.io linked to develop branch?< dev.my-react-app.com甚至my-react-app.gitlab.io链接到develop分支?<

I've had success using the browsable artifacts for this purpose.为此,我已经成功地使用了可浏览的工件。 In your example, you would create a job for your develop branch and set the PUBLIC_URL to the path on gitlab.io where the job's artifacts are published:在您的示例中,您将为您的开发分支创建一个作业,并将 PUBLIC_URL 设置为PUBLIC_URLgitlab.io作业工件的路径:

develop:
    artifacts:
        paths:
          - public

    environment:
        name: Develop
        url: "https://$CI_PROJECT_NAMESPACE.gitlab.io/-/$CI_PROJECT_NAME/-/jobs/$CI_JOB_ID/artifacts/public/index.html"

    script: |
        # whatever

    stage: deploy

    variables:
        PUBLIC_URL: "/-/$CI_PROJECT_NAME/-/jobs/$CI_JOB_ID/artifacts/public"

Setting the environment as indicated produces a »Review app« link in relevant merge requests, allowing you to get to the artifacts with a single click.按指示设置environment会在相关合并请求中生成一个»Review app«链接,让您只需单击一下即可访问工件。

Note : if your repository is in a subgroup , you need to insert the subgroup name in two places above above between /-/ and $CI_PROJECT_NAME for the resulting URLs to work.注意:如果您的存储库位于子组中,则需要在上面的/-/$CI_PROJECT_NAME之间的两个位置插入子组名称,以使生成的 URL 正常工作。

It is possible to keep several pages published for different pipelines/branches.可以为不同的管道/分支发布多个页面。

To do that you need to copy your pages content (basically test report, or whatever needs to be published) to specific unique directory in public folder.为此,您需要将页面内容(基本上是测试报告,或任何需要发布的内容)复制到公用文件夹中的特定唯一目录。 For example, the name of the directory can be the id of pipeline (CI_PIPELINE_ID).例如,目录的名称可以是管道的 id (CI_PIPELINE_ID)。 So the path to pages sources would be like public/$CI_PIPELINE_ID/ .所以页面源的路径就像public/$CI_PIPELINE_ID/

Then the whole public folder should be defined as artifacts with specific unique name (here again "$CI_PIPELINE_ID" can be used).然后应将整个公用文件夹定义为具有特定唯一名称的工件(此处再次可以使用“$CI_PIPELINE_ID”)。

Unique name for artifacts is needed to not override the artifacts with the next pipeline execution (if name is not specified, the default name will be taken https://docs.gitlab.com/ee/ci/yaml/#artifactsname ).工件的唯一名称需要在下一次管道执行时不覆盖工件(如果未指定名称,则将采用默认名称https://docs.gitlab.com/ee/ci/yaml/#artifactsname )。

Then you can access the published report via the link:然后您可以通过以下链接访问已发布的报告:

https://yourGitlab/yourNamespace/yourProjectName/{CI_PIPELINE_ID}/index.html

, that means you can access all your saved reports by changing the pipeline id. ,这意味着您可以通过更改管道 ID 来访问所有已保存的报告。

My example:我的例子:

stages:
  - publish

cache:
  # Required to keep artifacts from old builds, e.g. from master
  paths:
    - public

pages:
  stage: publish
  script:
    - mkdir -p public/$CI_PIPELINE_ID
    - cp target/site/allure-maven-plugin/* public/$CI_PIPELINE_ID/ -R
  artifacts:
    name: "$CI_PIPELINE_ID"
    paths:
      - public
    expire_in: 5 days
  when: always

Every GitLab project can have at most one Pages site.每个 GitLab 项目最多可以有一个 Pages 站点。 I can't find an explicit reference for this, but the documentation for .gitlab-ci.yml says:我找不到明确的参考,但.gitlab-ci.yml的文档说:

Be aware that Pages are by default branch/tag agnostic and their deployment relies solely on what you specify in .gitlab-ci.yml .请注意,默认情况下,页面与分支/标签无关,它们的部署仅依赖于您在.gitlab-ci.yml中指定的内容。 If you don't limit the pages job with theonly parameter , whenever a new commit is pushed to whatever branch or tag, the Pages will be overwritten.如果您不使用only参数限制pages作业,则每当将新提交推送到任何分支或标签时,Pages 将被覆盖。

Without the only parameter, updates to any branch publish to the same Pages site, overwriting whatever is there.如果没有only的参数,任何分支的更新都会发布到同一个Pages 站点,覆盖那里的任何内容。 With the only parameter, only the provided branch will trigger a Pages build.使用only参数,只有提供的分支才会触发 Pages 构建。

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

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