name: release
on: [ push ]
jobs:
  release-to-github:
    name: Release to GitHub
    runs-on: ubuntu-latest
    if: contains(github.ref, 'main')

    steps:
      - name: Checkout repository
        uses: actions/checkout@v2
        with:
          ref: main

      - name: Check if version has changed
        uses: EndBug/version-check@v1
        id: check

      - name: Version update detected
        if: steps.check.outputs.changed == 'true'
        run: 'echo "Version change found! New version: ${{ steps.check.outputs.version }} (${{ steps.check.outputs.type }})"'

      - name: Install dependencies
        if: steps.check.outputs.changed == 'true'
        run: npm install

      - name: Build plugin zip
        if: steps.check.outputs.changed == 'true'
        run: npm run create-zip

      - name: Create release
        if: steps.check.outputs.changed == 'true'
        uses: actions/create-release@v1
        id: create_release
        with:
          draft: false
          prerelease: false
          release_name: ${{ steps.check.outputs.version }}
          tag_name: ${{ github.ref }}
          body_path: CHANGELOG.md
        env:
          GITHUB_TOKEN: ${{ github.token }}

      - name: Upload artifact
        if: steps.check.outputs.changed == 'true'
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ github.token }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./cognito-forms-${{ steps.check.outputs.version }}.zip
          asset_name: cognito-forms-${{ steps.check.outputs.version }}.zip
          asset_content_type: application/zip
