<h1>No workaround</h1>
<p>I don't know of a workaround for this yet. I opened up a bug on GitLab. It's tracked here.</p>
<ul>
<li><a href="https://gitlab.com/gitlab-org/gitlab/-/issues/458828" rel="nofollow noreferrer">https://gitlab.com/gitlab-org/gitlab/-/issues/458828</a></li>
</ul>
<p>Try not to use conditional jobs if you need artifacts.</p>

CC BY-SA 4.0
2024-04-29T22:02:30.653Z
2024-04-29T22:02:30.653Z
Evan Carroll
<p>Gitlab allows you to run jobs conditionally based on the files being modified using the <a href="https://docs.gitlab.com/ee/ci/yaml/#ruleschanges" rel="nofollow noreferrer"><code>rules:changes</code></a> functionality. It looks like this,</p>
<pre class="lang-yaml prettyprint-override"><code>build:
  stage: build
  script:
    - mkdir dist
    - echo &quot;0&quot; &gt; dist/bar.txt
  rules:
    - changes:
      - trigger.txt
</code></pre>
<p>Now if you add or modify <code>trigger.txt</code> the job will run, and if you do not the job will not run. There is also a tremendously useful option in the server to <a href="https://docs.gitlab.com/ee/ci/jobs/job_artifacts.html#keep-artifacts-from-most-recent-successful-jobs" rel="nofollow noreferrer">&quot;Keep artifacts from most recent successful jobs&quot;</a>. The intention here is that a job that produces artifact will never lose that artifact until a <em>more</em> recent job has completed successfully. However, that doesn't seem to happen in practice if your jobs run conditionally. If your jobs run conditionally, the newest successful pipeline will free all of your artifacts to be reaped, and if the conditional job didn't run then you're out of luck. You can see that here, run this code first.</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

success:
  stage: build
  script:
    - echo Success &amp;&amp; exit 0;

build:
  stage: build
  script:
    - mkdir dist
    - echo &quot;0&quot; &gt; dist/bar.txt
  rules:
    - changes:
      - trigger.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; trigger.txt
git add trigger.txt
git commit -a -m'trigger';
git push;

echo Check artifacts
</code></pre>
<p>This satisfies the <code>rules:changes</code> and at this point you can see the artifact available,</p>
<p><a href="https://i.sstatic.net/A2pC77x8.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/A2pC77x8.png" alt="This is the UI to save the artifact" /></a></p>
<p>However, when you push up the code the <code>rules:changes</code> fails, and the job does not run but the pipeline succeeds,</p>
<pre class="lang-shell prettyprint-override"><code>echo bump &gt; notrigger.txt
git add notrigger.txt
git commit -a -m'no trigger';
git push;
</code></pre>
<p>This will result in your artifacts being destroyed.</p>
<p><a href="https://i.sstatic.net/GkTC2cQE.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/GkTC2cQE.png" alt="Artifacts destroyed." /></a></p>
<p>Is there a workaround so a conditional job too can preserve its last artifact?</p>

<gitlab><gitlab-ci>
How do you preserve conditionally created artifacts (artifacts created with changes)?
Evan Carroll
# How do you preserve conditionally created artifacts (artifacts created with changes)?

(Asked by: username_0 on 2024-04-29)

Gitlab allows you to run jobs conditionally based on the files being modified using the rules:changes functionality. It looks like this,
build:
  stage: build
  script:
    - mkdir dist
    - echo "0" > dist/bar.txt
  rules:
    - changes:
      - trigger.txt

Now if you add or modify trigger.txt the job will run, and if you do not the job will not run. There is also a tremendously useful option in the server to "Keep artifacts from most recent successful jobs". The intention here is that a job that produces artifact will never lose that artifact until a more recent job has completed successfully. However, that doesn't seem to happen in practice if your jobs run conditionally. If your jobs run conditionally, the newest successful pipeline will free all of your artifacts to be reaped, and if the conditional job didn't run then you're out of luck. You can see that here, run this code first.
#!/bin/sh
mkdir foo;
cd foo;
git init;
glab repo create;

GLCI=.gitlab-ci.yml
cat > "$GLCI" <<'EOF'
image: alpine:latest
stages:
  - build

success:
  stage: build
  script:
    - echo Success && exit 0;

build:
  stage: build
  script:
    - mkdir dist
    - echo "0" > dist/bar.txt
  rules:
    - changes:
      - trigger.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 > trigger.txt
git add trigger.txt
git commit -a -m'trigger';
git push;

echo Check artifacts

This satisfies the rules:changes and at this point you can see the artifact available,

However, when you push up the code the rules:changes fails, and the job does not run but the pipeline succeeds,
echo bump > notrigger.txt
git add notrigger.txt
git commit -a -m'no trigger';
git push;

This will result in your artifacts being destroyed.

Is there a workaround so a conditional job too can preserve its last artifact?

**Tags:** gitlab, gitlab-ci


---

## 1 answers

### Answer by username_0 on 2024-04-29. Score: 0 | Accepted answer

# No workaround

I don't know of a workaround for this yet. I opened up a bug on GitLab. It's tracked here.

* <https://gitlab.com/gitlab-org/gitlab/-/issues/458828>

Try not to use conditional jobs if you need artifacts.


---

