Github Actions:仅保留每个分支上最新版本的工件

问题描述 投票:0回答:1

我正在使用 Github Actions 构建我的项目,并通过以下工作流程部分将工件上传到 Github:

- uses: actions/upload-artifact@v2
  with:
    name: some-file
    path: some-path

问题是工件非常大并且很快就会消耗可用的存储配额。而且我什至不需要所有这些,只需要每个分支上最新版本的那些。

设置保留期不是一个解决方案,因为它还会从最新版本中删除工件。

github github-actions
1个回答
7
投票

您可以为此使用 Artifacts API

首先,您应该获得所有工件的列表:

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.rest.actions.listArtifactsForRepo({
              owner,
              repo,
            })
            
            res.data.artifacts
              .filter(({ name }) => name === '${{ steps.artifact-name.outputs.value }}')
              .forEach(({ id }) => {
                github.rest.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
© www.soinside.com 2019 - 2024. All rights reserved.