# This is a manually triggered workflow (hence on: workflow_dispatch).
# See: https://docs.github.com/en/actions/reference/events-that-trigger-workflows#manual-events

on: workflow_dispatch

jobs:
  publish-go:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: Read Tool Versions
        id: tool-versions
        run: echo "::set-output name=golang::$(sed -nr 's/golang ([0-9]+)/\1/p' .tool-versions)"

      - name: Setup Go
        uses: actions/setup-go@v2
        with:
          go-version: ${{ steps.tool-versions.outputs.golang }}

      - name: Generate code for Go users
        run: scripts/build-go.sh

      # ABLY_COMMON_GO_SSH_KEY is a deploy key, created on the ably-common-go repository.
      # see: https://docs.github.com/en/developers/overview/managing-deploy-keys#deploy-keys
      # The eval on ssh-agent creates some environment variables.
      # Those environment variables exist for the duration of this step only.
      - name: Publish
        env:
          SSH_PRIVATE_KEY: ${{ secrets.ABLY_COMMON_GO_SSH_KEY }}
        run: |
          echo "$SSH_PRIVATE_KEY" > ssh-private-key.pem
          chmod 400 ssh-private-key.pem
          eval $(ssh-agent -s)
          ssh-add ssh-private-key.pem
          git config --global user.name "Ably Common Publish Workflow [bot]"
          git config --global user.email "ably-common-publish-workflow[bot]@noreply.ably.com"
          scripts/publish-go.sh
