![](/img/trans.png)
[英]Share workflow artifacts between workflow runs / github actions using the API
[英]Github actions: How to cache dependencies between workflow runs of different branches?
我有一個 CI,它在創建的每個拉取請求和每次推送新提交時運行。 此 CI 安裝 Python 依賴項,然后運行一些測試。 我使用了兩個單獨的 requirements.txt 文件,因為其中一個包含較重的包,並且它們在 Docker 中的處理方式不同。 我正在嘗試使用actions/cache@v2
操作來緩存依賴項,但據我所知,它僅在同一分支中的運行之間緩存。 因此,例如,當我創建一個新 PR 時,沒有從另一個分支檢測到緩存,並且所有內容都是從頭開始安裝的。 有沒有辦法跨工作流運行緩存依賴項? 那么如果需求中沒有任何變化,那么 CI 在一個分支中創建的緩存可以被另一個分支使用嗎?
查看在兩個不同分支中運行的工作流的日志,緩存鍵是相同的:
branchA
Cache not found for input keys: /opt/hostedtoolcache/Python/3.8.12/x64-03a86b868f006751e123da18168c989ab4c3c2713de4f5c87cf732ffbb6fb4ae-cd1b416332d9d5b55f413e2bd74c2efce6107aef1ce3f497fa5a81b9abc83deb
branchB
Cache not found for input keys: /opt/hostedtoolcache/Python/3.8.12/x64-03a86b868f006751e123da18168c989ab4c3c2713de4f5c87cf732ffbb6fb4ae-cd1b416332d9d5b55f413e2bd74c2efce6107aef1ce3f497fa5a81b9abc83deb
name: ci
on:
pull_request:
types: [opened, synchronize]
branches-ignore:
- "master"
- "staging"
push:
branches-ignore:
- "master"
- "staging"
jobs:
run-tests:
name: Run tests
runs-on: ubuntu-latest
steps:
- name: Set up Python 3.8
uses: actions/setup-python@v2
with:
python-version: 3.8
- name: Checkout repository
uses: actions/checkout@v2
- name: Cache dependencies
uses: actions/cache@v2
with:
path: ${{ env.pythonLocation }}
key: ${{ env.pythonLocation }}-${{ hashFiles('requirements.txt') }}-${{ hashFiles('requirements-ml.txt') }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install -r requirements.txt
python -m pip install -r requirements-ml.txt
我的問題的解決方案非常愚蠢但有效。 我為每個將更新緩存的提交推送(如果需要)為暫存分支創建了一個特定的工作流。 這很有幫助,因為所有開發分支都是從暫存分支創建的,並且基礎分支中可用的每個緩存也可用於派生分支。 所以最后,解決方案就像原始帖子一樣,但事件已更新。
來自 github 的文檔:
工作流可以訪問和恢復在當前分支、基礎分支(包括分叉存儲庫的基礎分支)或默認分支(通常是主分支)中創建的緩存。 例如,可以從任何拉取請求訪問在默認分支上創建的緩存。 此外,如果分支功能-b 具有基本分支功能-a,則在功能-b 上觸發的工作流將有權訪問在默認分支(主)、功能-a 和功能-b 中創建的緩存。
name: update-cache
on:
push:
branches:
- "staging"
jobs:
# This workflow builds the Python package dependencies every time that the requirements
# files are modified and store it in cache to be accessible by all the CI in all other
# branches.
build-cache:
name: Build Cache
runs-on: ubuntu-latest
steps:
- name: Set up Python 3.8
uses: actions/setup-python@v2
with:
python-version: 3.8
- name: Checkout repository
uses: actions/checkout@v2
- name: Cache dependencies
uses: actions/cache@v2
id: cache
with:
path: ${{ env.pythonLocation }}
key: ${{ env.pythonLocation }}-${{ hashFiles('requirements.txt') }}-${{ hashFiles('requirements-ml.txt') }}
restore-keys:
${{ env.pythonLocation }}-
- if: steps.cache.outputs.cache-hit != 'true'
name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install -r requirements.txt
python -m pip install -r requirements-ml.txt
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.