https://github.com/statnmap/gitlab-pages-deploy
Bash to deploy GitLab Pages in a branch like gh-pages
https://github.com/statnmap/gitlab-pages-deploy
Last synced: 8 months ago
JSON representation
Bash to deploy GitLab Pages in a branch like gh-pages
- Host: GitHub
- URL: https://github.com/statnmap/gitlab-pages-deploy
- Owner: statnmap
- Created: 2021-12-02T11:14:21.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-09-06T16:41:32.000Z (almost 4 years ago)
- Last Synced: 2025-04-19T19:24:59.357Z (about 1 year ago)
- Language: Shell
- Size: 32.2 KB
- Stars: 6
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# GitLab-Pages-Deploy
Bash to deploy GitLab Pages in a branch like gh-pages
_(Highly inspired from https://github.com/marketplace/actions/gh-pages-deploy)_
It allows to keep track of modifications between to publications
- This creates a branch named "gh-pages"
- This stores the content of your branch "public/" directory inside
Also, if you want to be able to render the website for some specific branches, without deleting the 'main' one, you can use sub-website build. See [Publish one sub-website for each branch](https://github.com/statnmap/GitLab-Pages-Deploy#publish-one-site-for-each-branch)
## Demo
You can find repository with a demo for this script: https://gitlab.com/statnmap/gitlab-pages-demo/-/blob/main/README.md
You'll see Pages with sub-folders of the HTML output for each branch that is worth it.
## Prepare the "gitlab-ci.yml"
On GitLab, you can choose to publish the "gh-pages" branch instead of the public artifacts using this in the ".gitlab-ci.yml":
```yaml
gh-pages-prep:
stage: prepare-deploy
only:
- main
script:
# Deploy a unique site in gh-pages branch,
# or a sub-website for each branch if SITE_BY_BRANCH: "TRUE"
- wget https://raw.githubusercontent.com/statnmap/GitLab-Pages-Deploy/main/deploy_pages_branch.sh
- /bin/bash deploy_pages_branch.sh
pages:
stage: deploy
script:
- echo "deploy"
artifacts:
paths:
- public
only:
# Because we use "deploy_pages_branch", only gh-pages branch needs to be deployed
# All outputs from other branches in "prepare-deploy" step will push in "gh-pages"
- gh-pages
```
Also think about using `except: gh-pages` in your other stages because no file is available for this branch.
## Create your token to allow push
### Create New project token
- Go to: Settings > Access Token
- If it is not activated, use a personal access token: https://gitlab.com/-/profile/personal_access_tokens
- Choose a proper name to recognize it when you'll want to revoke it
- Choose role as "Developer"
- Check access: read_repository and write_repository
- Save your token, you will only see it once

### Add token in the CI/CD variables as `PROJECT_ACCESS_TOKEN`
- Go to: Settings > CI/CD > Variables
- Expand the section
- Add variable
- Fill key with `PROJECT_ACCESS_TOKEN`
- Fill the token with the one you saved above
- Mask variable but you can uncheck Protect variable
- Add variable

## Publish one sub-website for each branch
If you use the environment variable :
```yaml
variables:
SITE_BY_BRANCH: "TRUE"
gh-pages-prep:
stage: prepare-deploy
only:
- master
- main
- production
- validation
script:
# Deploy a unique site in gh-pages branch,
# or a sub-website for each branch if SITE_BY_BRANCH: "TRUE"
- wget https://raw.githubusercontent.com/statnmap/GitLab-Pages-Deploy/main/deploy_pages_branch.sh
- /bin/bash deploy_pages_branch.sh
pages:
stage: deploy
script:
- echo "deploy"
artifacts:
paths:
- public
only:
# Because we use "deploy_pages_branch", only gh-pages branch needs to be deployed
# All outputs from other branches in "prepare-deploy" step will push in "gh-pages"
- gh-pages
```
The 'gh-pages' branch will keep site content of other published branches in a dedicated subdirectory.
This will then create an index file at the root of your website to let you choose which version you want to see.
```html
Index of branch directories
- index.html
- master
- production
- validation
- other-branch
```