From e0da58fb20ea39d81ce1c0cbd4a1e717da7b7482 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Zieli=C5=84ski?= Date: Sat, 13 Sep 2025 19:58:59 +0200 Subject: [PATCH] release1 --- .gitea/workflows/morskaRelease.yml | 159 ++++++++++++++++++++--------- 1 file changed, 109 insertions(+), 50 deletions(-) diff --git a/.gitea/workflows/morskaRelease.yml b/.gitea/workflows/morskaRelease.yml index 55c6a01..179c3da 100644 --- a/.gitea/workflows/morskaRelease.yml +++ b/.gitea/workflows/morskaRelease.yml @@ -1,67 +1,126 @@ -name: Release Morska (from latest build cache) +name: ReleaseApp (auto from latest successful BuildApp) on: - workflow_dispatch: {} - -env: - DEPLOY_HOST: "bim-it.pl" - DEPLOY_USER: "mz" - DEPLOY_PATH: "./deployment/" - SSH_KEYFILE: "/ci-keys/morska" + workflow_dispatch: {} # Manual trigger with ZERO inputs jobs: release: runs-on: ubuntu-latest + env: + # --- Adjust to your instance/repo if needed --- + GITEA_BASE_URL: https://code.bim-it.pl + OWNER: mz + REPO: DiunaBI + WORKFLOW_NAME: BuildApp steps: - - name: Checkout (for completeness) + - name: Checkout (for tagging context only) uses: https://github.com/actions/checkout@v4 - - name: Tools + - name: Install tools run: | - set -euo pipefail - apt-get update -y - apt-get install -y zip openssh-client + sudo apt-get update + sudo apt-get install -y jq zip - - name: Verify runner cache - env: - SRC: /runner-cache/builds/latest + # 1) Find latest successful run of "BuildApp" + - name: Find latest successful BuildApp run + id: find_run + shell: bash run: | - set -euo pipefail - echo "Expecting artifacts in: $SRC" - test -d "$SRC/frontend" || { echo "Missing $SRC/frontend"; exit 1; } - test -d "$SRC/webapi" || { echo "Missing $SRC/webapi"; exit 1; } - ls -la "$SRC" + # Fetch recent runs and filter client-side + RESP="$(curl -fsSL \ + -H "Authorization: token ${{ secrets.GITEA_PAT }}" \ + "$GITEA_BASE_URL/api/v1/repos/$OWNER/$REPO/actions/runs?limit=50")" - - name: Create archives - env: - SRC: /runner-cache/builds/latest - run: | - set -euo pipefail - mkdir -p release - (cd "$SRC/frontend" && zip -r "$GITHUB_WORKSPACE/release/DiunaBI-Morska-Frontend.zip" .) - (cd "$SRC/webapi" && zip -r "$GITHUB_WORKSPACE/release/DiunaBI-Morska-WebApi.zip" .) - ls -la release + echo "$RESP" | jq . >/dev/null || { echo "Invalid JSON from API"; exit 1; } - - name: Prepare known_hosts - run: | - set -euo pipefail - mkdir -p ~/.ssh - ssh-keyscan -H "${{ env.DEPLOY_HOST }}" >> ~/.ssh/known_hosts 2>/dev/null || true + RUN_JSON="$(echo "$RESP" | jq -r \ + --arg W "$WORKFLOW_NAME" ' + .workflow_runs + | map(select(.status=="completed" and .conclusion=="success" and .workflow.name==$W)) + | sort_by(.id) | reverse | .[0] + ')" - - name: Upload via SCP - run: | - set -euo pipefail - scp -i "${{ env.SSH_KEYFILE }}" -o IdentitiesOnly=yes -o StrictHostKeyChecking=yes \ - ./release/DiunaBI-Morska-Frontend.zip \ - "${{ env.DEPLOY_USER }}@${{ env.DEPLOY_HOST }}:${{ env.DEPLOY_PATH }}" - scp -i "${{ env.SSH_KEYFILE }}" -o IdentitiesOnly=yes -o StrictHostKeyChecking=yes \ - ./release/DiunaBI-Morska-WebApi.zip \ - "${{ env.DEPLOY_USER }}@${{ env.DEPLOY_HOST }}:${{ env.DEPLOY_PATH }}" + RUN_ID="$(echo "$RUN_JSON" | jq -r '.id')" + RUN_HEAD_SHA="$(echo "$RUN_JSON" | jq -r '.head_sha')" - - name: Remote deploy + if [[ -z "$RUN_ID" || "$RUN_ID" == "null" ]]; then + echo "No successful '$WORKFLOW_NAME' run found." >&2 + exit 1 + fi + + echo "run_id=$RUN_ID" >> "$GITHUB_OUTPUT" + echo "head_sha=$RUN_HEAD_SHA" >> "$GITHUB_OUTPUT" + echo "Latest successful $WORKFLOW_NAME run: $RUN_ID ($RUN_HEAD_SHA)" + + # 2) List and download artifacts "frontend" and "webapi" via Gitea API + - name: List artifacts for that run + id: list_artifacts run: | - set -euo pipefail - ssh -i "${{ env.SSH_KEYFILE }}" -o IdentitiesOnly=yes -o StrictHostKeyChecking=yes \ - "${{ env.DEPLOY_USER }}@${{ env.DEPLOY_HOST }}" << 'EOF' - ./deployment/DiunaBI-Morska.Release.sh - EOF \ No newline at end of file + curl -fsSL \ + -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: | + mkdir -p artifacts/frontend + ART_ID=$(jq -r '.artifacts[] | select(.name=="frontend") | .id' artifacts.json) + curl -fsSL \ + -H "Authorization: token ${{ secrets.GITEA_PAT }}" \ + "$GITEA_BASE_URL/api/v1/repos/$OWNER/$REPO/actions/artifacts/$ART_ID/zip" \ + -o frontend.zip + unzip -q frontend.zip -d artifacts/frontend + rm -f frontend.zip + + - name: Download 'webapi' artifact (zip -> dir) + run: | + mkdir -p artifacts/webapi + ART_ID=$(jq -r '.artifacts[] | select(.name=="webapi") | .id' artifacts.json) + curl -fsSL \ + -H "Authorization: token ${{ secrets.GITEA_PAT }}" \ + "$GITEA_BASE_URL/api/v1/repos/$OWNER/$REPO/actions/artifacts/$ART_ID/zip" \ + -o webapi.zip + unzip -q webapi.zip -d artifacts/webapi + rm -f webapi.zip + + - name: Show artifact structure + run: | + echo "::group::frontend" + ls -laR artifacts/frontend || true + echo "::endgroup::" + echo "::group::webapi" + ls -laR artifacts/webapi || true + echo "::endgroup::" + + # 3) Package artifacts as ZIPs for release assets + - name: Package artifacts as ZIPs + run: | + mkdir -p build + (cd artifacts/frontend && zip -rq ../../build/frontend.zip .) + (cd artifacts/webapi && zip -rq ../../build/webapi.zip .) + ls -la build + + # 4) Autogenerate tag and title, then create release + upload assets + - name: Generate tag and title + id: meta + run: | + # Tag format: vYYYYMMDD-HHMM-run{RUN_ID} + TAG="v$(date -u +%Y%m%d-%H%M)-run${{ steps.find_run.outputs.run_id }}" + TITLE="Release ${TAG}" + echo "tag=$TAG" >> "$GITHUB_OUTPUT" + echo "title=$TITLE" >> "$GITHUB_OUTPUT" + + - name: Create Gitea release with assets + uses: https://gitea.com/actions/release-action@main + with: + api_key: ${{ secrets.GITEA_PAT }} + tag_name: ${{ steps.meta.outputs.tag }} + 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 }}`)." + files: | + build/frontend.zip + build/webapi.zip \ No newline at end of file