Using Github Actions, how can I build my assets ready to be deployed?

Viewed 606

I have a Laravel app, everything standard, and as part of my deployment process I need to run npm run production to build assets. I want this as part of my CI/CD, for which I'm using GitHub Actions.

My Actions YML is as follows:

name: Laravel

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  laravel-tests:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Copy .env
      run: php -r "file_exists('.env') || copy('.env.example', '.env');"
    - name: Install Dependencies
      run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
    - name: Generate key
      run: php artisan key:generate
    - name: Directory Permissions
      run: chmod -R 777 storage bootstrap/cache
    - name: Create Database
      run: |
        mkdir -p database
        touch database/database.sqlite
    - name: Execute tests (Unit and Feature tests) via PHPUnit
      env:
        DB_CONNECTION: sqlite
        DB_DATABASE: database/database.sqlite
      run: vendor/bin/phpunit    
    - uses: actions/setup-node@v1
      with:
        node-version: '10.x'   
    - name: Compile assets
      run: |
        npm install
        npm run production
    - name: Deploy to Laravel Forge
      run: curl ${{ secrets.FORGE_DEPLOYMENT_WEBHOOK }}

This runs fine. However I believe the assets are being compiled in the virtual machine. Once everything has passed I hit the deployment hook for Laravel Forge and my project is deployed. Deployment happens by Forge, it SSH's into the box, git pulls, runs artisan commands. The production compiled assets aren't in the repo, they're stuck in the VM, and disappear once run.

How can I get the assets that are built on the GitHub Actions VM into the Forge deployment? The only way I can think of is to add them via git inside the VM, but that would trigger another run of Actions, so that's not the best method.

1 Answers

You can push from your action without triggering another action, if you are using the default GITHUB_TOKEN.

When you use the repository's GITHUB_TOKEN to perform tasks on behalf of the GitHub Actions app, events triggered by the GITHUB_TOKEN will not create a new workflow run. This prevents you from accidentally creating recursive workflow runs. For example, if a workflow run pushes code using the repository's GITHUB_TOKEN, a new workflow will not run even when the repository contains a workflow configured to run when push events occur.

(source: https://help.github.com/en/actions/reference/events-that-trigger-workflows#triggering-new-workflows-using-a-personal-access-token)

Related