diff options
author | Roman / Nea <roman.graef@gmail.com> | 2021-12-29 16:14:58 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-29 10:14:58 -0500 |
commit | 89164ed02a503ad1578b13625a8bf35cae95f81b (patch) | |
tree | 1ed59eaf522173edcee2d82fff1d74844b79b5db | |
parent | 98436ad0cf75c9a4bacbfb35a63948cc61c543f9 (diff) | |
download | NotEnoughUpdates-89164ed02a503ad1578b13625a8bf35cae95f81b.tar.gz NotEnoughUpdates-89164ed02a503ad1578b13625a8bf35cae95f81b.tar.bz2 NotEnoughUpdates-89164ed02a503ad1578b13625a8bf35cae95f81b.zip |
github webhook action mashup 400 (#46)
-rw-r--r-- | .github/workflows/build.yml | 28 | ||||
-rwxr-xr-x | .github/workflows/send_webhook_update.sh | 86 |
2 files changed, 112 insertions, 2 deletions
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 60b1719f..f7eb2b2e 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -18,11 +18,21 @@ on: workflow_dispatch: jobs: build: - + env: + GIT_URL: ${{ github.server_url }}/${{ github.repository }}/tree/${{ github.sha }} + REF_NAME: ${{ github.ref_name }} + WEBHOOK_URL: ${{ secrets.WEBHOOK_URL }} runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 + - name: Send discord notification + id: sendmsg + if: ${{ env.WEBHOOK_URL }} + run: | + ./.github/workflows/send_webhook_update.sh + env: + STATUS: WORKING - name: Set up JDK 8 uses: actions/setup-java@v2 with: @@ -33,4 +43,18 @@ jobs: run: chmod +x ./gradlew && ./gradlew setupCIWorkspace build --no-daemon - uses: actions/upload-artifact@v2 with: - path: build/libs/*-dep.jar + path: build/libs/*-deps.jar + - name: Update discord notification + if: ${{ env.WEBHOOK_URL && success() }} + run: | + ./.github/workflows/send_webhook_update.sh + env: + STATUS: SUCCESS + MESSAGE_ID: ${{ steps.sendmsg.outputs.MESSAGE_ID }} + - name: Update discord notification + if: ${{ env.WEBHOOK_URL && failure() }} + run: | + ./.github/workflows/send_webhook_update.sh + env: + STATUS: FAILURE + MESSAGE_ID: ${{ steps.sendmsg.outputs.MESSAGE_ID }} diff --git a/.github/workflows/send_webhook_update.sh b/.github/workflows/send_webhook_update.sh new file mode 100755 index 00000000..1fc39adf --- /dev/null +++ b/.github/workflows/send_webhook_update.sh @@ -0,0 +1,86 @@ +#!/bin/bash + +COLOR_SUCCESS=8040199 +COLOR_WORKING=7472302 +COLOR_ERROR=14960972 +case "$STATUS" in + WORKING) + color="$COLOR_WORKING" + status_message="Build started." + ;; + FAILURE) + color="$COLOR_ERROR" + status_message="Build failed." + ;; + SUCCESS) + color="$COLOR_SUCCESS" + status_message="Build succeeded." + to_upload=$(echo build/libs/*-dep.jar) + upload_name=NotEnoughUpdates-beta-dep.jar + ;; +esac + +author_name=$(git log -1 --pretty=format:'%an') +commit_hash=$(git log -1 --pretty=format:'%h') +commit_subject=$(git log -1 --pretty=format:'%s') +commit_body=$(git log -1 --pretty=format:'%b') +commit_date=$(git log -1 --pretty=format:'%ct') + +author_avatar="https://github.com/$author_name.png" + +#language=json +read -r -d '' structure <<-"EOF" +{ + "content": $status, + "username": $username, + "avatar_url": $avatar_url, + "embeds": [ + { + "color": $color, + "url": $url, + "title": $subject, + "description": $body, + "footer": { + "text": $ref + } + } + ], + "allowed_mentions": { + "parse": [] + } +} +EOF +json=$(jq -n \ + --arg body "$commit_body" \ + --arg status "$status_message" \ + --arg subject "$commit_subject" \ + --arg username "$author_name" \ + --arg avatar_url "$author_avatar" \ + --argjson color "$color" \ + --arg url "$GIT_URL" \ + --arg ref "$REF_NAME" \ + "$structure") + +function make_request() { + if [ "$to_upload" != "" ]; then + upload_arg="-F" + fi + curl -X $1 -H "Content-Type: multipart/form-data" -F "payload_json=$json" "$upload_arg" "$upload_name=@$to_upload" "$WEBHOOK_URL$2?wait=true" +} + +if [ "$MESSAGE_ID" != "" ]; then + discord_output=$(make_request PATCH "/messages/$MESSAGE_ID") + RESULT=$? +else + discord_output=$(make_request POST) + RESULT=$? +fi + +if [ $RESULT != 0 ]; then + echo "$discord_output" + exit 1 +fi +echo "Message sent to discord." +echo "$discord_output" | jq . +id_string=$(echo "$discord_output" | jq .id) +echo "::set-output name=MESSAGE_ID=${id_string//\"/}" |