Set up a git webhook for your pm2 app in 5 minutes. Start, stop, or check the status of the webhook whenever you want. Use SSL verification if you need it.
pm2 install jmensch1/pm2-git-hook
Add a command to the env.githook
section of your ecosystem.json
file, for example:
apps:[{
name: 'myApp',
...
env: {
githook: {
command: 'git pull && npm install && pm2 restart myApp'
}
}
}]
pm2 trigger pm2-git-hook start myApp
This will start an http
server running on port 9000
that listens for push notifications from github. It will run the command in the cwd
of your app whenever you push to master
.
If you want to make sure server is working, run curl http://localhost:9000/status
. Alternatively, run pm2 trigger pm2-git-hook status myApp
, which simply executes the curl
command.
Go to Settings -> Webhook on github. Create a hook with payload URL http://{hostName}:9000/
, content-type application-json
, and no secret. Click Disable SSL Verification
and then Add Webhook
. Github will then ping the webhook server, and activate the hook.
Property | Description | Required | Default |
---|---|---|---|
command | The command to run when github sends a push notification. | true | |
branch | The branch to listen for pushes on. | false | master |
port | The port to run the server on. | false | 9000 |
protocol | http or https . Use https if you want SSL verification on your hook. |
false | http |
sslCertPath | the full path of your SSL cert | if protocol is https |
|
sslKeyPath | the full path of your SSL private key | if protocol is https |
|
secret | the webhook secret on github | if you create a webhook with a secret |
pm2 trigger pm2-git-hook start myApp
-- start the webhook serverpm2 trigger pm2-git-hook status myApp
-- get a status report on the serverpm2 trigger pm2-git-hook stop myApp
-- stop the webhook server
If your command
contains commands that require github credentials (e.g., git pull
), the command will fail unless your server is set up to take the creds from the .git-credentials
file, or from some other credentials helper program. You can use git config credential.helper store
to create a .git-credentials
file. (See here and here.)
You can run webhook servers for as many apps as you want. Just make sure the ports are different for each app.