Skip to content

Instantly share code, notes, and snippets.

@myobie
Last active January 7, 2020 14:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myobie/aafd9885aeea21aed19b85a60cacda4f to your computer and use it in GitHub Desktop.
Save myobie/aafd9885aeea21aed19b85a60cacda4f to your computer and use it in GitHub Desktop.
Zeit's now preview and prod deploy Actions Workflow – emulates how Zeit's Now GitHub integration works, but is even better because it forces a rebuild even if the source code hasn't changed
name: now
on:
push:
jobs:
deploy-prod:
name: Deploy prod
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/master'
steps:
- name: Installing node
uses: actions/setup-node@v1
with:
node-version: 10.x
# Use the same node.js version as the one Zeit's uses (currently node10.x)
- name: Checking out the code
uses: actions/checkout@v1
- name: Deploying now
run: now -C -f --prod --token "$ZEIT_TOKEN"
env:
ZEIT_TOKEN: ${{ secrets.ZEIT_TOKEN }}
deploy:
name: Deploy preview
runs-on: ubuntu-latest
if: github.ref != 'refs/heads/master'
steps:
- name: Installing node
uses: actions/setup-node@v1
with:
node-version: 10.x
# Use the same node.js version as the one Zeit's uses (currently node10.x)
- name: Checking out the code
uses: actions/checkout@v1
- name: Detect metadata
run: |
IFS='/' read -ra ref_parts <<< "${GITHUB_REF}"
branch_parts=`echo "${ref_parts[@]:2}"`
branch=`echo "${branch_parts// /-}" | tr '[:upper:]' '[:lower:]'`
echo "branch: ${branch}"
u=`now whoami --token "${ZEIT_TOKEN}" | perl -pe 'chomp'`
echo "now user: ${u}"
project=`cat now.json | jq -r .name | tr -d '.' | perl -pe 'chomp'`
echo "now project: ${project}"
scope=`cat now.json | jq -r .scope | tr -d '.' | perl -pe 'chomp'`
if [[ -n "${scope}" ]]; then
echo "now scope: ${scope}"
echo "https://${project}-git-${branch}.${scope}.now.sh" > /tmp/preview_url.txt
else
echo "https://${project}-git-${branch}.${u}.now.sh" > /tmp/preview_url.txt
fi
cat /tmp/preview_url.txt
env:
ZEIT_TOKEN: ${{ secrets.ZEIT_TOKEN }}
- name: Deploying now
run: |
now -C -f -b PREVIEW_URL=`cat /tmp/preview_url.txt` --token "$ZEIT_TOKEN" > /tmp/deployment.txt
cat /tmp/deployment.txt
env:
ZEIT_TOKEN: ${{ secrets.ZEIT_TOKEN }}
- name: Aliasing deployment
run: |
deployment=`cat /tmp/deployment.txt`
preview_url=`cat /tmp/preview_url.txt`
now alias set "${deployment}" "${preview_url}" --token "${ZEIT_TOKEN}"
echo "deployment: ${deployment}"
echo "preview_url: ${preview_url}"
env:
ZEIT_TOKEN: ${{ secrets.ZEIT_TOKEN }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment