name: Build image

on:
  push:
    branches: [ '*' ]
  pull_request:
    branches: [ master ]
  release:
    types: [ created ]

jobs:
  build:
    runs-on: macos-12 # Using macOS as https://github.com/actions/virtual-environments/issues/183
    steps:
      - uses: actions/checkout@v2
      - name: Download base image
        run: |
          mkdir output-virtualbox-iso
          wget --progress=dot:giga https://clovervm.ams3.digitaloceanspaces.com/base-focal.ova -O output-virtualbox-iso/clover-devel.ova
      - name: Build
        run: ./build.sh
      - name: Image size
        run: ls -lh output-virtualbox-ovf
      - name: Upload
        if: ${{ github.event_name == 'release' }}
        # Not using ready S3 actions, because they either don't run on macOS machines or don't support changing the endpoint
        run: |
          AWS_ACCESS_KEY_ID=${{secrets.S3_KEY}} AWS_SECRET_ACCESS_KEY=${{secrets.S3_SECRET_KEY}} aws s3 sync . s3://clovervm/ --acl public-read --endpoint-url https://ams3.digitaloceanspaces.com --no-progress
          echo Uploaded https://clovervm.ams3.digitaloceanspaces.com/$(ls)
        working-directory: output-virtualbox-ovf
