问题
I am deploying my React app using GitLab Pages, and it works well.
Here is my 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
I would like to have 2 versions of my React app with 2 different URLs. How can I do that?
For example right now, I have:
my-react-app.com
linked to master
branch
How should I have
dev.my-react-app.com
or even my-react-app.gitlab.io
linked to develop
branch?
回答1:
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). So the path to pages sources would be like 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).
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).
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.
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
- 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
回答2:
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:
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.
回答3:
Every GitLab project can have at most one Pages site. I can't find an explicit reference for this, but the documentation for .gitlab-ci.yml says:
Be aware that Pages are by default branch/tag agnostic and their deployment relies solely on what you specify in
.gitlab-ci.yml
. If you don’t limit thepages
job with the only parameter, whenever a new commit is pushed to whatever branch or tag, the Pages will be overwritten.
Without the only
parameter, updates to any branch publish to the same Pages site, overwriting whatever is there. With the only
parameter, only the provided branch will trigger a Pages build.
来源:https://stackoverflow.com/questions/55596789/deploying-gitlab-pages-for-different-branches