Laravel Github工作流程操作 – 缓存composer依赖项,未按预期工作

huangapple go评论61阅读模式
英文:

Laravel Github Workflow Action - Cache composer dependencies, not working as intended

问题

这是我的.github/workflow/staging.yml设置

name: 部署到暂存环境

on:
  push:
    branches:
      - staging

jobs:
  部署到暂存环境:
    runs-on: ubuntu-latest
    steps:
      - name: 检出代码
        uses: actions/checkout@v2

      - name: 缓存 Composer 依赖项
        uses: actions/cache@v2
        with:
          path: vendor
          key: composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: |
                        composer-

      - name: 设置 PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: 8.2
          tools: composer:v2
          coverage: none

      - name: 安装项目依赖项
        working-directory: ./application
        run: composer install --no-dev --no-interaction --prefer-dist --optimize-autoloader --no-dev

      ... 剩余的步骤

我的 Laravel 代码位于名为 application 的文件夹中(因此使用了 working-directory 指令)

我已经使用上述更改部署了应用程序,供应商文件缓存似乎已经“预热”。然后,在后续部署中,它看起来像这样:

Laravel Github工作流程操作 – 缓存composer依赖项,未按预期工作

正如您所见,它说还原了约 0 MB 的缓存供应商,在流程的其余部分中,它再次下载并安装了 Composer 依赖项(尽管 composer.lock 文件没有更改)

我尝试更改哈希检查位置,如下所示,但也没有帮助:
> composer-${{ hashFiles('./application/composer.lock') }}

这里可能出了什么问题?

英文:

This is my .github/workflow/staging.yml setup

name: Deploy Staging

on:
  push:
    branches:
      - staging

jobs:
  deploy-staging:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Cache composer dependencies
        uses: actions/cache@v2
        with:
          path: vendor
          key: composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: |
            composer-

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: 8.2
          tools: composer:v2
          coverage: none

      - name: Install Project Dependencies
        working-directory: ./application
        run: composer install --no-dev --no-interaction --prefer-dist --optimize-autoloader --no-dev

      ... rest of the steps

My laravel code exists inside a folder called application (hence the use of working-directory directive)

I've deployed the app once with the above changes and the vendor folder cache supposedly "warmed up". Then on subsequent deployment it looked like this:

Laravel Github工作流程操作 – 缓存composer依赖项,未按预期工作

as you can see, it said restored ~0 MB of cached vendor and in the rest of the flow it downloads and installs the composer dependencies again (even though the composer.lock file hasn't changed)

I tried changing hash checking location like this and it did not help either:
> composer-${{ hashFiles('./application/composer.lock') }}

Any ideas what might be going wrong here?

答案1

得分: 1

看起来你指定的 Composer 缓存路径不正确。

在你的工作流中,在运行 composer install 之前,你将工作目录设置为 ./application,但缓存操作正在寻找根目录中的 vendor 文件夹。

你需要将缓存操作中的路径更改为 ./application/vendor,然后正确缓存你的 Composer 依赖项,以便在后续运行中使用。

这是你工作流中更新后的缓存步骤:

- name: 缓存 Composer 依赖项
  uses: actions/cache@v2
  with:
    path: ./application/vendor
    key: composer-${{ hashFiles('**/composer.lock') }}
    restore-keys: |
            composer-

请记住,hashFiles('**/composer.lock') 将对你仓库中所有包括子目录中的 composer.lock 文件进行哈希。如果你有多个 composer.lock 文件,请考虑指定你想要的确切路径。

另外,请确保将 composer.lock 文件提交并推送到你的仓库,因为如果不包括在你的仓库中,GitHub actions runner 可能找不到它。

英文:

it seems that the path you have specified for the composer cache is incorrect.

In your workflow, you are setting the working directory to ./application before running composer install, but the cache action is looking for the vendor folder in the root directory.

You need to change the path in the cache action to ./application/vendor, this should then correctly cache your composer dependencies and make them available for subsequent runs.

Here's the updated caching step in your workflow:

- name: Cache composer dependencies
  uses: actions/cache@v2
  with:
    path: ./application/vendor
    key: composer-${{ hashFiles('**/composer.lock') }}
    restore-keys: |
      composer-

Remember that hashFiles('**/composer.lock') will hash all composer.lock files in your repository including in all subdirectories. If you have multiple composer.lock files, consider specifying the exact path to the one you want.

As well as, make sure the composer.lock file is committed and pushed into your repository, as the GitHub actions runner may not find it if it's not included in your repository.

huangapple
  • 本文由 发表于 2023年6月22日 08:17:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76527896.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定