Skip to content

Instantly share code, notes, and snippets.

@5kr1p7
Last active August 19, 2022 23:07
Show Gist options
  • Save 5kr1p7/e5a034039f60042584ea0ad5188bab16 to your computer and use it in GitHub Desktop.
Save 5kr1p7/e5a034039f60042584ea0ad5188bab16 to your computer and use it in GitHub Desktop.
GitHub Webhook

Webhook

Gihub Webhook URL: http://<URL>:9000/hooks/dev-deploy

gh_webhook.sh:

#!/usr/local/bin/bash

set -euo pipefail

CONFIG="/home/user/webhook-freebsd-amd64/hooks.yaml"
WEBHOOK_BIN="/home/user/webhook-freebsd-amd64/webhook"

if pgrep -x "webhook" >/dev/null
then
    echo "Webhook daemon is running"
else
    echo "Starting webhook daemon..."
    daemon -r -f -t "GitHub webhook" ${WEBHOOK_BIN} -hooks ${CONFIG}
fi

dev-deploy.sh:

#!/usr/local/bin/bash

set -euo pipefail

function git_clone() {

    mkdir -p ${LOCAL_GIT_DIR}
    cd ${LOCAL_GIT_DIR}
    #/usr/bin/env git clone -q ${REPO_URL} . ||  rm -rf ${LOCAL_GIT_DIR}
    /usr/bin/env git clone -q ${REPO_URL} .
}

function git_pull() {

    cd ${LOCAL_GIT_DIR}
    /usr/bin/env git checkout -q master
    /usr/bin/env git fetch -p -q --all
    /usr/bin/env git reset -q --hard origin/master
}

function main() {
    while getopts ":D:U:" opt; do
        case "${opt}" in
            D)    LOCAL_GIT_DIR="${OPTARG}";         ;;
            U)    REPO_URL="${OPTARG}";              ;;
            h|*)  echo "use -D, -U, option, not null"; ;;
        esac
    done
    shift "$((OPTIND-1))"

    if [ ! -d "${LOCAL_GIT_DIR}" ]; then
        git_clone
        git_pull
    else
        git_pull
    fi
}

main "${@:-""}" echo "${*:-""} success"

hooks.yaml:

- id: dev-deploy
  execute-command: "dev-deploy.sh"
  command-working-directory: "/home/user/"
  response-message: Executing redeploy script
  include-command-output-in-response: true
  trigger-rule-mismatch-http-response-code: 400

  pass-arguments-to-command:
    - source: string
      name: -D
    - source: string
      name: "/home/user/site.com"
    - source: string
      name: -U
    - source: payload
      name: repository.clone_url

  trigger-rule:
    and:
      - match:
          type: payload-hmac-sha1
          secret: "S3cr3t"
          parameter:
            source: header
            name: X-Hub-Signature
      - match:
          type: value
          value: refs/heads/master
          parameter:
            source: payload
            name: ref
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment