on:
    push:
        # Sequence of patterns matched against refs/tags
        tags:
            - '*.*' # match any tag

name: Upload Release Asset

jobs:
    build:
        name: Upload Release Asset
        runs-on: ubuntu-latest
        steps:
            - name: Checkout code
              uses: actions/checkout@v2
            - name: Get the version
              id: get_version
              run: echo ::set-output name=VERSION::${GITHUB_REF/refs\/tags\//}
            - name: Build project # This would build the project, and then creating build zip file
              run: |
                  # Install composer dependencies
                  composer install
                  # Install npm dependencies and move them into places
                  yarn
                  #yarn audit
                  npm run dev
                  #remove unwanted files & folders
                  rm -rf .git && rm -rf node_modules && rm -rf .github && rm -f .gitlab-ci.yml
                  # create a folder with version name and move all files into it so that it will be extracted into its own folder
                  shopt -s extglob dotglob
                  mkdir starter-${{ steps.get_version.outputs.VERSION }}
                  mv ./!(starter-${{ steps.get_version.outputs.VERSION }}) starter-${{ steps.get_version.outputs.VERSION }}
                  shopt -u dotglob
                  #just to make sure if any hidden files are copied or not
                  #ls -a
                  # Create final zip to download
                  zip -r starter-${{ steps.get_version.outputs.VERSION }}.zip starter-${{ steps.get_version.outputs.VERSION }}/ -x * .git/\ node_modules/\ *
            - name: Create Release
              id: create_release
              uses: actions/create-release@v1
              env:
                  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
              with:
                  tag_name: ${{ github.ref }}
                  release_name: Release ${{ 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 }} # This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`. See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps
                  asset_path: ./starter-${{ steps.get_version.outputs.VERSION }}.zip
                  asset_name: starter-${{ steps.get_version.outputs.VERSION }}.zip
                  asset_content_type: application/zip
