actions / upload-artifact

MIT License
3.26k stars 739 forks source link

[feat req] add option to overwrite existing artifacts #471

Open sni opened 11 months ago

sni commented 11 months ago

What would you like to be added?

It would be nice to have a option to allow overwrite existing artifacts like it was possible in v3.

Why is this needed?

I have an optional workflow step to sign artifacts, it downloads an artifact, signs it and uploads it with the same name again. Worked perfectly fine with actions/upload-artifact@v3. I tried to use geekyeggo/delete-artifact@v2 but even that doesn't work. Since this step is not applicable for pull requests, it would make things quite complicated to continue with separate artifact names.

robherley commented 11 months ago

👋 If you need to delete an artifact, you can do it with @actions/github-script.

Here's a full example:

name: Delete Artifact Example
on:
  workflow_dispatch:

permissions:
  actions: write # required permission to delete artifact

jobs:
  upload-and-delete:
    runs-on: ubuntu-latest
    steps:
    - name: Create a File
      run: echo "hello world" > hello.txt
    - name: Upload Artifact
      id: artifact-upload
      uses: actions/upload-artifact@v4
      with:
        name: my-artifact
        path: hello.txt
    - name: Delete Artifact
      uses: actions/github-script@v7
      with:
        script: |
          github.rest.actions.deleteArtifact({
            owner: context.repo.owner,
            repo: context.repo.repo,
            artifact_id: ${{ steps.artifact-upload.outputs.artifact-id }}
          });
    - name: Upload Artifact (again)
      uses: actions/upload-artifact@v4
      with:
        name: my-artifact
        path: hello.txt

Make sure you have the correct permissions set for the GITHUB_TOKEN, as noted above:

permissions:
  actions: write

I'm not sure if we're going to have an official "overwrite" at this time, since artifacts become immediately available in the public API in v4, unlike in v3 where you had to wait until the end of the run. But thanks for the feedback and I'll bring it up with the team!

sni commented 11 months ago

thanks for the workaround. For now, i just keep using v3 while hoping there will be a option to overwrite again in the future. If there are any implications, you could mention this in the docs and leave the decision to the user?

onedr0p commented 11 months ago

I would like to see this feature added too, I saw the workaround but I will keep using v3 until this is addressed as well.

zdgeorgiev commented 11 months ago

@robherley I guess that will not work in matrix

robherley commented 11 months ago

@zdgeorgiev It still works fine in a matrix:


name: Delete Artifact Matrix Example
on:
  workflow_dispatch:

permissions:
  actions: write # required permission to delete artifact

jobs:
  upload-and-delete:
    strategy:
      matrix:
        example: ['foo', 'bar']
    runs-on: ubuntu-latest
    steps:
    - name: Create a File
      run: echo "hello world" > hello.txt
    - name: Upload Artifact
      id: artifact-upload
      uses: actions/upload-artifact@v4
      with:
        name: my-artifact-${{ matrix.example }}
        path: hello.txt
    - name: Delete Artifact
      uses: actions/github-script@v7
      with:
        script: |
          github.rest.actions.deleteArtifact({
            owner: context.repo.owner,
            repo: context.repo.repo,
            artifact_id: ${{ steps.artifact-upload.outputs.artifact-id }}
          });
    - name: Upload Artifact (again)
      uses: actions/upload-artifact@v4
      with:
        name: my-artifact-${{ matrix.example }}
        path: hello.txt

You can even use the list artifacts API in octokit to list/filter/reduce on the artifact names and pass them to delete artifact.

Borda commented 11 months ago

I'm not sure if we're going to have an official "overwrite" at this time

TBH, that would be great as you would have easier interaction among jobs, not when a simple way to pass artifact-id from jon foo to job bar so this with aggregating artifacts may not work :(

onedr0p commented 11 months ago

I'm not sure if we're going to have an official "overwrite" at this time

@robherley that's unfortunate, I hope by looking at the number of issues/pr linked that will change. It seems like most are downgrading due to this not being a feature in v4.

robherley commented 11 months ago

@onedr0p 👋 I agree. Going to bring this back to the team considering how likely it'll be required. Thanks for the feedback!

sni commented 10 months ago

@robherley: were you able to address the issue with the team?

robherley commented 10 months ago

👋 @sni If all goes well, we should have this ready by tomorrow! I have a toolkit PR open to delete artifacts, once that is merged and published I'll add an input to overwrite which will delete the previous artifact before uploading the next

robherley commented 10 months ago

👋 This is now possible! Check out the example in the readme: https://github.com/actions/upload-artifact#overwriting-an-artifact

yury-s commented 10 months ago

Is this rolled out yet? we are seeing the following error on this workflow:

Run actions/upload-artifact@v4
  with:
    name: pull-request-number
    path: pull_request_number.txt
    overwrite: true
    if-no-files-found: warn
    compression-level: 6
  env:
    FORCE_COLOR: 1
    FLAKINESS_CONNECTION_STRING: 
    ELECTRON_SKIP_BINARY_DOWNLOAD: 1
    PWTEST_BOT_NAME: chromium-ubuntu-22.04-node20
With the provided path, there will be 1 file uploaded
Artifact name is valid!
Root directory input is valid!
Error: Failed to CreateArtifact: Received non-retryable error: Failed request: (409) Conflict: an artifact with this name already exists on the workflow run
melloware commented 10 months ago

Me too seeing this issue in v4. Downgrading to v3 fixes it for me.

harry-s-grewal commented 9 months ago

👋 This is now possible! Check out the example in the readme: https://github.com/actions/upload-artifact#overwriting-an-artifact

This worked for me! Thanks for adding this

SimonCockx commented 7 months ago

Is this rolled out yet? we are seeing the following error on this workflow:

I'm having the same issue. Even with overwrite: true I'm seeing the same error.

bryan-bar commented 1 month ago

Is this rolled out yet? we are seeing the following error on this workflow:

I'm having the same issue. Even with overwrite: true I'm seeing the same error.

This only occurs for me during retries on the second upload. The initial run of workflow is able to upload and then overwrite and the first upload during a job retry works but then fails during the second upload.

I also made sure to set the permission actions: write and it says that the file is being deleted but it gives me the same error. One thing I noticed with the example is that it uses 2 separate jobs while I am trying to upload twice in the same job.

With the provided path, there will be 1 file uploaded
Artifact '<REDACTED_ARTIFACT_NAME>' (ID: 2055953587) deleted
Artifact name is valid!
Root directory input is valid!
Error: Failed to CreateArtifact: Received non-retryable error: Failed request: (409) Conflict: an artifact with this name already exists on the workflow run

Update: After looking at the logs, I notice that during retry it tries to delete the old artifact id:


The issue was that I did not have overwrite: true for the initial upload action. Adding that fixed my issue.