Deploiement automatique à l'aide de webhooks

blog

Deploiement automatique à l'aide de webhooks

Installation

sudo apt install webhook

Mise en place

Création d'un endpoint:

rules.json
[
  {
    "id": name, 
    "execute-command": deploy.sh, 
    "command-working-directory": working directory,
    "pass-arguments-to-command": [
      {
        "source": "payload",
        "name": "head_commit.message"
      },
      {
        "source": "payload",
        "name": "pusher.name"
      },
      {
        "source": "payload",
        "name": "head_commit.id"
      }
    ],
    "trigger-rule": {
      "and": [
        {
          "match": {
            "type": "payload-hash-sha1",
            "secret": github secret,
            "parameter": {
              "source": "header",
              "name": "X-Hub-Signature"
            }
          }
        },
        {
          "match": {
            "type": "value",
            "value": "refs/heads/main",
            "parameter": {
              "source": "payload",
              "name": "ref"
            }
          }
        }
      ]
    }
  }
]

Création du script d'automatisation:

deploy.sh
#!/bin/sh
 
git pull -f origin branch
 
npm install
npm run build
 
pm2 restart appname

Lancement du serveur webhook

webhook -hooks hooks.json -port port
nging/site-enabled/server_name
server {
 
        listen 443 ssl http2;
        listen [::]:443 ssl http2;
 
        server_name server_name 
 
        location /hooks/ {
                proxy_pass http://localhost:port/hooks/;
        }
}