on:
  push:
    tags:
      - v*

jobs:
  push-to-remote:
  # We want to use the macos runner provided by github actions. This requires to push to a remote first.
  # After the push we can use the action under frontend/.github/actions/ to deploy properly using fastlane on macos.
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          path: 'src'

      - name: Checkout remote repository
        uses: actions/checkout@v3
        with:
          path: 'dest'
          ref: 'main'
          github-server-url: 'https://github.com'
          repository: 'moll-re/anyway-frontend-builder'
          token: ${{ secrets.PUSH_GITHUB_API_TOKEN }}
          fetch-depth: 0
          persist-credentials: true

      - name: Copy files to remote repository
        run: cp -r src/frontend/. dest/

      - name: Commit and push changes
        run: |
          cd dest
          git config --global user.email "me@moll.re"
          git config --global user.name "[bot]"
          git add .
          git commit -m "Automatic code update for tag"
          git tag -a ${{ github.ref_name }} -m "mirrored tag"
          git push origin main --tags