Skip to content

Instantly share code, notes, and snippets.

@pinkkis
Last active November 24, 2019 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pinkkis/a9df6cfa96d6575b0ec5af71df58c48a to your computer and use it in GitHub Desktop.
Save pinkkis/a9df6cfa96d6575b0ec5af71df58c48a to your computer and use it in GitHub Desktop.
Node CI/CD with build and test, and GH Pages static page publish. Implements npm dependencies caching and pr/push differentiation
name: Node CI/CD
on:
pull_request:
branches:
- master
push:
branches:
- master
jobs:
build:
env:
CI: true
runs-on: ubuntu-latest
steps:
- name: Check publish secret exists
run: |
if [ -z "${{secrets.PERSONAL_TOKEN}}" ]
then
echo "PERSONAL_TOKEN secret not present"
exit 1
fi
- name: Checkout
uses: actions/checkout@v1
- name: Use Node.js 12.x
uses: actions/setup-node@v1
with:
node-version: '12.x'
- name: Cache NPM dependencies
uses: actions/cache@v1
with:
path: ~/.npm
key: ${{ runner.OS }}-build-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.OS }}-build-${{ env.cache-name }}-
${{ runner.OS }}-build-
${{ runner.OS }}-
- name: Install NPM dependencies
run: npm ci
- name: NPM Build
run: npm run build --if-present
- name: NPM Test
run: npm test --if-present
if: success()
- name: Git GH-Pages Deploy
if: github.event_name == 'push' && success()
run: |
cd $PUBLISH_DIR
touch .nojekyll
git init
git config user.name github_action
git config user.email github_action@github.com
git config commit.gpgsign false
git add .
git commit -m "Automated GH-Pages Deploy by $GITHUB_ACTOR"
git push --force --quiet https://${{secrets.PERSONAL_TOKEN}}@github.com/$GITHUB_REPOSITORY.git ${GITHUB_REF##*/}:$TARGET_BRANCH
env:
TARGET_BRANCH: gh-pages
PUBLISH_DIR: dist
@pinkkis
Copy link
Author

pinkkis commented Nov 24, 2019

You need to add a PERSONAL_TOKEN to the repo secrets with (repo) permissions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment