name: Release Docker Image for @growi/slackbot-proxy

on:
  pull_request:
    branches:
      - release/slackbot-proxy/**
    types: [closed]

jobs:

  build-and-push-image:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
      with:
        ref: ${{ github.event.pull_request.base.ref }}

    - name: Retrieve information from package.json
      uses: myrotvorets/info-from-package-json-action@1.2.0
      id: package-json
      with:
        workingDir: packages/slackbot-proxy

    - name: Docker meta
      id: meta
      uses: docker/metadata-action@v4
      with:
        images: weseek/growi-slackbot-proxy,ghcr.io/weseek/growi-slackbot-proxy,asia.gcr.io/${{ secrets.GCP_PRJ_ID_SLACKBOT_PROXY }}/growi-slackbot-proxy
        tags: |
          type=raw,value=latest
          type=raw,value=${{ steps.package-json.outputs.packageVersion }}

    - 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@v2
      with:
        registry: ghcr.io
        username: wsmoogle
        password: ${{ secrets.DOCKER_REGISTRY_ON_GITHUB_PASSWORD }}

    - name: Authenticate to Google Cloud for GROWI.cloud
      uses: google-github-actions/auth@v1
      with:
        credentials_json: '${{ secrets.GCP_SA_KEY_SLACKBOT_PROXY }}'

    - name: Setup gcloud
      uses: google-github-actions/setup-gcloud@v1

    - name: Configure docker for gcloud
      run: |
        gcloud auth configure-docker --quiet

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

    - name: Build and push
      uses: docker/build-push-action@v2
      with:
        context: .
        file: ./packages/slackbot-proxy/docker/Dockerfile
        platforms: linux/amd64
        push: true
        builder: ${{ steps.buildx.outputs.name }}
        cache-from: type=gha
        cache-to: type=gha,mode=max
        tags: ${{ steps.meta.outputs.tags }}

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

    - name: Add tag
      uses: anothrNick/github-tag-action@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        CUSTOM_TAG: v${{ steps.package-json.outputs.packageVersion }}
        VERBOSE : true

    - name: Update Docker Hub Description
      uses: peter-evans/dockerhub-description@v3
      with:
        username: wsmoogle
        password: ${{ secrets.DOCKER_REGISTRY_PASSWORD }}
        repository: weseek/growi-slackbot-proxy
        readme-filepath: ./packages/slackbot-proxy/docker/README.md


  create-pr-for-next-rc:
    needs: build-and-push-image

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
      with:
        ref: ${{ github.event.pull_request.base.ref }}

    - uses: actions/setup-node@v3
      with:
        node-version: '16'
        cache: 'yarn'
        cache-dependency-path: '**/yarn.lock'

    - name: Install dependencies
      run: |
        npx lerna bootstrap

    - name: Bump versions for next RC
      run: |
        yarn bump-versions:slackbot-proxy

    - name: Retrieve information from package.json
      uses: myrotvorets/info-from-package-json-action@1.2.0
      id: package-json
      with:
        workingDir: packages/slackbot-proxy

    - name: Commit
      uses: github-actions-x/commit@v2.9
      with:
        github-token: ${{ secrets.GITHUB_TOKEN }}
        push-branch: support/prepare-v${{ steps.package-json.outputs.packageVersion }}
        commit-message: 'Bump version'
        name: GitHub Action

    - name: Create PR
      uses: repo-sync/pull-request@v2
      with:
        source_branch: support/prepare-v${{ steps.package-json.outputs.packageVersion }}
        destination_branch: master
        pr_title: Prepare v${{ steps.package-json.outputs.packageVersion }}
        pr_label: exclude from changelog
        pr_body: "An automated PR generated by ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
        github_token: ${{ secrets.GITHUB_TOKEN }}
