name: ci-static

on:
  push:
    tags:
      - v*

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - name: Checkout
      uses: actions/checkout@v3

    - name: Setup PHP
      uses: shivammathur/setup-php@v2
      with:
        php-version: '7.4'
        extensions: zip

    - name: Setup Node
      uses: actions/setup-node@v3
      with:
        node-version: 14

    - name: Setup Git and SSH access
      env:
        SSH_AUTH_SOCK: /tmp/ssh_agent.sock
      run: |
        git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
        git config --global user.name "github-actions[bot]"
        ssh-agent -a $SSH_AUTH_SOCK > /dev/null
        ssh-add - <<< "${{ secrets.STATIC_SSH_PRIVATE_KEY }}"

    - name: Build and upload to static
      env:
        SSH_AUTH_SOCK: /tmp/ssh_agent.sock
      run: |
        cp configuration_sample.php configuration.php
        chmod -R u+w repository/
        chmod -R u+w private/
        composer install --no-dev
        npm install
        npm run build
        rm -Rf node_modules
        rm -Rf frontend
        rm -Rf tests
        rm -Rf docs
        rm -Rf .git
        rm -Rf .github
        rm README.md couscous.yml repository/.gitignore babel.config.js cypress* .env* .eslint* .gitignore jest.* .php_cs* phpunit* postcss* vue*
        cd ..
        zip -r filegator_${{ github.ref_name }}.zip ${{ github.event.repository.name }}
        git clone git@github.com:filegator/static.git
        cp filegator_${{ github.ref_name }}.zip static/builds/filegator_${{ github.ref_name }}.zip
        rm static/builds/filegator_latest.zip
        cp filegator_${{ github.ref_name }}.zip static/builds/filegator_latest.zip
        cd static
        git add .
        git commit -m "releasing static files for ${{ github.ref_name }}"
        git push

