【问题标题】:Github Action Cache question with containers带有容器的 Github Action Cache 问题
【发布时间】:2021-03-16 22:50:40
【问题描述】:

我有一个简短的问题,但我找不到关于它的文档(也许不可能) 如何检索从容器中构建的内容(像这样):

prod-dependencies:
name: Production Dependencies
runs-on: ubuntu-20.04
container: elixir:1.11-alpine
env:
MIX_ENV: prod
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Retrieve Cached Dependencies
uses: actions/cache@v2
id: mix-prod-cache
with:
path: |
${{ github.workspace }}/deps
${{ github.workspace }}/_build
key: PROD-${{ hashFiles('mix.lock') }}
- name: Install Dependencies
if: steps.mix-prod-cache.outputs.cache-hit != 'true'
run: |
apk add build-base rust cargo
mix do local.hex --force, local.rebar --force, deps.get --only prod, deps.compile

我需要在 Alpine 上构建,因为我在 Alpine 上进行部署,并且某些 C 库对于 Erlang VM 的工作是不同的。

所以这里是我构建依赖项的地方,我想将它们放在缓存中以供后续作业使用,但缓存永远不会填充。根据 doc GitHub 安装一个带有容器的卷来检索工件,但我必须错过使用它。

非常感谢您的帮助。

【问题讨论】:

  • 请记住,Github 操作/缓存的范围仅限于特定的 分支(有一些注意事项,请注意回退到 repo 的默认分支上的缓存)。因此,如果这些作业是在不同的分支上创建的,它们可能无法用于后续作业。有关在 S3 中缓存工件以供其他分支使用的示例,请参阅 elixirforum.com/t/using-github-action-cache/37922/4。如果您的 YAML 保留正确的格式,这将有助于提高可读性。
  • 感谢您的回答。我确认这些将在同一个分支上使用。主要是为了减少构建时间。

标签: caching elixir github-actions


【解决方案1】:

这是一个 Elixir 应用程序的示例 Github 工作流程,该应用程序具有一个使用 action/cache@v2 的 Postgres 数据库。请注意,它定义了两个单独的步骤来恢复deps_build 目录。在此工作流程中,mix deps.getmix compile 步骤始终运行:如果缓存恢复,它们应该会非常快速地执行。

改编自this workflow

name: Test

on:
  pull_request:
    branches:
      - develop
    paths-ignore:
      - 'docs/**'
      - '*.md'

jobs:
  test:

    name: Lint and test

    runs-on: ubuntu-18.04

    env:
      MIX_ENV: test

    services:
      db:
        image: postgres:11
        ports: ['5432:5432']
        env:
          POSTGRES_PASSWORD: postgres
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    steps:
      - uses: actions/checkout@v2
      - uses: erlef/setup-elixir@v1
        with:
          otp-version: '23.1.2'
          elixir-version: '1.11.2'

      - name: Configure SSH for private Repos
        uses: webfactory/ssh-agent@v0.4.1
        with:
          ssh-private-key: ${{ secrets.HEADLESS_PRIV }}

      - name: Restore the deps cache
        uses: actions/cache@v2
        id: deps-cache-restore
        with:
          path: deps
          key: ${{ runner.os }}-deps-mixlockhash-${{ hashFiles(format('{0}{1}', github.workspace, '/mix.lock')) }}
          restore-keys: |
            ${{ runner.os }}-deps-

      - name: Restore the _build cache
        uses: actions/cache@v2
        id: build-cache-restore
        with:
          path: _build
          key: ${{ runner.os }}-build-mixlockhash-${{ hashFiles(format('{0}{1}', github.workspace, '/mix.lock')) }}
          restore-keys: |
            ${{ runner.os }}-build-

      - name: Get/update Mix dependencies
        run: |
          mix local.hex --force
          mix local.rebar
          mix deps.get

      - name: Compile
        run: mix compile

      - name: Create database
        run: mix ecto.create

      - name: Migrate database
        run: mix ecto.migrate

      - name: Test
        run: mix test

【讨论】:

  • 是的,我在 ubuntu 上为 UT 执行此操作,但我的问题是您在容器中构建。我无法缓存,因为它可能没有将卷安装到底层机器?我想在 Alpine 上构建以部署 Alpine 容器。
  • 基本上问题来自运行:ubuntu-20.04 容器:elixir:1.11-alpine --
猜你喜欢
  • 2020-11-11
  • 2021-06-05
  • 2022-11-11
  • 2020-03-05
  • 2020-05-29
  • 1970-01-01
  • 2021-08-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多