Skip to content

Commit

Permalink
filter by branch and commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gbanu committed Feb 13, 2025
1 parent 2f6db65 commit e4387da
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 48 deletions.
37 changes: 13 additions & 24 deletions .github/workflows/staging-deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ on:
commit_sha:
description: 'Commit SHA to deploy'
required: true
environment_name:
description: 'Environment to deploy to'
required: true

concurrency: staging
concurrency: ${{ github.event.inputs.environment_name }}

env:
build_workflow_name: build.yml
Expand All @@ -23,46 +26,32 @@ jobs:
steps:
- name: Print inputs
run: |
echo "Branch: ${{ github.event.inputs.branch_name }}"
echo "Branch: ${{ github.event.inputs.environment_name }}"
echo "Commit SHA: ${{ github.event.inputs.commit_sha }}"
- name: Get workflow runs for commit
- name: Get workflow runs for the specific branch and commit
id: get_workflow_run
uses: octokit/[email protected]
with:
# Remove the event filter to get all events
route: GET /repos/${{ github.repository }}/actions/workflows/${{ env.build_workflow_name }}/runs?branch=${{ github.event.inputs.branch_name }}
route: GET /repos/${{ github.repository }}/actions/workflows/${{ env.build_workflow_name }}/runs?branch=${{ github.event.inputs.branch_name }}&head_sha=${{ github.event.inputs.commit_sha }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Find build for specific commit
id: set_build_workflow_id
run: |
COMMIT_SHA="${{ github.event.inputs.commit_sha }}"
WORKFLOW_DATA='${{ steps.get_workflow_run.outputs.data }}'
# Find the workflow run for the specific commit across all supported events
WORKFLOW_ID=$(echo "$WORKFLOW_DATA" | jq -r --arg COMMIT "$COMMIT_SHA" '
.workflow_runs[]
| select(
.head_sha == $COMMIT and (
# List all events that can trigger builds
.event == "push" or
.event == "pull_request" or
.event == "release" or
.event == "workflow_dispatch" or
.event == "schedule"
)
)
| .id
' | head -n1) # Get the first matching workflow
WORKFLOW_ID=$(echo "$WORKFLOW_DATA" | jq -r '
.workflow_runs[0].id // empty
')
if [ -z "$WORKFLOW_ID" ]; then
echo "::error::No build found for commit $COMMIT_SHA on branch ${{ github.event.inputs.branch_name }}"
echo "::error::No build found for commit ${{ github.event.inputs.commit_sha }} on branch ${{ github.event.inputs.branch_name }}"
exit 1
fi
echo "Found build workflow ID: $WORKFLOW_ID for commit $COMMIT_SHA"
echo "Found build workflow ID: $WORKFLOW_ID for commit ${{ github.event.inputs.commit_sha }} on branch ${{ github.event.inputs.branch_name }}"
echo "workflow_id=$WORKFLOW_ID" >> $GITHUB_OUTPUT
- name: Check for war artifact
Expand All @@ -89,7 +78,7 @@ jobs:
needs: check-build-status
runs-on: [self-hosted, ase-large-ubuntu]
environment:
name: artemis-staging-localci.artemis.cit.tum.de
name: ${{ github.event.inputs.environment_name }}
url: ${{ vars.DEPLOYMENT_URL }}
env:
DEPLOYMENT_HOSTS_PRIMARY: ${{ vars.DEPLOYMENT_HOSTS_PRIMARY }}
Expand Down
37 changes: 13 additions & 24 deletions .github/workflows/testserver-deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ on:
commit_sha:
description: 'Commit SHA to deploy'
required: true
environment_name:
description: 'Environment to deploy to'
required: true

concurrency: staging
concurrency: ${{ github.event.inputs.environment_name }}

env:
build_workflow_name: build.yml
Expand All @@ -23,46 +26,32 @@ jobs:
steps:
- name: Print inputs
run: |
echo "Branch: ${{ github.event.inputs.branch_name }}"
echo "Branch: ${{ github.event.inputs.environment_name }}"
echo "Commit SHA: ${{ github.event.inputs.commit_sha }}"
- name: Get workflow runs for commit
- name: Get workflow runs for the specific branch and commit
id: get_workflow_run
uses: octokit/[email protected]
with:
# Remove the event filter to get all events
route: GET /repos/${{ github.repository }}/actions/workflows/${{ env.build_workflow_name }}/runs?branch=${{ github.event.inputs.branch_name }}
route: GET /repos/${{ github.repository }}/actions/workflows/${{ env.build_workflow_name }}/runs?branch=${{ github.event.inputs.branch_name }}&head_sha=${{ github.event.inputs.commit_sha }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Find build for specific commit
id: set_build_workflow_id
run: |
COMMIT_SHA="${{ github.event.inputs.commit_sha }}"
WORKFLOW_DATA='${{ steps.get_workflow_run.outputs.data }}'
# Find the workflow run for the specific commit across all supported events
WORKFLOW_ID=$(echo "$WORKFLOW_DATA" | jq -r --arg COMMIT "$COMMIT_SHA" '
.workflow_runs[]
| select(
.head_sha == $COMMIT and (
# List all events that can trigger builds
.event == "push" or
.event == "pull_request" or
.event == "release" or
.event == "workflow_dispatch" or
.event == "schedule"
)
)
| .id
' | head -n1) # Get the first matching workflow
WORKFLOW_ID=$(echo "$WORKFLOW_DATA" | jq -r '
.workflow_runs[0].id // empty
')
if [ -z "$WORKFLOW_ID" ]; then
echo "::error::No build found for commit $COMMIT_SHA on branch ${{ github.event.inputs.branch_name }}"
echo "::error::No build found for commit ${{ github.event.inputs.commit_sha }} on branch ${{ github.event.inputs.branch_name }}"
exit 1
fi
echo "Found build workflow ID: $WORKFLOW_ID for commit $COMMIT_SHA"
echo "Found build workflow ID: $WORKFLOW_ID for commit ${{ github.event.inputs.commit_sha }} on branch ${{ github.event.inputs.branch_name }}"
echo "workflow_id=$WORKFLOW_ID" >> $GITHUB_OUTPUT
- name: Check for war artifact
Expand All @@ -89,7 +78,7 @@ jobs:
needs: check-build-status
runs-on: [self-hosted, ase-large-ubuntu]
environment:
name: artemis-staging-localci.artemis.cit.tum.de
name: ${{ github.event.inputs.environment_name }}
url: ${{ vars.DEPLOYMENT_URL }}
env:
DEPLOYMENT_HOSTS_PRIMARY: ${{ vars.DEPLOYMENT_HOSTS_PRIMARY }}
Expand Down

0 comments on commit e4387da

Please sign in to comment.