我正在使用 GitLab Pages 部署我的 React 应用程序,并且运行良好。
这是我的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
现在,我刚刚根据我的分支创建了一个开发版本develop
我想要我的 React 应用程序有 2 个版本,并且有 2 个不同的 URL。我怎样才能做到这一点?
例如现在,我有:
my-react-app.com
链接到master
branch
我应该如何拥有
dev.my-react-app.com
甚至my-react-app.gitlab.io
链接到develop
分支?
我已经成功地使用可浏览的工件来实现此目的。在您的示例中,您将为开发分支创建一个作业并设置PUBLIC_URL
到路径gitlab.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"
设置environment
如图所示,会在相关合并请求中生成“查看应用程序”链接,让您只需单击一下即可访问工件。
Note:如果您的存储库位于subgroup https://docs.gitlab.com/ee/user/group/subgroups/index.html,您需要在上面的两个位置之间插入子组名称/-/
and $CI_PROJECT_NAME
以使生成的 URL 正常工作。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)