Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tokens): update to use tokens differently #1

Merged
merged 1 commit into from
Jan 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,27 @@ This action publishes a gitbook to github pages.
The source branch is `master`, and the target branch is `gh-pages`. If `gh-pages` does not exist, it will be created automatically.

## How to Use

```yml
uses: tuliren/publish-gitbook@v1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
jobs:
build:
name: Build Gitbook
runs-on: ubuntu-latest
steps:
# We must check out the repo first
- name: Checkout
uses: actions/checkout@v2
# Now run this publish action
- name: Publish
uses: tuliren/publish-gitbook@v0.9.1
with:
personal_token: ${{ secrets.PERSONAL_TOKEN }}
```

## Gotcha
Unfortunately auto `GITHUB_TOKEN` in workflows cannot trigger a GitHub Pages build. So this action is currently useless. More details can be found here:

Unfortunately auto `GITHUB_TOKEN` in workflows cannot trigger a GitHub Pages build. So the current workaround is to generate a new Personal Access Token and supply it separately as `personal_token`.

More details can be found here:
- [Github action not triggering gh-pages upon push](https://github.community/t5/GitHub-Actions/Github-action-not-triggering-gh-pages-upon-push/td-p/26869)
- [Cannot deploy with GITHUB_TOKEN for new repository](https://github.com/peaceiris/actions-gh-pages/issues/9)
37 changes: 28 additions & 9 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
#!/bin/sh -l

print_info(){
echo "\033[32mINFO \033[0m $@" > /dev/stderr
}

print_error(){
echo "\033[31mERROR\033[0m \a $@" > /dev/stderr
}

PUBLISHER_REPO=""
# setup publisher
if [ -n "${INPUT_PERSONAL_TOKEN}" ]; then
print_info "using provided PERSONAL_TOKEN"
PUBLISHER_REPO="https://x-access-token:${INPUT_PERSONAL_TOKEN}@github.com/${GITHUB_REPOSITORY}.git"
elif [ -n "${GITHUB_TOKEN}" ]; then
print_info "using automatic GITHUB_TOKEN"
PUBLISHER_REPO="https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git"
else
print_error "no PERSONAL_TOKEN or GITHUB_TOKEN found, you can provide one in your workflow YAML"
exit 1
fi

# config git
git config --local user.name "${GITHUB_ACTOR}"
git config --local user.email "${GITHUB_ACTOR}@users.noreply.github.com"

# check github token
[ -z "${INPUT_GITHUB_TOKEN}" ] && {
echo 'Missing input "github_token: ${{ secrets.GITHUB_TOKEN }}".';
exit 1;
};

# checkout gh-pages branch
set +e
git checkout gh-pages || git checkout -b gh-pages
Expand All @@ -18,17 +33,22 @@ git status
set -e

# install gitbook
print_info "installing gitbook-cli"
npm install gitbook-cli -g
gitbook -v

print_info "installing gitbook plugins"
gitbook --version
gitbook install

# build gitbook
print_info "buildling gitbook"
gitbook build

# copy the static site files into the current directory
cp -R _book/* .

# remove 'node_modules' and '_book' directory
print_info "cleaning artifacts"
git clean -fx node_modules
git clean -fx _book

Expand All @@ -39,9 +59,8 @@ git add .
COMMIT_MESSAGE="Update gitbook `date '+%Y-%m-%d %H:%M:%S'`"
git commit -a -m "${COMMIT_MESSAGE}"

# setup publisher
PUBLISHER_REPO="https://${GITHUB_ACTOR}:${INPUT_GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git"
git remote add publisher ${PUBLISHER_REPO}

# push to the publisher
print_info "pushing to gh-pages branch"
git push -q -u publisher gh-pages