name: Publish

on:
  push:
    tags:
      - '*'

permissions:
  contents: write

jobs:
  build:
    name: Create release and publish escript for every new tag
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - uses: erlef/setup-beam@v1
      with:
          otp-version: '26'
    - name: Compile
      run: ./bootstrap
    - name: CT tests
      run: ./rebar3 ct

    - name: Create Release
      id: create_release
      uses: actions/create-release@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        tag_name: ${{ github.ref }}
        release_name: ${{ github.ref }}
        draft: false
        prerelease: false

    - name: Upload Release Asset
      id: upload-release-asset
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }}
        asset_path: ./rebar3
        asset_name: rebar3
        asset_content_type: application/octet-stream

    - name: Configure AWS credentials
      if: "!github.event.release.prerelease"
      uses: aws-actions/configure-aws-credentials@v1
      with:
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        aws-region: us-east-1

    - name: Copy rebar3 escript to S3
      if: "!github.event.release.prerelease"
      run: |
        aws s3 cp ./rebar3 s3://rebar3
