我正在使用Github Actions构建我的项目并使用以下工作流程上传工件到Github:
- uses: actions/upload-artifact@v2
with:
name: some-file
path: some-path
问题在于这些构件非常大,很快就会消耗可用存储空间。而且我甚至不需要它们所有,只需要每个分支上最新构建的那些。
设置保留期并不是解决方案,因为它也会删除最新构建的构件。
我正在使用Github Actions构建我的项目并使用以下工作流程上传工件到Github:
- uses: actions/upload-artifact@v2
with:
name: some-file
path: some-path
问题在于这些构件非常大,很快就会消耗可用存储空间。而且我甚至不需要它们所有,只需要每个分支上最新构建的那些。
设置保留期并不是解决方案,因为它也会删除最新构建的构件。
GET /repos/{owner}/{repo}/actions/artifacts
在响应对象中没有有关分支名称的信息,因此需要调整工件名称以包括分支名称。
然后通过筛选获取旧的工件并将其删除:
DELETE /repos/{owner}/{repo}/actions/artifacts/{artifact_id}
在上传新文件之前,建议先运行此步骤。但使用何种策略取决于您自己。
以下是使用octokit的简单示例:
jobs:
doit:
runs-on: ubuntu-latest
steps:
- name: Create some file to upload
run: echo "test" > test.txt
- name: Store artifact's name
id: artifact-name
env:
REF: ${{ github.ref }}
run: echo "::set-output name=value::${REF////-}-test"
- name: List, filter and delete artifacts
uses: actions/github-script@v4
id: artifact
with:
script: |
const { owner, repo } = context.issue
const res = await github.actions.listArtifactsForRepo({
owner,
repo,
})
res.data.artifacts
.filter(({ name }) => name === '${{ steps.artifact-name.outputs.value }}')
.forEach(({ id }) => {
github.actions.deleteArtifact({
owner,
repo,
artifact_id: id,
})
})
- name: Upload latest artifact
uses: actions/upload-artifact@v2
with:
name: ${{ steps.artifact-name.outputs.value }}
path: test.txt
GITHUB_TOKEN
权限可能不足以删除其他工作流运行中的构件;生成个人访问令牌并将其提供给actions/github-script
(通过github-token
输入)可以解决这个问题。 - Yang Hanlin