Pof
Pof

Reputation: 979

Using Github actions cache to avoid installing composer vendor

I have a project which is using GitHub actions to build and deploy code. This project is a Wordpress theme based on sage (it does'n matter, but FIY). This project is using node_modules and composer vendors.

My deployment workflow is composed of 3 jobs :

I'm getting /public folder on the 3rd step because it's an artifact, but I'm struggling deploying /vendor folder, because it doesn't exist on the 3rd job. I could use artifacts to send /vendor from 2nd to 3rd job, but it's a bit slow.

Do you know if there is a way to use cache to get /vendor folder on the 3rd step ? Do I need to re-run composer install on the last step in order to get this /vendor folder ? If so, my 2nd step is a bit useless isn't it ?

Thank you for your help !

Upvotes: 0

Views: 2720

Answers (1)

Ed Kloczko
Ed Kloczko

Reputation: 528

Caches can be used across multiple jobs as long as they're in the same workflow - you won't need to run composer install in both steps. Artifacts are only needed if you need to use the folder/files outside of your workflow.

As long as the first job will always run, you can use the same cache key for both steps and make the assumption that the cache will always exist during the third job.

Something like this would work for you. In a "normal" run (the workflow hasn't already been run for this github.sha), the first cache step will miss and composer install will run. Once job-two is finished, everything in /vendor will be cached with github.sha as the cache key. job-three will pick that /vendor folder up from the cache so you can use it in the rest of the steps.

jobs:
  job-two:
    steps:
    - name: Cache vendor folder
      uses: actions/cache@v3
      with:
        path: /vendor
        key: ${{ github.sha }}

    - name: Build vendor folder
      run: composer install

  job-three:
    steps:
    - name: Retrieve cached vendor folder
      uses: actions/cache@v3
      with:
        path: /vendor
        key: ${{ github.sha }}

Hard to get it perfect without seeing your workflow file - you might have to tweak some of the paths.

Upvotes: 6

Related Questions