name: Release Docker Images for RC

on:
  push:
    branches:
      - rc/**

jobs:

  build-rc:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Setup semver
      id: semver
      run: |
        semver=`npm run version --silent`
        echo "::set-output name=SEMVER::$semver"

    - name: Docker meta
      id: meta
      uses: docker/metadata-action@v3
      with:
        images: weseek/growi,ghcr.io/weseek/growi
        tags: |
          type=raw,value=${{ steps.semver.outputs.SEMVER }}
          type=raw,value=${{ steps.semver.outputs.SEMVER }}.{{sha}}

    - name: Login to docker.io registry
      run: |
        echo ${{ secrets. DOCKER_REGISTRY_PASSWORD }} | docker login --username wsmoogle --password-stdin

    - name: Login to GitHub Container Registry
      uses: docker/login-action@v1
      with:
        registry: ghcr.io
        username: wsmoogle
        password: ${{ secrets.DOCKER_REGISTRY_ON_GITHUB_PASSWORD }}

    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v1

    - name: Cache Docker layers
      uses: actions/cache@v2
      with:
        path: /tmp/.buildx-cache
        key: ${{ runner.os }}-buildx-app-${{ github.sha }}
        restore-keys: |
          ${{ runner.os }}-buildx-app-

    - name: Build and push
      uses: docker/build-push-action@v2
      with:
        context: .
        file: ./packages/app/docker/Dockerfile
        platforms: linux/amd64
        push: true
        cache-from: type=local,src=/tmp/.buildx-cache
        cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-new
        tags: ${{ steps.meta.outputs.tags }}

    - name: Move cache
      run: |
        rm -rf /tmp/.buildx-cache
        mv /tmp/.buildx-cache-new /tmp/.buildx-cache
