# GitHub Webhook

## 목표

github에 푸시가 되면 webhook을 사용해서 특정 서버에 통보한다.

Settings > webHooks >> Add Webhook

![alt text](https://3379277180-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MaoWGMVdC3ztoOAzHPq%2Fuploads%2Fgit-blob-a8bd9a05bd06c510c23e65e804a3817af1bee192%2Fimage.png?alt=media)

![alt text](https://3379277180-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MaoWGMVdC3ztoOAzHPq%2Fuploads%2Fgit-blob-5b3f7c04d4cf24b1cbd98bb247b3bbe812f980cb%2Fimage-1.png?alt=media)

`let me select individual events`를 선택하면 옵션이 나오는데 선택하면된다.

나는 기본적으로 `just the push event`만 선택햇다.

## create a webhook

![alt text](https://3379277180-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MaoWGMVdC3ztoOAzHPq%2Fuploads%2Fgit-blob-ac4ac3264adc5ff27822bf524b73b6f3538ef7e5%2Fimage-2.png?alt=media)

content type은 그림과 다르게 `application/json`으로 선택했다.

이제 푸시가 들어오면 웹훅이 호출된다.

![alt text](https://3379277180-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MaoWGMVdC3ztoOAzHPq%2Fuploads%2Fgit-blob-bad14b8ba18a4654d1fd981e45faa681a7c59f5b%2Fimage-3.png?alt=media)

### payload

페이로드는 25MB로 제한됩니다. 이벤트가 더 큰 페이로드를 생성하는 경우, GitHub는 해당 웹훅 이벤트에 대한 페이로드를 제공하지 않습니다.

### header

* X-GitHub-Hook-ID: The unique identifier of the webhook.
* X-GitHub-Event: The name of the event that triggered the delivery.
* X-GitHub-Delivery: A globally unique identifier (GUID) to identify the delivery.
* X-Hub-Signature: This header is sent if the webhook is configured with a secret. This is the HMAC hex digest of the request body, and is generated using the SHA-1 hash function and the secret as the HMAC key. X-Hub-Signature is provided for compatibility with existing integrations. We recommend that you use the more secure X-Hub-Signature-256 instead.
* X-Hub-Signature-256: This header is sent if the webhook is configured with a secret. This is the HMAC hex digest of the request body, and is generated using the SHA-256 hash function and the secret as the HMAC key. For more information, see "Validating webhook deliveries."
* User-Agent: This header will always have the prefix GitHub-Hookshot/.
* X-GitHub-Hook-Installation-Target-Type: The type of resource where the webhook was created.
* X-GitHub-Hook-Installation-Target-ID: The unique identifier of the resource where the webhook was created.

## webapi서버 만들기

간단히 webapi서버를 만들고 디비를 만들어서 리퀘스트를 받아보면 될거같다.

## test

`git push`를 해보자.

![alt text](https://3379277180-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MaoWGMVdC3ztoOAzHPq%2Fuploads%2Fgit-blob-955ca717058542379ecf2fdbd53bfa86f21b198a%2Fimage-4.png?alt=media)

실서버에서 리퀘스트가 들어온것을 알수있다.

payload를 확인하여 디비에 저장하면 끝

![alt text](https://3379277180-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MaoWGMVdC3ztoOAzHPq%2Fuploads%2Fgit-blob-a311ca35c0589f26d1818db9520847330e03b7e0%2Fimage-5.png?alt=media)

여기서 실행된 결과를 볼수 있고 실제 payload를 확인할수 있다.

![alt text](https://3379277180-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MaoWGMVdC3ztoOAzHPq%2Fuploads%2Fgit-blob-ffa00ea63abd62b621bbe3e0cd2a77f5f37db993%2Fimage-6.png?alt=media)

실패하면 알림마크가 보인다.

## 추가

참고로 webhook를 바로 쓰지 않고 action을 써서 webhook을 발생시킬수 있다.

[https://github.com/marketplace/actions/workflow-webhook-action](https://github.com/marketplace/actions/workflow-webhook-action)

이걸 yaml에 사용하면 된다.
