archivr 0.2.1

A Tumblr backup tool
Documentation
name: Build

on:
  workflow_dispatch:
  push:
    tags:
      - "v*"

permissions:
  contents: write

jobs:
  build:
    name: Build (${{ matrix.target }})
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-latest
            use_cross: true
          - target: x86_64-apple-darwin
            os: macos-latest
          - target: aarch64-apple-darwin
            os: macos-latest
          - target: x86_64-pc-windows-msvc
            os: windows-latest

    steps:
      - uses: actions/checkout@v6

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}

      - uses: Swatinem/rust-cache@v2
        with:
          key: ${{ matrix.target }}

      - name: Install cross
        if: matrix.use_cross
        uses: taiki-e/install-action@cross

      - name: Build (cross)
        if: matrix.use_cross
        run: cross build --release --target ${{ matrix.target }}

      - name: Build
        if: ${{ !matrix.use_cross }}
        run: cargo build --release --target ${{ matrix.target }}

      - name: Prepare artifact (unix)
        if: runner.os != 'Windows'
        run: |
          mkdir -p dist
          cp target/${{ matrix.target }}/release/archivr dist/
          cd dist
          tar czf archivr-${{ matrix.target }}.tar.gz archivr
          rm archivr

      - name: Prepare artifact (windows)
        if: runner.os == 'Windows'
        shell: pwsh
        run: |
          New-Item -ItemType Directory -Force -Path dist
          Copy-Item "target/${{ matrix.target }}/release/archivr.exe" dist/
          Compress-Archive -Path dist/archivr.exe -DestinationPath "dist/archivr-${{ matrix.target }}.zip"
          Remove-Item dist/archivr.exe

      - name: Upload artifact
        uses: actions/upload-artifact@v6
        with:
          name: archivr-${{ matrix.target }}
          path: dist/archivr-${{ matrix.target }}.*

  release:
    name: Release
    if: startsWith(github.ref, 'refs/tags/')
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Download artifacts
        uses: actions/download-artifact@v8
        with:
          path: artifacts
          merge-multiple: true

      - name: Create release
        uses: softprops/action-gh-release@v2
        with:
          generate_release_notes: true
          files: artifacts/*

  codeberg-release:
    name: Upload to Codeberg Release
    if: startsWith(github.ref, 'refs/tags/')
    needs: release
    runs-on: ubuntu-latest
    steps:
      - name: Download artifacts
        uses: actions/download-artifact@v8
        with:
          path: artifacts
          merge-multiple: true

      - name: Upload to Codeberg release
        env:
          CODEBERG_TOKEN: ${{ secrets.CODEBERG_TOKEN }}
          TAG: ${{ github.ref_name }}
        run: |
          REPO="ryf/archivr"
          API="https://codeberg.org/api/v1"

          # Get existing release or create one
          RELEASE_ID=$(curl -sf \
            -H "Authorization: token ${CODEBERG_TOKEN}" \
            "${API}/repos/${REPO}/releases/tags/${TAG}" \
            | jq -r '.id // empty')

          if [ -z "$RELEASE_ID" ]; then
            RELEASE_ID=$(curl -sf -X POST \
              -H "Authorization: token ${CODEBERG_TOKEN}" \
              -H "Content-Type: application/json" \
              -d "{\"tag_name\": \"${TAG}\", \"name\": \"${TAG}\"}" \
              "${API}/repos/${REPO}/releases" \
              | jq -r '.id')
          fi

          # Upload each artifact
          for file in artifacts/*; do
            filename=$(basename "$file")
            echo "Uploading ${filename}..."
            curl -sf -X POST \
              -H "Authorization: token ${CODEBERG_TOKEN}" \
              -F "attachment=@${file}" \
              "${API}/repos/${REPO}/releases/${RELEASE_ID}/assets?name=${filename}"
          done