This commit is contained in:
Michał Zieliński
2025-09-13 20:08:32 +02:00
parent e0da58fb20
commit 73836dd9fc

View File

@@ -13,7 +13,7 @@ jobs:
REPO: DiunaBI REPO: DiunaBI
WORKFLOW_NAME: BuildApp WORKFLOW_NAME: BuildApp
steps: steps:
- name: Checkout (for tagging context only) - name: Checkout (for tag context only)
uses: https://github.com/actions/checkout@v4 uses: https://github.com/actions/checkout@v4
- name: Install tools - name: Install tools
@@ -21,68 +21,73 @@ jobs:
sudo apt-get update sudo apt-get update
sudo apt-get install -y jq zip sudo apt-get install -y jq zip
# 1) Find latest successful run of "BuildApp" # 1) Find latest successful run of "BuildApp" via REST API (/api/v1 ... /actions/tasks)
- name: Find latest successful BuildApp run - name: Find latest successful BuildApp run
id: find_run id: find_run
shell: bash shell: bash
run: | run: |
# Fetch recent runs and filter client-side set -euo pipefail
# NOTE: /api/v1 is the correct REST base for Gitea.
RESP="$(curl -fsSL \ RESP="$(curl -fsSL \
-H "Authorization: token ${{ secrets.GITEA_PAT }}" \ -H "Authorization: token ${{ secrets.GITEA_PAT }}" \
"$GITEA_BASE_URL/api/v1/repos/$OWNER/$REPO/actions/runs?limit=50")" "$GITEA_BASE_URL/api/v1/repos/$OWNER/$REPO/actions/tasks?limit=100")"
echo "$RESP" | jq . >/dev/null || { echo "Invalid JSON from API"; exit 1; } # Try to match by various fields (workflow.name in newer versions, fallbacks otherwise)
RUN_JSON="$(jq -r --arg W "$WORKFLOW_NAME" '
RUN_JSON="$(echo "$RESP" | jq -r \
--arg W "$WORKFLOW_NAME" '
.workflow_runs .workflow_runs
| map(select(.status=="completed" and .conclusion=="success" and .workflow.name==$W)) | map(
| sort_by(.id) | reverse | .[0] select(
')" (.status=="completed") and
(.conclusion=="success") and
(
(.workflow.name? // .workflow_name? // .display_title? // "") == $W
)
)
)
| sort_by(.run_number // .id)
| reverse
| .[0]
' <<< "$RESP")"
RUN_ID="$(echo "$RUN_JSON" | jq -r '.id')" if [[ -z "$RUN_JSON" || "$RUN_JSON" == "null" ]]; then
RUN_HEAD_SHA="$(echo "$RUN_JSON" | jq -r '.head_sha')" echo "No successful run found for workflow name: $WORKFLOW_NAME"
echo "Available runs (debug):"
if [[ -z "$RUN_ID" || "$RUN_ID" == "null" ]]; then echo "$RESP" | jq -r '.workflow_runs[] | {id, status, conclusion, display_title, workflow_name: .workflow_name, wname: .workflow.name}'
echo "No successful '$WORKFLOW_NAME' run found." >&2
exit 1 exit 1
fi fi
RUN_ID="$(jq -r '.id // .run_id' <<< "$RUN_JSON")"
HEAD_SHA="$(jq -r '.head_sha // .head_commit?.id // empty' <<< "$RUN_JSON")"
echo "run_id=$RUN_ID" >> "$GITHUB_OUTPUT" echo "run_id=$RUN_ID" >> "$GITHUB_OUTPUT"
echo "head_sha=$RUN_HEAD_SHA" >> "$GITHUB_OUTPUT" echo "head_sha=$HEAD_SHA" >> "$GITHUB_OUTPUT"
echo "Latest successful $WORKFLOW_NAME run: $RUN_ID ($RUN_HEAD_SHA)" echo "Latest successful $WORKFLOW_NAME run: $RUN_ID ($HEAD_SHA)"
# 2) List and download artifacts "frontend" and "webapi" via Gitea API # 2) Download artifacts using GUI-style URLs
- name: List artifacts for that run # These URLs serve ZIPs directly. Authorization via token MAY or MAY NOT work depending on instance config.
id: list_artifacts # If it fails with 302/403, consider using a community download action as a fallback.
run: | - name: Download 'frontend' artifact (GUI URL)
curl -fsSL \ shell: bash
-H "Authorization: token ${{ secrets.GITEA_PAT }}" \
"$GITEA_BASE_URL/api/v1/repos/$OWNER/$REPO/actions/runs/${{ steps.find_run.outputs.run_id }}/artifacts" \
| tee artifacts.json
# Fail early if neither artifact is present
(jq -e '.artifacts[] | select(.name=="frontend")' artifacts.json >/dev/null) || { echo "Artifact 'frontend' not found"; exit 1; }
(jq -e '.artifacts[] | select(.name=="webapi")' artifacts.json >/dev/null) || { echo "Artifact 'webapi' not found"; exit 1; }
- name: Download 'frontend' artifact (zip -> dir)
run: | run: |
set -euo pipefail
mkdir -p artifacts/frontend mkdir -p artifacts/frontend
ART_ID=$(jq -r '.artifacts[] | select(.name=="frontend") | .id' artifacts.json) # -L to follow redirects; --fail-with-body to fail on non-2xx
curl -fsSL \ curl -LfS --fail-with-body \
-H "Authorization: token ${{ secrets.GITEA_PAT }}" \ -H "Authorization: token ${{ secrets.GITEA_PAT }}" \
"$GITEA_BASE_URL/api/v1/repos/$OWNER/$REPO/actions/artifacts/$ART_ID/zip" \ "$GITEA_BASE_URL/$OWNER/$REPO/actions/runs/${{ steps.find_run.outputs.run_id }}/artifacts/frontend" \
-o frontend.zip -o frontend.zip
unzip -q frontend.zip -d artifacts/frontend unzip -q frontend.zip -d artifacts/frontend
rm -f frontend.zip rm -f frontend.zip
- name: Download 'webapi' artifact (zip -> dir) - name: Download 'webapi' artifact (GUI URL)
shell: bash
run: | run: |
set -euo pipefail
mkdir -p artifacts/webapi mkdir -p artifacts/webapi
ART_ID=$(jq -r '.artifacts[] | select(.name=="webapi") | .id' artifacts.json) curl -LfS --fail-with-body \
curl -fsSL \
-H "Authorization: token ${{ secrets.GITEA_PAT }}" \ -H "Authorization: token ${{ secrets.GITEA_PAT }}" \
"$GITEA_BASE_URL/api/v1/repos/$OWNER/$REPO/actions/artifacts/$ART_ID/zip" \ "$GITEA_BASE_URL/$OWNER/$REPO/actions/runs/${{ steps.find_run.outputs.run_id }}/artifacts/webapi" \
-o webapi.zip -o webapi.zip
unzip -q webapi.zip -d artifacts/webapi unzip -q webapi.zip -d artifacts/webapi
rm -f webapi.zip rm -f webapi.zip
@@ -96,7 +101,7 @@ jobs:
ls -laR artifacts/webapi || true ls -laR artifacts/webapi || true
echo "::endgroup::" echo "::endgroup::"
# 3) Package artifacts as ZIPs for release assets # 3) Package artifacts as ZIPs for the release assets
- name: Package artifacts as ZIPs - name: Package artifacts as ZIPs
run: | run: |
mkdir -p build mkdir -p build
@@ -104,11 +109,10 @@ jobs:
(cd artifacts/webapi && zip -rq ../../build/webapi.zip .) (cd artifacts/webapi && zip -rq ../../build/webapi.zip .)
ls -la build ls -la build
# 4) Autogenerate tag and title, then create release + upload assets # 4) Auto-generate tag/title and create the release on Gitea
- name: Generate tag and title - name: Generate tag and title
id: meta id: meta
run: | run: |
# Tag format: vYYYYMMDD-HHMM-run{RUN_ID}
TAG="v$(date -u +%Y%m%d-%H%M)-run${{ steps.find_run.outputs.run_id }}" TAG="v$(date -u +%Y%m%d-%H%M)-run${{ steps.find_run.outputs.run_id }}"
TITLE="Release ${TAG}" TITLE="Release ${TAG}"
echo "tag=$TAG" >> "$GITHUB_OUTPUT" echo "tag=$TAG" >> "$GITHUB_OUTPUT"
@@ -120,7 +124,7 @@ jobs:
api_key: ${{ secrets.GITEA_PAT }} api_key: ${{ secrets.GITEA_PAT }}
tag_name: ${{ steps.meta.outputs.tag }} tag_name: ${{ steps.meta.outputs.tag }}
name: ${{ steps.meta.outputs.title }} name: ${{ steps.meta.outputs.title }}
body: "Automated release generated from latest successful **${{ env.WORKFLOW_NAME }}** run (`run_id=${{ steps.find_run.outputs.run_id }}`, `commit=${{ steps.find_run.outputs.head_sha }}`)." body: "Automated release from latest successful **${{ env.WORKFLOW_NAME }}** run (`run_id=${{ steps.find_run.outputs.run_id }}`, `commit=${{ steps.find_run.outputs.head_sha }}`)."
files: | files: |
build/frontend.zip build/frontend.zip
build/webapi.zip build/webapi.zip