Skip to content

Commit

Permalink
Add option to manually set delay before healthcheck is performed
Browse files Browse the repository at this point in the history
  • Loading branch information
AkhileshNS committed Jul 22, 2020
1 parent 338817d commit fac457c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ jobs:
appdir: "tests/test-2"
buildpack: "https://github.com/heroku/heroku-buildpack-static.git"
healthcheck: "https://akhileshns-hd-test-2.herokuapp.com/"
delay: 5
- run: npm run test-action https://akhileshns-hd-test-2.herokuapp.com/

deploy-test-3:
Expand Down
50 changes: 41 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: akhileshns/heroku-deploy@v3.2.6 # This is the action
- uses: akhileshns/heroku-deploy@v3.3.6 # This is the action
with:
heroku_api_key: ${{secrets.HEROKU_API_KEY}}
heroku_app_name: "YOUR APP's NAME" #Must be unique in Heroku
Expand Down Expand Up @@ -73,6 +73,7 @@ The action comes with additional options that you can use to configure your proj
| appdir | false | Set if your app is located in a subdirectory | api, apis/python |
| healthcheck | false | A URL to which a healthcheck is performed (checks for 200 request) | https://demo-rest-api.herokuapp.com |
| checkstring | false | Value to check for when conducting healthcheck requests | ok |
| delay | false | Time (in seconds) to wait before performing healthcheck. Defaults to 0 seconds | 5 |
| procfile | false | Contents of the Procfile to save and deploy | web: npm start |
## Examples
Expand All @@ -96,7 +97,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: akhileshns/heroku-deploy@v3.2.6 # This is the action
- uses: akhileshns/heroku-deploy@v3.3.6 # This is the action
with:
heroku_api_key: ${{secrets.HEROKU_API_KEY}}
heroku_app_name: "YOUR APP's NAME" #Must be unique in Heroku
Expand Down Expand Up @@ -131,7 +132,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: akhileshns/heroku-deploy@v3.2.6 # This is the action
- uses: akhileshns/heroku-deploy@v3.3.6 # This is the action
with:
heroku_api_key: ${{secrets.HEROKU_API_KEY}}
heroku_app_name: "YOUR APP's NAME" #Must be unique in Heroku
Expand All @@ -158,7 +159,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: akhileshns/heroku-deploy@v3.2.6 # This is the action
- uses: akhileshns/heroku-deploy@v3.3.6 # This is the action
with:
heroku_api_key: ${{secrets.HEROKU_API_KEY}}
heroku_app_name: "YOUR APP's NAME" #Must be unique in Heroku
Expand Down Expand Up @@ -187,7 +188,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: akhileshns/heroku-deploy@v3.2.6 # This is the action
- uses: akhileshns/heroku-deploy@v3.3.6 # This is the action
with:
heroku_api_key: ${{secrets.HEROKU_API_KEY}}
heroku_app_name: "YOUR APP's NAME" #Must be unique in Heroku
Expand Down Expand Up @@ -216,7 +217,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: akhileshns/heroku-deploy@v3.2.6 # This is the action
- uses: akhileshns/heroku-deploy@v3.3.6 # This is the action
with:
heroku_api_key: ${{secrets.HEROKU_API_KEY}}
heroku_app_name: "YOUR APP's NAME" #Must be unique in Heroku
Expand Down Expand Up @@ -247,7 +248,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: akhileshns/heroku-deploy@v3.2.6 # This is the action
- uses: akhileshns/heroku-deploy@v3.3.6 # This is the action
with:
heroku_api_key: ${{secrets.HEROKU_API_KEY}}
heroku_app_name: "YOUR APP's NAME" #Must be unique in Heroku
Expand All @@ -258,6 +259,37 @@ jobs:

This will essentially check if the value returned by sending a GET request to the **healthcheck** url is equal to the **checkstring**

### Adding Delay

In some cases, a healthcheck ends up being performed before the application has been setup on Heroku. To counter this, you can manually set the **delay** option in the action to make the action wait a certain period of time (in seconds) before performing the healthcheck

_.github/workflows/main.yml_

```yaml
name: Deploy
on:
push:
branches:
- master # Changing the branch here would also work
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: akhileshns/[email protected] # This is the action
with:
heroku_api_key: ${{secrets.HEROKU_API_KEY}}
heroku_app_name: "YOUR APP's NAME" #Must be unique in Heroku
heroku_email: "YOUR EMAIL"
healthcheck: "https://[YOUR APP's NAME].herokuapp.com/health"
checkstring: "ok"
delay: 5
```

By default, the delay will be 0 if you choose to not set it

## Environment Variables

Heroku offers a means of passing sensitive information to your app (such as api keys etc) via something it calls **config vars** which you can find in the settings of your heroku app. But sometimes you might want to store sensitive information (api keys etc) in GitHub Secrets instead just to ensure platform independence. If you choose to this, you can then pass those secrets to your heroku app by using the "env" object of the action:-
Expand All @@ -277,7 +309,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: akhileshns/heroku-deploy@v3.2.6 # This is the action
- uses: akhileshns/heroku-deploy@v3.3.6 # This is the action
with:
heroku_api_key: ${{secrets.HEROKU_API_KEY}}
heroku_app_name: "YOUR APP's NAME" #Must be unique in Heroku
Expand Down Expand Up @@ -312,7 +344,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: akhileshns/heroku-deploy@v3.2.6 # This is the action
- uses: akhileshns/heroku-deploy@v3.3.6 # This is the action
with:
heroku_api_key: ${{secrets.HEROKU_API_KEY}}
heroku_app_name: "YOUR APP's NAME" #Must be unique in Heroku
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ inputs:
description: "Value to check for when conducting healthcheck request"
required: false
default: ""
delay:
description: "Time (in seconds) to wait before performing healthcheck"
required: false
default: "0"
procfile:
description: "Contents of the Procfile to save and deploy"
required: false
Expand Down
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const fs = require("fs");
const path = require("path");

// Support Functions
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

const createCatFile = ({ email, api_key }) => `cat >~/.netrc <<EOF
machine api.heroku.com
login ${email}
Expand Down Expand Up @@ -90,6 +92,7 @@ let heroku = {
appdir: core.getInput("appdir"),
healthcheck: core.getInput("healthcheck"),
checkstring: core.getInput("checkstring"),
delay: parseInt(core.getInput("delay")),
procfile: core.getInput("procfile"),
};

Expand Down Expand Up @@ -146,6 +149,10 @@ if (heroku.appdir) {
}

if (heroku.healthcheck) {
if (typeof heroku.delay === "number" && heroku.delay !== NaN) {
await sleep(heroku.delay * 1000);
}

try {
const res = await p(heroku.healthcheck);
if (heroku.checkstring && heroku.checkstring !== res.body.toString()) {
Expand Down

0 comments on commit fac457c

Please sign in to comment.