name: Publish Job Success Metric to CloudWatch

on:
  workflow_run:
    workflows: "*"
    types:
      - completed
    branches:
      - master

permissions:
  id-token: write
  contents: read

jobs:
  publish-job-success-to-cloudwatch:
    if: ${{ github.repository == 'deepjavalibrary/djl' && github.event.workflow_run.event == 'schedule' }}
    runs-on: ubuntu-latest
    steps:
      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v4
        with:
          role-to-assume: arn:aws:iam::185921645874:role/djl-github-cloudwatch-ci-metrics
          aws-region: us-west-2
      - name: Publish Job Success Metric
        env:
          WORKFLOW_NAME: ${{ github.event.workflow_run.display_title }}
          REPO_NAME: ${{ github.event.workflow_run.repository.name }}
          CONCLUSION: ${{ github.event.workflow_run.conclusion }}
        run: |
          workflow_name=$(echo "$WORKFLOW_NAME" | tr -d ' ')
          metric_name="${REPO_NAME}-${workflow_name}-Failure"
          failedBuild=$([ "$CONCLUSION" == "success" ]; echo $?)
          aws cloudwatch put-metric-data --namespace GithubCI \
            --metric-name "$metric_name" \
            --value $failedBuild \
            --unit Count
