Documentation
name: release

# Only do the release on x.y.z tags.
on:
  push:
    tags:
      - "[0-9]+.[0-9]+.[0-9]+"

# We need this to be able to create releases.
permissions:
  contents: write

jobs:
  # The create-release job runs purely to initialize the GitHub release itself,
  # and names the release after the `x.y.z` tag that was pushed. It's separate
  # from building the release so that we only create the release once.
  create-release:
    name: create-release
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Get the release version from the tag
        if: env.VERSION == ''
        run: echo "VERSION=${{ github.ref_name }}" >> $GITHUB_ENV
      - name: Show the version
        run: |
          echo "version is: $VERSION"
      - name: Check that tag version and Cargo.toml version are the same
        shell: bash
        run: |
          if ! grep -q "version = \"$VERSION\"" Cargo.toml; then
            echo "version does not match Cargo.toml" >&2
            exit 1
          fi
      - name: Create GitHub release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: gh release create $VERSION --draft --verify-tag --title $VERSION
    outputs:
      version: ${{ env.VERSION }}

  build-release:
    name: build-release
    needs: ["create-release"]
    runs-on: ${{ matrix.os }}
    env:
      CARGO: cargo
      TARGET_FLAGS: ""
      TARGET_DIR: ./target
      # Emit backtraces on panics.
      RUST_BACKTRACE: 1
    strategy:
      fail-fast: false
      matrix:
        include:
          - build: linux
            os: ubuntu-latest
            rust: nightly
            target: x86_64-unknown-linux-musl
          - build: stable-x86
            os: ubuntu-latest
            rust: stable
            target: i686-unknown-linux-gnu
          - build: stable-aarch64
            os: ubuntu-latest
            rust: stable
            target: aarch64-unknown-linux-gnu
          - build: stable-arm-gnueabihf
            os: ubuntu-latest
            rust: stable
            target: armv7-unknown-linux-gnueabihf
          - build: stable-arm-musleabihf
            os: ubuntu-latest
            rust: stable
            target: armv7-unknown-linux-musleabihf
          - build: stable-arm-musleabi
            os: ubuntu-latest
            rust: stable
            target: armv7-unknown-linux-musleabi
          - build: macos-x86_64
            os: macos-latest
            rust: nightly
            target: x86_64-apple-darwin
          - build: macos
            os: macos-latest
            rust: nightly
            target: aarch64-apple-darwin

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Install packages (Ubuntu)
        if: matrix.os == 'ubuntu-latest'
        shell: bash
        run: |
          sudo apt-get update
          sudo apt-get install -y musl-tools gcc-aarch64-linux-gnu gcc-arm-linux-gnueabihf

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

      - name: Set target variables
        shell: bash
        run: |
          echo "TARGET_FLAGS=--target ${{ matrix.target }}" >> $GITHUB_ENV
          echo "TARGET_DIR=./target/${{ matrix.target }}" >> $GITHUB_ENV

      - name: Show command used for Cargo
        shell: bash
        run: |
          echo "cargo command is: ${{ env.CARGO }}"
          echo "target flag is: ${{ env.TARGET_FLAGS }}"
          echo "target dir is: ${{ env.TARGET_DIR }}"

      - name: Build release binary
        shell: bash
        run: |
          ${{ env.CARGO }} build --verbose --release ${{ env.TARGET_FLAGS }}
          echo "BIN=target/${{ matrix.target }}/release/redito" >> $GITHUB_ENV

      - name: Determine binary name
        shell: bash
        run: |
          version="${{ needs.create-release.outputs.version }}"
          echo "BINARY=redito-$version-${{ matrix.target }}" >> $GITHUB_ENV

      - name: Binary sanity check
        shell: bash
        run: |
          "${{ env.BIN }}" --version
          "${{ env.BIN }}" --help

      - name: Setup binary for upload
        shell: bash
        run: |
          mv "${{ env.BIN }}" "${{ env.BINARY }}"
          shasum -a 256 "${{ env.BINARY }}" > "${{ env.BINARY }}.sha256"
          echo "ASSET=$BINARY" >> $GITHUB_ENV
          echo "ASSET_SUM=$BINARY.sha256" >> $GITHUB_ENV

      - name: Upload release archive
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        shell: bash
        run: |
          version="${{ needs.create-release.outputs.version }}"
          gh release upload "$version" ${{ env.ASSET }} ${{ env.ASSET_SUM }}

      - name: Upload artifact for Docker image
        uses: actions/upload-artifact@v4
        with:
          name: redito-${{ matrix.target }}
          path: ${{ env.ASSET }}

  publish-docker:
    name: Publish Docker images
    needs: ["build-release", "create-release"]
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
      id-token: write
    env:
      VERSION: ${{ needs.create-release.outputs.version }}
      IMAGE_NAME: redito
      DOCKERHUB_USER: felipou
      QUAY_USER: felipou
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Download compiled binaries
        uses: actions/download-artifact@v4
        with:
          path: ./dist

      - name: List downloaded files
        run: ls -R ./dist

      - name: Prepare Docker build context
        run: |
          mkdir docker-build
          mv ./dist/redito-x86_64-unknown-linux-musl ./docker-build/redito
          chmod +x ./docker-build/redito
          cat > docker-build/Dockerfile <<'EOF'
          FROM scratch
          COPY redito /usr/local/bin/redito
          ENTRYPOINT ["/usr/local/bin/redito"]
          EOF

      - name: Log in to DockerHub
        uses: docker/login-action@v3
        with:
          username: ${{ env.DOCKERHUB_USER }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Log in to Quay.io
        uses: docker/login-action@v3
        with:
          registry: quay.io
          username: ${{ env.QUAY_USER }}
          password: ${{ secrets.QUAY_TOKEN }}

      - name: Build and push Docker images
        uses: docker/build-push-action@v6
        with:
          context: ./docker-build
          push: true
          tags: |
            ${{ env.DOCKERHUB_USER }}/${{ env.IMAGE_NAME }}:${{ env.VERSION }}
            ${{ env.DOCKERHUB_USER }}/${{ env.IMAGE_NAME }}:latest
            quay.io/${{ env.QUAY_USER }}/${{ env.IMAGE_NAME }}:${{ env.VERSION }}
            quay.io/${{ env.QUAY_USER }}/${{ env.IMAGE_NAME }}:latest