From 9c771f098c038c03d5b886078806105c7fa53d29 Mon Sep 17 00:00:00 2001 From: datadunia Date: Fri, 8 May 2026 05:41:06 +0700 Subject: [PATCH] refactor: extract shared CI steps to deploy_call.yaml reusable workflow --- .gitea/workflows/beta.yaml | 14 ++++ .gitea/workflows/deploy_call.yaml | 124 ++++++++++++++++++++++++++++++ .gitea/workflows/release.yaml | 119 ++-------------------------- 3 files changed, 144 insertions(+), 113 deletions(-) create mode 100644 .gitea/workflows/beta.yaml create mode 100644 .gitea/workflows/deploy_call.yaml diff --git a/.gitea/workflows/beta.yaml b/.gitea/workflows/beta.yaml new file mode 100644 index 0000000..72ac714 --- /dev/null +++ b/.gitea/workflows/beta.yaml @@ -0,0 +1,14 @@ +name: Beta Release + +on: + push: + tags: + - 'v*-beta*' + - 'v*-test*' + +jobs: + deploy: + uses: ./.gitea/workflows/deploy_call.yaml + with: + prerelease: true + secrets: inherit diff --git a/.gitea/workflows/deploy_call.yaml b/.gitea/workflows/deploy_call.yaml new file mode 100644 index 0000000..55e3c69 --- /dev/null +++ b/.gitea/workflows/deploy_call.yaml @@ -0,0 +1,124 @@ +name: Deploy + +on: + workflow_call: + inputs: + prerelease: + description: 'Mark as prerelease' + required: false + type: boolean + default: false + +permissions: + contents: write + +jobs: + build-and-release: + runs-on: ubuntu-latest + steps: + - name: Clone repository with submodules + run: | + git config --global --remove-section http || true + git config --global --unset-all core.askPass || true + TOKEN="${{ secrets.BUILD_TOKEN }}" + git clone --recurse-submodules \ + -c credential.helper="" \ + https://token:$TOKEN@git.datadunia.com/devops/wireguard-vpn.git . + + - name: Setup Go + uses: actions/setup-go@v5 + with: + go-version: '1.22' + + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: '20' + + - name: Build Vue frontend + run: | + cd app/frontend + npm install + npm run build + + - name: Build Go binaries + run: | + cd app + GOOS=linux GOARCH=amd64 go build -o ../wgrplane-linux-amd64 . + GOOS=linux GOARCH=arm64 go build -o ../wgrplane-linux-arm64 . + + - name: Generate latest.json + run: | + VERSION="${{ gitea.ref_name }}" + RELEASE_DATE="$(date -u +'%Y-%m-%dT%H:%M:%SZ')" + REPO="${{ gitea.repository }}" + SERVER="${{ gitea.server_url }}" + cat > latest.json << ENDJSON + { + "version": "$VERSION", + "release_date": "$RELEASE_DATE", + "download_urls": { + "amd64": "${SERVER}/${REPO}/releases/download/${VERSION}/wgrplane-linux-amd64", + "arm64": "${SERVER}/${REPO}/releases/download/${VERSION}/wgrplane-linux-arm64" + } + } + ENDJSON + + - name: Create Release and upload assets + env: + # PINDAHKAN TOKEN KE SINI + TOKEN: ${{ secrets.BUILD_TOKEN }} + run: | + # Sekarang $TOKEN sudah bisa dipakai di Bash + echo "--- Diagnostic ---" + if [ -z "$TOKEN" ]; then + echo "Value: [EMPTY]" + exit 1 + else + echo "Length: ${#TOKEN} characters" + fi + + REPO="${{ gitea.repository }}" + TAG="${{ gitea.ref_name }}" + API="${{ gitea.server_url }}/api/v1" + + # 1. Create Release + JSON_BODY=$(printf '{"tag_name":"%s","name":"%s","body":"Release %s","draft":false,"prerelease":%s}' "$TAG" "$TAG" "$TAG" "${{ inputs.prerelease }}") + + RELEASE_RESP=$(curl -s -X POST \ + -H "Authorization: token $TOKEN" \ + -H "Content-Type: application/json" \ + -d "$JSON_BODY" \ + "$API/repos/$REPO/releases") + + # Ambil ID dengan lebih teliti + RELEASE_ID=$(echo "$RELEASE_RESP" | grep -o '"id":[0-9]*' | head -n 1 | cut -d':' -f2) + + if [ -z "$RELEASE_ID" ] || [ "$RELEASE_ID" = "null" ]; then + echo "Gagal membuat release. Response: $RELEASE_RESP" + exit 1 + fi + + echo "Release ID: $RELEASE_ID" + + # 2. Upload Assets + for FILE in wgrplane-linux-amd64 wgrplane-linux-arm64 latest.json; do + if [ -f "$FILE" ]; then + echo "Uploading $FILE..." + curl -s -X POST \ + -H "Authorization: token $TOKEN" \ + -F "attachment=@$FILE" \ + "$API/repos/$REPO/releases/$RELEASE_ID/assets?name=$FILE" + else + echo "Skip $FILE (tidak ditemukan)" + fi + done + + - name: Cleanup build artifacts + if: always() + run: | + git config --global --remove-section http || true + git config --global --unset-all core.askPass || true + rm -f wgrplane-linux-amd64 wgrplane-linux-arm64 latest.json + rm -rf app/frontend/node_modules app/frontend/dist + echo "Cleanup done" diff --git a/.gitea/workflows/release.yaml b/.gitea/workflows/release.yaml index 0b4f417..8faa5c1 100644 --- a/.gitea/workflows/release.yaml +++ b/.gitea/workflows/release.yaml @@ -3,118 +3,11 @@ name: Release on: push: tags: - - 'v*' - -permissions: - contents: write + - 'v[0-9]*.[0-9]*.[0-9]*' jobs: - build-and-release: - runs-on: ubuntu-latest - steps: - - name: Clone repository with submodules - run: | - git config --global --remove-section http || true - git config --global --unset-all core.askPass || true - TOKEN="${{ secrets.BUILD_TOKEN }}" - git clone --recurse-submodules \ - -c credential.helper="" \ - https://token:$TOKEN@git.datadunia.com/devops/wireguard-vpn.git . - - - name: Setup Go - uses: actions/setup-go@v5 - with: - go-version: '1.22' - - - name: Setup Node.js - uses: actions/setup-node@v4 - with: - node-version: '20' - - - name: Build Vue frontend - run: | - cd app/frontend - npm install - npm run build - - - name: Build Go binaries - run: | - cd app - GOOS=linux GOARCH=amd64 go build -o ../wgrplane-linux-amd64 . - GOOS=linux GOARCH=arm64 go build -o ../wgrplane-linux-arm64 . - - - name: Generate latest.json - run: | - VERSION="${{ gitea.ref_name }}" - RELEASE_DATE="$(date -u +'%Y-%m-%dT%H:%M:%SZ')" - REPO="${{ gitea.repository }}" - SERVER="${{ gitea.server_url }}" - cat > latest.json << ENDJSON - { - "version": "$VERSION", - "release_date": "$RELEASE_DATE", - "download_urls": { - "amd64": "${SERVER}/${REPO}/releases/download/${VERSION}/wgrplane-linux-amd64", - "arm64": "${SERVER}/${REPO}/releases/download/${VERSION}/wgrplane-linux-arm64" - } - } - ENDJSON - - - name: Create Release and upload assets - env: - # PINDAHKAN TOKEN KE SINI - TOKEN: ${{ secrets.BUILD_TOKEN }} - run: | - # Sekarang $TOKEN sudah bisa dipakai di Bash - echo "--- Diagnostic ---" - if [ -z "$TOKEN" ]; then - echo "Value: [EMPTY]" - exit 1 - else - echo "Length: ${#TOKEN} characters" - fi - - REPO="${{ gitea.repository }}" - TAG="${{ gitea.ref_name }}" - API="${{ gitea.server_url }}/api/v1" - - # 1. Create Release - JSON_BODY=$(printf '{"tag_name":"%s","name":"%s","body":"Release %s","draft":false,"prerelease":false}' "$TAG" "$TAG" "$TAG") - - RELEASE_RESP=$(curl -s -X POST \ - -H "Authorization: token $TOKEN" \ - -H "Content-Type: application/json" \ - -d "$JSON_BODY" \ - "$API/repos/$REPO/releases") - - # Ambil ID dengan lebih teliti - RELEASE_ID=$(echo "$RELEASE_RESP" | grep -o '"id":[0-9]*' | head -n 1 | cut -d':' -f2) - - if [ -z "$RELEASE_ID" ] || [ "$RELEASE_ID" = "null" ]; then - echo "Gagal membuat release. Response: $RELEASE_RESP" - exit 1 - fi - - echo "Release ID: $RELEASE_ID" - - # 2. Upload Assets - for FILE in wgrplane-linux-amd64 wgrplane-linux-arm64 latest.json; do - if [ -f "$FILE" ]; then - echo "Uploading $FILE..." - curl -s -X POST \ - -H "Authorization: token $TOKEN" \ - -F "attachment=@$FILE" \ - "$API/repos/$REPO/releases/$RELEASE_ID/assets?name=$FILE" - else - echo "Skip $FILE (tidak ditemukan)" - fi - done - - - name: Cleanup build artifacts - if: always() - run: | - git config --global --remove-section http || true - git config --global --unset-all core.askPass || true - rm -f wgrplane-linux-amd64 wgrplane-linux-arm64 latest.json - rm -rf app/frontend/node_modules app/frontend/dist - echo "Cleanup done" + deploy: + uses: ./.gitea/workflows/deploy_call.yaml + with: + prerelease: false + secrets: inherit