name: release

on:
  push:
    tags:
      - v[0-9]+.[0-9]+.[0-9]+*

env:
  CARGO_TERM_COLOR: always
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-go@v3
        with:
          go-version: "1.19.6"
      - name: cache go mod
        uses: actions/cache@v3
        with:
          path: ~/go/pkg/mod
          key: ${{ runner.os }}-go-${{ hashFiles('go.sum') }}
          restore-keys: |
            ${{ runner.os }}-go
      - name: cache cargo
        uses: actions/cache@v3
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            tools/optimizer-server/target/
          key: ${{ runner.os }}-cargo-${{ hashFiles('tools/optimizer-server/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo
      - name: build nydus-snapshotter and optimizer
        run: |
          go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.51.2
          export PATH=$PATH:$(go env GOPATH)/bin
          make static-release
      - name: upload artifacts
        uses: actions/upload-artifact@v3
        with:
          name: nydus-snapshotter_artifacts
          path: |
            bin/containerd-nydus-grpc
            bin/nydus-overlayfs
            bin/optimizer-nri-plugin
            bin/optimizer-server
  upload:
    runs-on: ubuntu-latest
    needs: [build]
    steps:
      - uses: actions/checkout@v3
      - name: Download Artifacts
        uses: actions/download-artifact@v3
        with:
          name: nydus-snapshotter_artifacts
          path: nydus-snapshotter
      - name: Upload Artifacts
        run: |
          tag=$(echo $GITHUB_REF | cut -d/ -f3-)
          tarball="nydus-snapshotter-$tag-x86_64.tgz"
          chmod +x nydus-snapshotter/*
          tar cf - nydus-snapshotter | gzip > ${tarball}
          echo "tag=$tag" >> $GITHUB_ENV
          echo "tarball=$tarball" >> $GITHUB_ENV

          tarball_shasum="$tarball.sha256sum"
          sha256sum $tarball > $tarball_shasum
          echo "tarball_shasum=$tarball_shasum" >> $GITHUB_ENV
      - name: Release
        uses: softprops/action-gh-release@v1
        with:
          name: "Nydus Snapshotter ${{ env.tag }} Release"
          generate_release_notes: true
          files: |
            ${{ env.tarball }}
            ${{ env.tarball_shasum }}

  publish-image:
    runs-on: ubuntu-latest
    needs: [build]
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
      - name: Log in to the container registry
        uses: docker/login-action@v2
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: download artifacts
        uses: actions/download-artifact@v3
        with:
          name: nydus-snapshotter_artifacts
          path: misc/snapshotter
      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
      - name: Get the nydusd version
        id: step_one
        run: |
          export NYDUS_STABLE_VER=$(curl https://api.github.com/repos/dragonflyoss/nydus/releases/latest | jq -r .tag_name)
          echo "NYDUS_STABLE_VER=$NYDUS_STABLE_VER" >> "$GITHUB_ENV"
          printf 'nydus version is: %s\n' "$NYDUS_STABLE_VER"
      - name: build and push nydus-snapshotter image
        uses: docker/build-push-action@v3
        with:
          context: misc/snapshotter
          file: misc/snapshotter/Dockerfile
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          build-args: NYDUS_VER=${{ env.NYDUS_STABLE_VER }}
