Update Version File #109
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Update Version File | |
| permissions: | |
| contents: write | |
| on: | |
| workflow_run: | |
| workflows: | |
| - Update Docker Compose | |
| types: | |
| - completed | |
| env: | |
| API_URL: https://baizhi.cloud:9443/api/open/site/1/static | |
| VERSION_FILE_PATH: /koala-qa/version.json | |
| jobs: | |
| sync-version: | |
| if: ${{ github.event.workflow_run.conclusion == 'success' }} | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| with: | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| fetch-depth: 0 | |
| ref: ${{ github.event.repository.default_branch }} | |
| - name: Determine version tag | |
| id: version_info | |
| run: | | |
| set -euo pipefail | |
| TARGET_SHA="${{ github.event.workflow_run.head_sha }}" | |
| VERSION="${{ github.event.workflow_run.head_branch }}" | |
| if [ -n "$VERSION" ] && [[ "$VERSION" =~ ^v[0-9]+\.[0-9]+\.[0-9]+ ]]; then | |
| echo "version=$VERSION" >> "$GITHUB_OUTPUT" | |
| exit 0 | |
| fi | |
| git fetch --tags --force --prune | |
| VERSION=$(git tag --points-at "$TARGET_SHA" | grep '^v' | head -n1 || true) | |
| if [ -z "$VERSION" ]; then | |
| echo "No matching version tag found; skipping." | |
| echo "skip=true" >> "$GITHUB_OUTPUT" | |
| exit 0 | |
| fi | |
| echo "version=$VERSION" >> "$GITHUB_OUTPUT" | |
| - name: Update version.json | |
| if: steps.version_info.outputs.skip != 'true' | |
| run: | | |
| set -euo pipefail | |
| VERSION="${{ steps.version_info.outputs.version }}" | |
| jq -n --arg version "$VERSION" '{"version": $version}' > version.json | |
| if git diff --quiet -- version.json; then | |
| echo "version.json already set to $VERSION." | |
| else | |
| git config --local user.email "action@github.com" | |
| git config --local user.name "GitHub Action" | |
| git add version.json | |
| git commit -m "chore: update version.json to $VERSION" | |
| git push | |
| fi | |
| - name: Encode version.json to base64 | |
| if: steps.version_info.outputs.skip != 'true' | |
| id: encode_version | |
| run: | | |
| ENCODED_CONTENT=$(base64 -w 0 version.json) | |
| echo "encoded_content=$ENCODED_CONTENT" >> "$GITHUB_OUTPUT" | |
| echo "Content encoded successfully (length: ${#ENCODED_CONTENT})" | |
| - name: Push version.json via API | |
| if: steps.version_info.outputs.skip != 'true' | |
| run: | | |
| VERSION="${{ steps.version_info.outputs.version }}" | |
| RESPONSE=$(curl -s -w "\nHTTP_CODE:%{http_code}" \ | |
| '${{ env.API_URL }}' \ | |
| -H 'sec-ch-ua-platform: "Linux"' \ | |
| -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36' \ | |
| -H 'sec-ch-ua: "Chromium";v="140", "Not=A?Brand";v="24", "Google Chrome";v="140"' \ | |
| -H 'Content-Type: application/json' \ | |
| -H 'sec-ch-ua-mobile: ?0' \ | |
| -H 'X-SLCE-API-TOKEN: ${{ secrets.SLCE_API_TOKEN }}' \ | |
| --data-raw '{ | |
| "path": "'"${{ env.VERSION_FILE_PATH }}"'", | |
| "page": "'"${{ steps.encode_version.outputs.encoded_content }}"'", | |
| "zip": false, | |
| "dir": false | |
| }') | |
| HTTP_CODE=$(echo "$RESPONSE" | tail -n1 | cut -d: -f2) | |
| RESPONSE_BODY=$(echo "$RESPONSE" | head -n -1) | |
| echo "HTTP Code: $HTTP_CODE" | |
| echo "Response: $RESPONSE_BODY" | |
| if [ "$HTTP_CODE" -ge 200 ] && [ "$HTTP_CODE" -lt 300 ]; then | |
| echo "✅ Successfully pushed version.json via API" | |
| echo "📦 Version: $VERSION" | |
| else | |
| echo "❌ API request failed with HTTP code: $HTTP_CODE" | |
| echo "Response: $RESPONSE_BODY" | |
| exit 1 | |
| fi |