Skip to content
This repository was archived by the owner on Dec 5, 2022. It is now read-only.

Replace GH_PAT with built-in GITHUB_TOKEN #18

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
2 changes: 1 addition & 1 deletion .github/main.workflow
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ action "Deploy to GitHub Pages" {
env = {
BUILD_DIR = "public/"
}
secrets = ["GH_PAT"]
secrets = ["GITHUB_TOKEN"]
}
13 changes: 2 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,8 @@ This action isn't that, though I've borrowed much of the git action from these w

## Secrets

<img src="https://github.com/maxheld83/ghpages/blob/master/action-config.png?raw=true" align="right" width=200/>

Deployment to GitHub pages happens by `git push`ing to the `gh-pages` (or `master`) branch.
To authorise this, the GitHub action needs a secret.
For now, somewhat confusingly, the `GITHUB_TOKEN` [available for every repo](https://developer.github.com/actions/creating-workflows/storing-secrets/) *does* suffice to push to `gh-pages`, but *does not* suffice to trigger a page build on GitHub, or even propagate the content to the GitHub content-delivery network.

You therefore **have to [create a custom Personal Access Token (PAT)](https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/)** much like you'd do for external services (say, Travis).
You then have to paste this token into the GitHub UI as a secret under the name `GH_PAT` (repository settings/secrets) and call it in the action as in the below.

I've asked GitHub to streamline this process.
The discussion is documented [here](https://github.com/maxheld83/ghaction-ghpages/issues/1).
To authorise this, the GitHub action needs access to the `GITHUB_TOKEN` secret.


## Environment Variables
Expand All @@ -59,6 +50,6 @@ action "Deploy to GitHub Pages" {
env = {
BUILD_DIR = "public/"
}
secrets = ["GH_PAT"]
secrets = ["GITHUB_TOKEN"]
}
```
Binary file removed action-config.png
Binary file not shown.
Binary file modified action-in-use.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#!/bin/sh

set -e
set -u

echo "#################################################"
echo "Changing directory to 'BUILD_DIR' $BUILD_DIR ..."
cd $BUILD_DIR

echo "#################################################"
echo "Now deploying to GitHub Pages..."
REMOTE_REPO="https://${GH_PAT}@github.com/${GITHUB_REPOSITORY}.git" && \
REMOTE_REPO="https://${GH_PAT:-"x-access-token:$GITHUB_TOKEN"}@github.com/${GITHUB_REPOSITORY}.git" && \
REPONAME="$(echo $GITHUB_REPOSITORY| cut -d'/' -f 2)" && \
OWNER="$(echo $GITHUB_REPOSITORY| cut -d'/' -f 1)" && \
GHIO="${OWNER}.github.io" && \
Expand All @@ -27,6 +28,7 @@ fi && \
git add . && \
git commit -m 'Deploy to GitHub pages' && \
git push --force $REMOTE_REPO master:$REMOTE_BRANCH && \
curl -XPOST -H"Authorization: token ${GITHUB_TOKEN}" -H"Accept: application/vnd.github.mister-fantastic-preview+json" https://api.github.com/repos/${GITHUB_REPOSITORY}/pages/builds && \ # https://git.io/fjsFk
rm -fr .git && \
cd $GITHUB_WORKSPACE && \
echo "Content of $BUILD_DIR has been deployed to GitHub Pages."