Skip to content

Commit e5ea5fc

Browse files
committed
Implement a Github Action Workflow Log Processor Workflow
1 parent 96173b1 commit e5ea5fc

File tree

2 files changed

+48
-42
lines changed

2 files changed

+48
-42
lines changed

.github/workflows/fetch_and_log.yaml

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
fetch-workflows:
1010
runs-on: ubuntu-latest
1111
outputs:
12-
workflow_ids: ${{ steps.fetch-ids.outputs.workflow_ids }}
12+
workflow_ids: ${{ steps.fetch-unprocessed-run-ids.outputs.workflow_ids }}
1313

1414
steps:
1515
- name: Get Last Processed Run ID
@@ -24,54 +24,60 @@ jobs:
2424
- name: Fetch Unprocessed Workflow Runs
2525
id: fetch-unprocessed-run-ids
2626
run: |
27-
HOURS=1
28-
# WORKFLOW_IDS=$(gh run list --repo $GITHUB_REPOSITORY --limit 100 --json databaseId,status,createdAt --jq '[.[] | select(.createdAt > (now - (1 * 3600))) | .databaseId] | @json')
29-
# LAST_RUN_ID=$(echo $WORKFLOW_IDS | jq '.[-1]')
27+
# This script ensures that only completed GitHub workflow runs are processed while avoiding workflows that are still in progress.
28+
# Steps:
29+
# 1. Fetch the latest workflow runs from GitHub, sorted by databaseId.
30+
# 2. Identify the oldest incomplete workflow (first one still running).
31+
# 3. Return an array of workflow IDs up to (but not including) the oldest incomplete workflow ID.
32+
WORKFLOW_DATA=$(gh run list --repo $GITHUB_REPOSITORY --limit 100 --json databaseId,createdAt,displayTitle,status --jq "[.[] | select(.databaseId > $LAST_PROCESSED_RUN_ID and .displayTitle != \"Fetch and Log Workflow Runs\")] | sort_by(.databaseId)")
3033
31-
WORKFLOW_IDS=$(gh run list --repo $GITHUB_REPOSITORY --limit 100 --json databaseId,createdAt --jq "[.[] | select(.databaseId > $LAST_PROCESSED_RUN_ID) | .databaseId] | @json")
32-
LAST_RUN_ID=$(echo $WORKFLOW_IDS | jq '.[-1]') # Store the highest run ID
33-
34-
echo "LAST_RUN_ID=$LAST_RUN_ID" >> $GITHUB_ENV
34+
OLDEST_INCOMPLETE_ID=$(echo "$WORKFLOW_DATA" | jq '[.[] | select(.status != "completed") | .databaseId] | min // empty')
35+
36+
if [[ -n "$OLDEST_INCOMPLETE_ID" ]]; then
37+
WORKFLOW_IDS=$(echo "$WORKFLOW_DATA" | jq "[.[] | select(.status == \"completed\" and .databaseId < $OLDEST_INCOMPLETE_ID)] | [.[].databaseId] | @json")
38+
else
39+
WORKFLOW_IDS=$(echo "$WORKFLOW_DATA" | jq "[.[] | select(.status == \"completed\")] | [.[].databaseId] | @json")
40+
fi
41+
42+
echo "Workflow runs to process: $WORKFLOW_IDS"
3543
echo "workflow_ids=$WORKFLOW_IDS" >> $GITHUB_OUTPUT
3644
env:
3745
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
46+
47+
process-workflows:
48+
needs: fetch-workflows
49+
runs-on: ubuntu-latest
50+
if: ${{ needs.fetch-workflows.outputs.workflow_ids != '[]' && needs.fetch-workflows.outputs.workflow_ids != '' }}
51+
strategy:
52+
matrix:
53+
run_id: ${{ fromJson(needs.fetch-workflows.outputs.workflow_ids || '[]') }}
54+
max-parallel: 1
55+
steps:
56+
- name: Workflow Run to Process
57+
run:
58+
echo ${{ matrix.run_id }}
59+
60+
# - name: Send Workflow Logs to Splunk
61+
# uses: ykoer/github-workflow-splunk-logger@dev
62+
# with:
63+
# github_token: ${{ secrets.GITHUB_TOKEN }}
64+
# splunk_url: ${{ vars.HEC_URL }}
65+
# splunk_token: ${{ secrets.HEC_TOKEN }}
66+
# run_id: ${{ matrix.run_id }}
3867

39-
- name: Save Last Processed Run ID
68+
- name: Save Processed Run ID
4069
if: success()
4170
run: |
42-
gh secret set LAST_PROCESSED_ID --repo $GITHUB_REPOSITORY --body "$LAST_RUN_ID"
71+
gh variable set LAST_PROCESSED_RUN_ID --repo $GITHUB_REPOSITORY --body "${{ matrix.run_id }}"
4372
env:
4473
GH_TOKEN: ${{ secrets.GH_PAT }}
45-
46-
process-logs:
74+
75+
76+
# Add a fallback job that runs when there are no workflow Run's to process
77+
process-workflows-empty:
4778
needs: fetch-workflows
4879
runs-on: ubuntu-latest
49-
# strategy:
50-
# matrix:
51-
# run_id: ${{ fromJson(needs.fetch-workflows.outputs.workflow_ids || '[]') }}
80+
if: ${{ needs.fetch-workflows.outputs.workflow_ids == '[]' || needs.fetch-workflows.outputs.workflow_ids == '' }}
5281
steps:
53-
# - run:
54-
# echo ${{ matrix.run_id }}
55-
- run: |
56-
echo "processing logs...."
57-
echo "-----------------"
58-
echo ${{ needs.fetch-workflows.outputs.workflow_ids }}
59-
echo "-----------------"
60-
echo ${{ fromJson(needs.fetch-workflows.outputs.workflow_ids || '[]') }}
61-
echo "-----------------"
62-
63-
# process-logs:
64-
# needs: fetch-workflows
65-
# runs-on: ubuntu-latest
66-
# strategy:
67-
# matrix:
68-
# run_id: ${{ fromJson(needs.fetch-workflows.outputs.workflow_ids) }}
69-
70-
# steps:
71-
# - name: Send Workflow Logs to Splunk
72-
# uses: ykoer/github-workflow-splunk-logger@dev
73-
# with:
74-
# github_token: ${{ secrets.GITHUB_TOKEN }}
75-
# splunk_url: ${{ vars.HEC_URL }}
76-
# splunk_token: ${{ secrets.HEC_TOKEN }}
77-
# run_id: ${{ matrix.run_id }}
82+
- name: No workflows to process
83+
run: echo "No new workflow runs to process"

.github/workflows/log_to_splunk.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
uses: ykoer/github-workflow-splunk-logger@dev
1414
with:
1515
github_token: ${{ secrets.GITHUB_TOKEN }}
16-
splunk_url: ${{ vars.HEC_URL }}
16+
splunk_url: ${{ vars.HEC_URL_TEST }}
1717
splunk_token: ${{ secrets.HEC_TOKEN }}
18-
run_id: 13309230902
18+
run_id: 13593560361
1919

0 commit comments

Comments
 (0)