This is a Node module and command line utility for getting the correct URL of the Now deployment automatically created by Now's GitHub integration. Given a git directory with a GitHub remote and a git "ref" (a commit SHA or branch name), it uses the GitHub and Now REST APIs to:
- Get the commit status created by Now for the given ref, the URL of which contains the deployment id; then
- Get the deployment info from the Now API, which tells us the hostname for the given deployment id.
In other words:
repo + ref → GitHub API → commit status → deployment id → Now API → hostname
This tool makes it easy to alias Now deployments for each branch after tests pass on your CI provider. For instance, in Travis:
-
Run
npm install --save --dev now-github-url
-
Add a
script/deploy
script that does the following:branch="$TRAVIS_BRANCH" alias=$(now-github-url --ref="$TRAVIS_COMMIT") # or hard-code the prefix if you don't have a "name" field prefix=$(jq -r .name now.json) now $* alias "${prefix}-${branch}.now.sh"
Or, if you need to test path aliases in your branch deployment:
branch="$TRAVIS_BRANCH" root="$(now-github-url --ref="$TRAVIS_COMMIT")" now $* alias $root cat rules.json | jq -rM ".rules[-1].dest = \"$root\"" > rules-preview.json # or hard-code the prefix if you don't have a "name" field prefix=$(jq -r .name now.json) now $* alias "${prefix}-${branch}.now.sh" -r rules-preview.json
-
Add a
deploy
section to your.travis.yml
that runs the script with yourNOW_TOKEN
environment variable:deploy: on: branch: staging provider: script script: script/deploy skip_cleanup: true
-
Create a new Now access token from your dashboard and add it as an environment variable to your Travis repo settings as
NOW_TOKEN
. -
If you don't have one already, create a GitHub API access token and add it to your environment as
GH_TOKEN
. -
💰
You can also use the utility in a local git repo if you're logged in with the
Now CLI and have GH_TOKEN
set to a GitHub access token in your environment.
cd ~/my/cool/repo
export NOW_TOKEN="..."
export GH_TOKEN="..."
npx now-github-url