javascript - 我尝试使用我的 React 应用程序设置 gitlab 页面,但它显示 404 错误

标签 javascript gitlab gitlab-ci gitlab-pages

我尝试使用我的 React 应用程序设置 gitlab 页面,但是,我无法这样做,因为它没有生成任何 url 供我访问。我已经设置了 gitlab-ci.yml。

> image: node:latest
pages:
  script:
    - npm install
    - npm run build
    - mkdir public2
    - mv public/* public2
  artifacts:
    paths:
      - public2
  only:
    - master
  stage: deploy

最佳答案

为 React 应用程序设置 Gitlab 页面的示例:

第1步:我创建了一个React项目并推送到gitlab

$ npx create-react-app hello-react

这是 repo :https://gitlab.com/ygou/hello-react

第2步:创建.gitlab-ci.yml如下图

https://gitlab.com/ygou/hello-react/blob/master/.gitlab-ci.yml

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

# Announce the URL as per CRA docs
# https://create-react-app.dev/docs/advanced-configuration/
variables:
  PUBLIC_URL: /hello-react

# Cache node modules - speeds up future builds
cache:
  paths:
  - 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:
    - npm install # Install all dependencies
    - npm run build # Build for production
    - 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

工作完成

CI/CD 作业完成后,您可以看到 React 应用程序的 Gitlab 页面:

https://ygou.gitlab.io/hello-react/

引用:https://ohmybuck.com/2018-08-12-build-a-react-website-with-full-cicd-in-two-minutes/

关于javascript - 我尝试使用我的 React 应用程序设置 gitlab 页面,但它显示 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49566102/

相关文章:

macos - 在本地OSX上丢失数据docker gitlab

javascript - Firefox WebExtension内容脚本: TypeError: browser.运行时未定义

javascript - CSS3 Rotate3d 在所需的方向

git - 在 PowerShell 中将 curl REST 调用转换为 Invoke-RestMethod

docker - 如何提取 docker 镜像的所有替代标签?

java - 如何使用 gradle 使 GitLab CI 发布 Spring Boot JAR?

java - LWJGL 使用 GitLab CI 构建

docker - Gitlab CI - 注册表和 nginx

javascript - Angular 2 和自定义下拉菜单

javascript - 如果滚动超过 300vh