From e19383d5f0d6493830e57718109c3718d21fac71 Mon Sep 17 00:00:00 2001 From: Jason Wen Date: Sat, 9 Dec 2023 11:58:25 +0000 Subject: [PATCH] CI: Post notifications to Discord --- .gitlab-ci.yml | 39 +++++++++++++++++++ .../discord_template_notify_dev_private.json | 13 +++++++ .../discord_template_notify_dev_public.json | 9 +++++ 3 files changed, 61 insertions(+) create mode 100644 release/ci/discord_template_notify_dev_private.json create mode 100644 release/ci/discord_template_notify_dev_public.json diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 5a6bebb847..7309937f7a 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -8,11 +8,13 @@ variables: DEV_C3_SOURCE_BRANCH: "master-dev-c3" GIT_CONFIG_USER_EMAIL: "gitlab@pipeline.com" GIT_CONFIG_USER_NAME: "Gitlab Pipeline" + PUBLIC_REPO_URL: "https://github.com/sunnyhaibin/sunnypilot" stages: - build - commit_changes - publish + - notify default: retry: 2 @@ -176,3 +178,40 @@ publish to github: - if: $NEW_BRANCH when: manual - when: never + +.notify_discord: ¬ify_discord + stage: notify + needs: ["build"] + variables: + DISCORD_HOOK: "${DISCORD_MANUAL_BUILD_WEBHOOK_URL}" # Default hook if not overriden by children + before_script: + - 'apk add curl jq envsubst' + script: + - echo using [${TEMPLATE}] + - cat release/ci/${TEMPLATE} | envsubst | tee payload.json + - 'curl -X POST -H "Content-Type: application/json" -d "$(cat payload.json)" ${DISCORD_HOOK} | jq .' + rules: + - if: $NEW_BRANCH + when: on_success + - when: never + allow_failure: true + +notify pending action: + extends: ".notify_discord" + variables: + TEMPLATE: "discord_template_notify_dev_private.json" + before_script: + - !reference [".notify_discord", "before_script"] + - export AVATAR_URL=$(curl -s -X GET "https://gitlab.com/api/v4/avatar?email=${GITLAB_USER_EMAIL}" | jq -r '.avatar_url') + +notify new dev build: + extends: ".notify_discord" + stage: notify + needs: ["publish to github"] # This notify shall only happen after a publish to github public + variables: + TEMPLATE: "discord_template_notify_dev_public.json" + rules: + - if: $NEW_BRANCH == "dev-c3" + variables: + DISCORD_HOOK: "${DISCORD_NEW_BUILD_WEBHOOK_URL}" # Overriding hook because we know we are dev-c3 + - !reference [".notify_discord", "rules"] diff --git a/release/ci/discord_template_notify_dev_private.json b/release/ci/discord_template_notify_dev_private.json new file mode 100644 index 0000000000..5da1e4d405 --- /dev/null +++ b/release/ci/discord_template_notify_dev_private.json @@ -0,0 +1,13 @@ +{ + "embeds": [ + { + "title": "❗️ Action Required for `${CI_COMMIT_REF_NAME}` ❗️", + "description": "[${CI_PROJECT_NAME}](${CI_PROJECT_URL}): Pipeline [#${CI_PIPELINE_ID}](${CI_PROJECT_URL}/-/pipelines/${CI_PIPELINE_ID}) of branch [${CI_COMMIT_REF_NAME}](${CI_PROJECT_URL}/-/commits/${CI_COMMIT_REF_NAME}) by ${GITLAB_USER_NAME} (${GITLAB_USER_LOGIN}) is ready to publish manually as [${NEW_BRANCH}](${PUBLIC_REPO_URL}/tree/${NEW_BRANCH})", + "color": 16763904, + "author": { + "name": "${GITLAB_USER_LOGIN}", + "icon_url": "${AVATAR_URL}" + } + } + ] +} diff --git a/release/ci/discord_template_notify_dev_public.json b/release/ci/discord_template_notify_dev_public.json new file mode 100644 index 0000000000..d1a7b37f96 --- /dev/null +++ b/release/ci/discord_template_notify_dev_public.json @@ -0,0 +1,9 @@ +{ + "embeds": [ + { + "title": "🎉 sunnypilot `${NEW_BRANCH}` New Update 🎉", + "description": "[sunnypilot](${PUBLIC_REPO_URL}): Build #${CI_PIPELINE_IID} of branch [${NEW_BRANCH}](${PUBLIC_REPO_URL}/tree/${NEW_BRANCH}) has been published.\n\nDrive safe! 🚗💨", + "color": 4321431 + } + ] +}