GTNH-Actions-Workflows/.github/workflows/trigger-rebuild-with-deps.yml

108 lines
4.6 KiB
YAML

name: Trigger a rebuild with externally required PRs
# Note: this reusable workflow must called from a context that is triggered by a "Build and test" run (which then propagates the event)
# That context requires workflow_run, which runs in the context of the default branch,
# therefore we use the statuses api to place it on the source commit manually here
on:
workflow_call:
permissions:
actions: write
contents: read
statuses: write
jobs:
trigger-rebuild-with-deps:
name: Apply required PRs from external repos
runs-on: ubuntu-latest
if: github.event.workflow_run.conclusion == 'failure'
&& github.event.workflow_run.event == 'pull_request'
&& github.event.workflow_run.run_attempt == 1
steps:
- name: Look up PR url
id: lookup
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }} # Not inlined below to prevent injection from branch names
run: |
PR_URL=$(gh api --method GET \
"repos/${{ github.repository }}/pulls" \
-f state=open \
-f head="${{ github.event.workflow_run.head_repository.owner.login }}:${HEAD_BRANCH}" \
-f base="${{ github.event.repository.default_branch }}" \
--jq "([.[] | select(.head.sha == \"${{ github.event.workflow_run.head_sha }}\")] | first | .html_url) // (first | .html_url) // empty")
if [ -z "$PR_URL" ]; then
echo "Failed to find a PR url"
exit 1
fi
echo "url=$PR_URL" >> "$GITHUB_OUTPUT"
- name: Collect required PRs
uses: MalTeeez/packscripts@a89fe398d047ac9e2e165465a51096be4f9853cb
env:
NO_COLOR: 1
with:
command: >-
pr deps ${{ steps.lookup.outputs.url }}
--target_dir ./.packscripts/
--jar_suffix -dev.jar
--artifact_name -dev.jar
--artifact_name build-libs
--build_job "Build and test"
--other_allowed_owner "GTNewHorizons"
github_token: ${{ secrets.GITHUB_TOKEN }}
config: https://raw.githubusercontent.com/MalTeeez/packscripts-auto-builds/refs/heads/gtnh-daily/packscripts.json
annotated_file: https://raw.githubusercontent.com/MalTeeez/packscripts-auto-builds/refs/heads/gtnh-daily/annotated_mods.json
- name: Check if deps were collected
id: check-deps
run: |
if [ -n "$(ls -A ./.packscripts/ 2>/dev/null)" ]; then
echo "found=true" >> "$GITHUB_OUTPUT"
else
echo "found=false" >> "$GITHUB_OUTPUT"
fi
- name: Mark status pending on PR commit
if: steps.check-deps.outputs.found == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh api -X POST \
repos/${{ github.repository }}/statuses/${{ github.event.workflow_run.head_sha }} \
-f state="pending" \
-f context="Trigger rebuild with deps" \
-f description="Applying external PR deps and rebuilding…" \
-f target_url="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
- name: Upload external deps artifact
if: steps.check-deps.outputs.found == 'true'
uses: actions/upload-artifact@v7
with:
# Named after the triggering run ID so the rerun can find it by its own run_id.
# workflow_run always runs in the default branch context, so artifacts would otherwise
# be filed under master's head_sha instead of the PR's, making them invisible to the rerun.
name: external-deps-${{ github.event.workflow_run.id }}
path: ./.packscripts/
include-hidden-files: true
- name: Rerun failed build
if: steps.check-deps.outputs.found == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh api -X POST \
repos/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }}/rerun-failed-jobs
- name: Report final status to PR commit
if: always() && steps.check-deps.outputs.found == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh api -X POST \
repos/${{ github.repository }}/statuses/${{ github.event.workflow_run.head_sha }} \
-f state="${{ job.status }}" \
-f context="Trigger rebuild with deps" \
-f description="External PR deps applied; rebuild triggered" \
-f target_url="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"