name: 🚀 Release

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

jobs:
  check:
    name: Check
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v3
        with:
          submodules: recursive
      - name: Setup Rust toolchain and cache
        uses: actions-rust-lang/setup-rust-toolchain@v1.3.7
      - name: Install just
        uses: taiki-e/install-action@just
      - name: Install requirements
        run: sudo env PATH=$PATH just ci-prepare
      - name: Build
        run: just youki-release
      - name: test
        # TODO(utam0k): The feature test needs nightly
        # run: just test-basic featuretest test-oci
        run: just test-basic test-oci

  upload:
    name: Upload
    needs: check
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v3
      - name: Setup Rust toolchain and cache
        uses: actions-rust-lang/setup-rust-toolchain@v1.3.7
      - uses: taiki-e/install-action@just
      - name: Install requirements
        run: sudo env PATH=$PATH just ci-prepare
      - name: Release build
        run: just youki-release
      - name: Create output directory
        run: mkdir output
      - name: Copy files to output
        run: |
          cp youki output/
          cp README.md output/README.md
          cp LICENSE output/LICENSE
      - name: Upload artifact
        uses: actions/upload-artifact@v3
        with:
          name: output
          path: output/*

  release:
    name: Create Draft Release
    runs-on: ubuntu-20.04
    needs:
      - upload
    steps:
      - uses: actions/checkout@v3

      - name: Determine Release Info
        id: info
        env:
          GITHUB_REF: ${{ github.ref }}
        run: |
          VERSION=${GITHUB_REF##*v}
          MAJOR=${VERSION%%.*}
          MINOR=${VERSION%.*}
          MINOR=${MINOR#*.}
          PATCH=${VERSION##*.}
          echo "VERSION=${VERSION}" >> $GITHUB_ENV
          echo "OUTPUTDIR=youki_${MAJOR}_${MINOR}_${PATCH}_linux" >> $GITHUB_ENV
          echo "INNERDIR=youki-${VERSION}" >> $GITHUB_ENV

      - name: Create Output Directory
        run: mkdir -p ${{ env.OUTPUTDIR }}/${{ env.INNERDIR }}

      - name: Download Linux Artifacts
        uses: actions/download-artifact@v3
        with:
          name: output
          path: ${{ env.OUTPUTDIR }}/${{ env.INNERDIR }}

      - name: Restore File Modes
        run: |
          chmod 755 ${{ env.OUTPUTDIR }}/${{ env.INNERDIR }}/youki
      - name: Create tarball
        run: tar -zcvf ${{ env.OUTPUTDIR }}.tar.gz ${{ env.OUTPUTDIR }}
      - name: Create Release Draft
        uses: softprops/action-gh-release@v1
        with:
          name: ${{ env.VERSION }} Release
          draft: true
          token: ${{ secrets.GITHUB_TOKEN }}
          files: ./${{ env.OUTPUTDIR }}.tar.gz

  publish:
    name: Publish Packages
    needs: check
    runs-on: ubuntu-20.04
    env:
      CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
    steps:
      - uses: actions/checkout@v3
      - name: Setup Rust toolchain and cache
        uses: actions-rust-lang/setup-rust-toolchain@v1.3.7
      - name: Publish libcgroups
        run: cargo publish -p libcgroups --no-verify
      - name: Publish libcontainer
        run: cargo publish -p libcontainer --no-verify
      - name: Publish liboci-cli
        run: cargo publish -p liboci-cli --no-verify
      - name: Publish youki
        run: cargo publish -p youki --no-verify
