Answers: <h1>Workarounds</h1>
  <p>Because we don't (yet) have an API to download the last artifact produced by a successful job by name there are two options here to work around this limitation,</p>
  <ol>
  <li><p>Restructure your CI: the end-points assume that <em>only</em> successful pipelines produce artifacts that are saved. You're fighting an uphill battle with a pipeline that can fail, but succeed in producing something you want.</p>
  </li>
  <li><p>Crawl the jobs for the project, something like this</p>
  <pre><code>curl &quot;https://gitlab.acme.net/api/v4/projects/1571/jobs?access_token=TOKEN&quot;
    | jq '.[] | select(.status==&quot;success&quot; and .name==&quot;build&quot;)'
  </code></pre>
  </li>
  </ol>
  
Answers: CC BY-SA 4.0
Answers: 2024-04-29T20:32:47.503Z
Answers: 2024-04-29T20:32:47.503Z
Answers: Evan Carroll
Body: <p>I have a job which is maintained by a team, let's call it docs. It runs in a pipeline with jobs maintained by other teams. If the docs team job succeeds, I want to download artifact <strong>regardless of the other jobs which impact the pipeline status</strong>. An example of this is as follows,</p>
  <pre class="lang-sh prettyprint-override"><code>#!/bin/sh
  mkdir foo;
  cd foo;
  git init;
  glab repo create;
  
  GLCI=.gitlab-ci.yml
  cat &gt; &quot;$GLCI&quot; &lt;&lt;'EOF'
  image: alpine:latest
  stages:
    - build
  
  fail:
    stage: build
    script:
      - exit 42 # failure
  
  build:
    stage: build
    script:
      - mkdir dist
      - echo &quot;0&quot; &gt; dist/bar.txt
    artifacts:
      expire_in: 5 seconds
      paths:
        - dist/*
  
  EOF
  
  #commit ci
  git add &quot;$GLCI&quot;;
  git commit -m 'Add gitlab ci';
  git branch --set-upstream-to=main
  git push --set-upstream -o ci.skip origin main;
  
  echo bump &gt; test.txt
  git add test.txt
  git commit -a -m'bump';
  git push;
  </code></pre>
  <p>In the above we have two jobs in a pipeline, <code>build</code> (which passes) and <code>fail</code> (which fails). I want to download the last <code>build</code> job's artifact factoring in nothing but the <em>job status</em>.</p>
  <p><a href="https://i.sstatic.net/pzPsKw9f.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/pzPsKw9f.png" alt="Job status" /></a></p>
  <p>The end point, <a href="https://docs.gitlab.com/ee/api/job_artifacts.html#download-a-single-artifact-file-from-specific-tag-or-branch" rel="nofollow noreferrer"><strong>Download a single artifact file from specific tag or branch</strong></a> in the <a href="https://docs.gitlab.com/ee/api/job_artifacts.html" rel="nofollow noreferrer">Artifacts API</a> almost does what I want, but it only triggers on successful pipelines,</p>
  <blockquote>
  <p>Download a single artifact file for a specific job of the <strong>latest successful pipeline</strong> for the given reference name from inside the job’s artifacts archive. The file is extracted from the archive and streamed to the client, with the plain/text content type.</p>
  </blockquote>
  <p>How can I work around this limitation? How can this be done?</p>
  
Tags: <gitlab><gitlab-ci>
Title: How can I download the latest artifact by name, regardless of pipeline status?
UserDisplayName: Evan Carroll
ThreadText: # How can I download the latest artifact by name, regardless of pipeline status?
  
  (Asked by: username_0 on 2024-04-29)
  
  I have a job which is maintained by a team, let's call it docs. It runs in a pipeline with jobs maintained by other teams. If the docs team job succeeds, I want to download artifact **regardless of the other jobs which impact the pipeline status**. An example of this is as follows,
  
  ```sh
  #!/bin/sh
  mkdir foo;
  cd foo;
  git init;
  glab repo create;
  
  GLCI=.gitlab-ci.yml
  cat > "$GLCI" <<'EOF'
  image: alpine:latest
  stages:
    - build
  
  fail:
    stage: build
    script:
      - exit 42 # failure
  
  build:
    stage: build
    script:
      - mkdir dist
      - echo "0" > dist/bar.txt
    artifacts:
      expire_in: 5 seconds
      paths:
        - dist/*
  
  EOF
  
  #commit ci
  git add "$GLCI";
  git commit -m 'Add gitlab ci';
  git branch --set-upstream-to=main
  git push --set-upstream -o ci.skip origin main;
  
  echo bump > test.txt
  git add test.txt
  git commit -a -m'bump';
  git push;
  
  ```
  
  In the above we have two jobs in a pipeline, `build` (which passes) and `fail` (which fails). I want to download the last `build` job's artifact factoring in nothing but the *job status*.
  
  [![Job status](https://i.sstatic.net/pzPsKw9f.png)](https://i.sstatic.net/pzPsKw9f.png)
  
  The end point, [**Download a single artifact file from specific tag or branch**](https://docs.gitlab.com/ee/api/job_artifacts.html#download-a-single-artifact-file-from-specific-tag-or-branch) in the [Artifacts API](https://docs.gitlab.com/ee/api/job_artifacts.html) almost does what I want, but it only triggers on successful pipelines,
  
  > Download a single artifact file for a specific job of the **latest successful pipeline** for the given reference name from inside the job’s artifacts archive. The file is extracted from the archive and streamed to the client, with the plain/text content type.
  
  How can I work around this limitation? How can this be done?
  
  **Tags:** gitlab, gitlab-ci
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2024-04-29. Score: 0 | Accepted answer
  
  # Workarounds
  
  Because we don't (yet) have an API to download the last artifact produced by a successful job by name there are two options here to work around this limitation,
  
  1. Restructure your CI: the end-points assume that *only* successful pipelines produce artifacts that are saved. You're fighting an uphill battle with a pipeline that can fail, but succeed in producing something you want.
  2. Crawl the jobs for the project, something like this
  
     ```
     curl "https://gitlab.acme.net/api/v4/projects/1571/jobs?access_token=TOKEN"
       | jq '.[] | select(.status=="success" and .name=="build")'
  
     ```
  
  
  ---
  
  