From 3f25f72bb506ea55c9cf01cf1861731fe4e0b5cc Mon Sep 17 00:00:00 2001 From: Evgeniy Frolov Date: Tue, 17 Dec 2024 11:54:20 +0300 Subject: [PATCH] feat(ci): reusable workflows (#1) * feat(ci): add notification Signed-off-by: Evgeniy Frolov * chore(ci): update notification workflow Signed-off-by: Evgeniy Frolov * chore(ci): add cleanup workflow update notification Signed-off-by: Evgeniy Frolov --------- Signed-off-by: Evgeniy Frolov --- .github/workflows/cleanup.yml | 53 ++++++++++++++++++++++ .github/workflows/notification.yml | 73 ++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 .github/workflows/cleanup.yml create mode 100644 .github/workflows/notification.yml diff --git a/.github/workflows/cleanup.yml b/.github/workflows/cleanup.yml new file mode 100644 index 0000000..211a502 --- /dev/null +++ b/.github/workflows/cleanup.yml @@ -0,0 +1,53 @@ +name: cleanup + +on: + workflow_call: + inputs: + werfDir: + required: false + type: string + withoutKube: + required: false + type: string + werfRepo: + required: false + type: string + secrets: + cleanupToken: + description: "cleanup token" + required: true + kubeconfig: + required: false + githubToken: + required: true + + defaults: + run: + shell: bash + +jobs: + cleanup: + name: cleanup + runs-on: ubuntu-latest-16-cores + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Install werf + uses: werf/actions/install@v2 + with: + channel: alpha + + - name: Cleanup + run: | + . $(werf ci-env github --as-file) + werf cleanup + env: + WERF_DIR: ${{ inputs.werfDir }} + WERF_WITHOUT_KUBE: ${{ inputs.withoutKube }} + GITHUB_TOKEN: ${{ secrets.githubToken }} + WERF_REPO_GITHUB_TOKEN: ${{ secrets.cleanupToken }} + WERF_KUBE_CONFIG_BASE64: ${{ secrets.kubeconfig }} + WERF_REPO: ${{ inputs.werfRepo }} diff --git a/.github/workflows/notification.yml b/.github/workflows/notification.yml new file mode 100644 index 0000000..515b490 --- /dev/null +++ b/.github/workflows/notification.yml @@ -0,0 +1,73 @@ +name: notification + +on: + workflow_call: + secrets: + webhook: + description: "Loop webhook URL" + required: true + notificationChannel: + description: "Loop notification channel" + required: true + loopNotificationGroup: + description: "Loop notification group" + required: false + +defaults: + run: + shell: bash + +jobs: + notify: + runs-on: ubuntu-22.04 + timeout-minutes: 10 + steps: + - name: Run workflow status conclusion manager + uses: technote-space/workflow-conclusion-action@v3 + + - if: env.WORKFLOW_CONCLUSION == 'failure' + name: get failed jobs + id: failed_jobs + run: | + echo 'failed<> $GITHUB_OUTPUT + echo "$(curl ${{ github.api_url }}/repos/${{ github.repository }}/actions/runs/${{ github.run_id }}/jobs | jq -r '.jobs[]| select (.conclusion == "failure") | .html_url')" >> $GITHUB_OUTPUT + echo 'EOF' >> $GITHUB_OUTPUT + - if: env.WORKFLOW_CONCLUSION == 'failure' && (startsWith(github.ref, 'refs/pull/') || github.event_name == 'workflow_dispatch') + name: get user + id: get_user + run: | + echo username=$(curl ${{ github.api_url }}/users/${{ github.triggering_actor }} | jq -r '.name' | tr '[:upper:]' '[:lower:]' | tr " " . ) >> $GITHUB_OUTPUT + + - if: "(env.WORKFLOW_CONCLUSION == 'failure' && ! startsWith(github.ref, 'refs/pull/') && github.event_name != 'workflow_dispatch' )" + name: Failure notify + uses: mattermost/action-mattermost-notify@master + with: + MATTERMOST_WEBHOOK_URL: ${{ secrets.webhook }} + MATTERMOST_CHANNEL: ${{ secrets.notificationChannel }} + TEXT: | + ${{ secrets.loopNotificationGroup }} Прод упал :pepe_monkaw: + [${{ github.workflow }}](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) ${{ github.ref_name }} :pepe_deadge: + Упавшие джобы: + ${{ steps.failed_jobs.outputs.failed }} + + - if: "env.WORKFLOW_CONCLUSION == 'success' && ! startsWith(github.ref, 'refs/pull/') && ! github.event_name == 'workflow_dispatch'" + name: Success notify + uses: mattermost/action-mattermost-notify@master + with: + MATTERMOST_WEBHOOK_URL: ${{ secrets.webhook }} + MATTERMOST_CHANNEL: ${{ secrets.notificationChannel }} + TEXT: | + Workflow успешно завершился :bender: + [${{ github.workflow }}](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) ${{ github.ref_name }} :success: + + - if: env.WORKFLOW_CONCLUSION == 'failure' && (startsWith(github.ref, 'refs/pull/') || github.event_name == 'workflow_dispatch') + name: Failure notify for PRs + uses: mattermost/action-mattermost-notify@master + with: + MATTERMOST_WEBHOOK_URL: ${{ secrets.webhook }} + MATTERMOST_CHANNEL: ${{ secrets.notificationChannel }} + TEXT: | + @${{ steps.get_user.outputs.username }} Проблема в ветке :press_f: + [${{ github.workflow }}](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) ${{ github.ref_name }} :pepe_deadge: + Упавшие джобы: + ${{ steps.failed_jobs.outputs.failed }} \ No newline at end of file