https://gitlab.com/spiffydesign – main
https://gitlab.com/lab555
https://gitlab.com/lab555/plainhtml – remaned to: https://lab555.gitlab.io/
PAGES URL: https://lab555.gitlab.io/plainhtml/
Renamed Project
– https://gitlab.com/lab555/pages-lab555NEW PAGE URL: https://lab555.gitlab.io/pages-lab555
React Lessons (group)
- LESSON ONE: https://react-lessons.gitlab.io/scoreboard/
React to Gitlab Pages
Here are the links for the completed project for reference: Github – https://github.com/jtorbett23/gitlab-pages-react-example Gitlab – https://gitlab.com/jtorbett23/gitlab-pages-react-example Gitlab Pages url – https://jtorbett23.gitlab.io/gitlab-pages-react-example/
https://dev.to/jtorbett23/react-gitlab-pages-42l6
React + Gitlab Pages
Introduction
In this tutorial we will be showing you how to deploy a react application with a Gitlab deployment pipeline to Gitlab pages.
Prerequisites
For this tutorial you will need to have the following setup
- Github Account – https://github.com/join
- Gitlab Account – https://gitlab.com/users/sign_up
- git – https://git-scm.com/downloads
- node and npm – https://nodejs.org/en/download/
Check git, node & npm have installed properly with:
git --version
node --version
npm --version
Creating our react app
In a directory of your choice create a react app with the following command
npx create-react-app gitlab-pages-react-example
(Feel free to change gitlab-pages-react-example to whatever name you desire)
Enter your new react project folder
cd gitlab-pages-react-example
You can use npm start
to check your project runs properly and you should see the following at http://localhost:3000
Upload your react app to Github
Create an empty public repository on Github. Then to your local react project and enter these commands to push your code to Github
git remote add origin https://github.com/alethal/ns-simplified.git
(You will need to replace https://github.com/alethal/ns-simplified.git with the link to your repository)
HEAD – BRANCH – should be named “master”
or there will be issues getting it to deploy to github pages.git push -u origin master
You should now see your react application on your Github repository:
Linking Gitlab and Github
Login to your Gitlab account and create a new project choosing "Run CI/CD for external repository" selecting the repo we created earlier
Sync Gitlab to Github (easy)
https://dev.to/brunorobert/github-and-gitlab-sync-44mnThis one is simple. You could set up some CI/CD yourself. But Gitlab will automatically do this for you.
- Go to “Settings > Repository > Mirroring repositories” (also for Run CI/CD pipelines for external repositories)
- Enter your Github repo with your username in front https://github username@github.com/path/to/your/repo.git
- In the password field, enter your Github token
- Select push (this requires a subscription)
- Press Mirror repository
- From now on, changes to Gitlab will be mirrored to Github
Once created open your project and go to Settings > General > Visibility, project features, permissions. Then check that Gitlab pages is allowed
Creating our deployment pipeline
For gitlab to create a pipeline to deploy code it requires a
.gitlab-ci.yml
file at the root level of project.
(Read more gitlab yaml here – https://docs.gitlab.com/ee/ci/yaml/)
Here is the .gitlab-ci.yml
we will be starting with:
image: node
pages:
stage: deploy
cache:
paths:
- node_modules/
script:
- npm install
- npm run build
- rm -rf public
- cp build/index.html build/404.html
- mv build public
artifacts:
paths:
- public
only:
- master
Images
image: node
defines node as the type of image that docker will use, allowing use to access npm easily.
Note: if you require a specific node version that can be specified by adding :NODE-VERSION e.g image: node:10.16.3
Stages
pages:
stage: deploy
This setups a stage for our pipeline, in which we can execute various scripts. For Gitlab pages we need to name this stage "pages" so it deploys content to the correct place.
Caching
cache:
paths:
- node_modules/
Caches our node_modules so we don't need to download dependencies every time we run our pipeline.
Scripts
scripts:
allows use to run scripts through a terminal
npm install
first installs/updates our dependenciesnpm run build
the builds our project into a build folderrm -rf public
will remove the public folder as we need to use the namespace public to help Gitlab pages recognise our site contentcp build/index.html build/404.html
as react is a single page app we set the 404 page to a copy of our index.html to handle errors through index.htmlmv build public
copy the built project from build to public to allow Gitlab pages to recognise it once deployed
Artifacts
artifacts:
paths:
- public
Artifacts are the output of a pipeline stage and we output our public folder holding our built site.
Restricting pipeline stages
only:
- master
only
lets us set what code branches of project will run this pipeline stage, it is set to master so we don't push development code to our site.
Deploying to Gitlab pages
Once your .gitlab-ci.yml
has been pushed to your Github repo Gitlab will sync these changes and run a pipeline based off it.
Your root folder should look like:
-public
-src
-.gitignore
-.gitlab-ci.yml
-package-lock.json
-package.json
On Gitlab navigate to CI/CD > Pipelines and you should see a pipeline has been triggered. Once your pipeline is completed it should look like this:
After your pipeline is complete you will be able to view your site by navigating to Settings > Pages and clicking the url under "Access pages"
Your page should look like this:
Note: we are now have the padlock so are using https
If you instead just get a white page, you will need to edit your package.json
and add the "homepage" attribute. For my project my url is https://jtorbett23.gitlab.io/gitlab-pages-react-example/
, so i will need to set my homepage as "gitlab-pages-react-example".
{
"homepage": "gitlab-pages-react-example",
"name": "gitlab-pages-react-example",
"version": "0.1.0",
...
You will need to wait for new pipeline to successfully run before seeing the changes.
Deploying a change from our local machine
Now let's make a change to locally to see if updates our site automatically.
Change the text in src/App.js
from
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
to whatever you would like e.g
<p>
Deployment pipeline is working :)
</p>
Push these changes and it should trigger your deployment pipeline. After it has finished you should see your changes on Gitlab Pages
Improving our pipeline
Currently our pipeline only consists of a single step which means when we are developing further down the line it will be hard to know why our pipeline is failing.
So we are going to separate our pipeline into three stages: build, test and deploy.
Build
build:
stage: build
cache:
paths:
- node_modules/
script:
- npm install
- npm run build
artifacts:
paths:
- build
Here we install and cache our dependencies and then build the project outputted as an artifact to be accessible by other stages.
Test
test:
stage: test
cache:
paths:
- node_modules/
policy: pull
script:
- npm run test
Here we use the cached dependencies through the policy: pull
to run our test scripts.
For this stage we will also need to update test script in the package.json
to make sure it finds all our test scripts.
...
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --watchAll=false",
"eject": "react-scripts eject"
}
...
This is done by adding the option --watchAll=false
.
Deployment
pages:
stage: deploy
dependencies:
- build
script:
- rm -rf public
- cp build/index.html build/404.html
- mv build public
artifacts:
paths:
- public
Here we take the built project from the build stage's artifact and then deploy the code as normal.
Here is the final .gitlab-ci.yml
:
image: node
build:
stage: build
cache:
paths:
- node_modules/
script:
- npm install
- npm run build
artifacts:
paths:
- build
test:
stage: test
cache:
paths:
- node_modules/
policy: pull
script:
- npm run test
pages:
stage: deploy
dependencies:
- build
script:
- rm -rf public
- cp build/index.html build/404.html
- mv build public
artifacts:
paths:
- public
only:
- master
Deploying to Gitlab pages – Blank White Page – once deployed to pages?
To deploy this project to Gitlab pages first fork this project.Then create a new Gitlab project and choose “Run CI/CD for external repository”.
Once your first pipeline has finished running go to Settings > Pages to find the url the page is hosted.
If you only see a whitescreen make sure to edit the “homepage” attribute in the package.json to match your url.
For example my url is “https://jtorbett23.gitlab.io/gitlab-pages-react-example/”, so my homepage will need to be “gitlab-pages-react-example”.
You need to add this attribute (if not already) at the top of package.json
, and make sure it is pointing to the gitlab pages url for your site.
88%
In your package.json homepage
is not correct, so it is messing up the build. Change
to
Wrap up
You have now learned how to deploy a react application from Github to Gitlab pages using Gitlab CI/CD pipeline.
Here are the links for the completed project for reference: Github – https://github.com/jtorbett23/gitlab-pages-react-example Gitlab – https://gitlab.com/jtorbett23/gitlab-pages-react-example Gitlab Pages url – https://jtorbett23.gitlab.io/gitlab-pages-react-example/