<pre class="lang-yaml prettyprint-override"><code># .github/workflows/build-image.yml
name: Build Image

on:
  push:
    branches:
      - main
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Build Docker image
        run: |
          docker build -t my-image:latest .
          echo &quot;IMAGE_TAG=my-image:latest&quot; &gt;&gt; $GITHUB_ENV

      - name: Upload image tag
        uses: actions/upload-artifact@v2
        with:
          name: image-tag
          path: $GITHUB_ENV
</code></pre>
<pre class="lang-yaml prettyprint-override"><code># .github/workflows/generate-artifacts.yml
name: Generate Artifacts

on:
  workflow_run:
    workflows: [&quot;Build Image&quot;]
    types:
      - completed

jobs:
  generate:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Download image tag
        uses: actions/download-artifact@v2
        with:
          name: image-tag
          path: .

      - name: Load image tag
        run: source $GITHUB_ENV

      - name: Generate artifacts
        run: |
          docker run --rm my-image:latest generate-artifacts
          # Add commands to generate and upload your artifacts here
</code></pre>

CC BY-SA 4.0
2024-11-12T19:35:19.643Z
Generated Artifacts only runs here when the other workflow is complete. That's not "optional"
Evan Carroll
CC BY-SA 4.0
2024-11-15T15:04:23.103Z
I missed something. Here you didn't talk about it being optional : "but every time an image is built the job that generates artifacts in a different workflow should run AFTER it."
YassineLbk
CC BY-SA 4.0
2024-11-15T15:05:44.997Z
In the first workflow, this is missing "Not every push builds image". Because, you didn't define conditions on whats kind of commits/pushs can trigger the image's build.
YassineLbk
CC BY-SA 4.0
2024-11-15T15:07:24.393Z
It's literally in the title "optional workflows", and the question says "Not every push builds image" but I did use the word "optional" now in the question too
Evan Carroll
CC BY-SA 4.0
2024-11-10T23:18:51.883Z
2024-11-15T15:03:38.323Z
2024-11-15T15:03:38.323Z
YassineLbk
<p>I have two work workflows in a repository.</p>
<ol>
<li>An optional workflow that builds an image.</li>
<li>Uses the latest image created by the optional workflow to generate artifacts.</li>
</ol>
<p>Not every push builds image, but every time an image is built the job that generates artifacts in a different workflow should run <strong>AFTER</strong> it.</p>
<p>How can I model this with GitHub Actions?</p>

<github-actions>
How does Github Actions handle optional workflows?
Evan Carroll
# How does Github Actions handle optional workflows?

(Asked by: username_0 on 2024-11-08)

I have two work workflows in a repository.

1. An optional workflow that builds an image.
2. Uses the latest image created by the optional workflow to generate artifacts.

Not every push builds image, but every time an image is built the job that generates artifacts in a different workflow should run **AFTER** it.

How can I model this with GitHub Actions?

**Tags:** github-actions


---

## 1 answers

### Answer by username_1 on 2024-11-10. Score: 0

# .github/workflows/build-image.yml
name: Build Image

on:
  push:
    branches:
      - main
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Build Docker image
        run: |
          docker build -t my-image:latest .
          echo "IMAGE_TAG=my-image:latest" >> $GITHUB_ENV

      - name: Upload image tag
        uses: actions/upload-artifact@v2
        with:
          name: image-tag
          path: $GITHUB_ENV

# .github/workflows/generate-artifacts.yml
name: Generate Artifacts

on:
  workflow_run:
    workflows: ["Build Image"]
    types:
      - completed

jobs:
  generate:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Download image tag
        uses: actions/download-artifact@v2
        with:
          name: image-tag
          path: .

      - name: Load image tag
        run: source $GITHUB_ENV

      - name: Generate artifacts
        run: |
          docker run --rm my-image:latest generate-artifacts
          # Add commands to generate and upload your artifacts here

> **Comments:**
> **username_0**: Generated Artifacts only runs here when the other workflow is complete. That's not "optional"  
> **username_1**: I missed something. Here you didn't talk about it being optional : "but every time an image is built the job that generates artifacts in a different workflow should run AFTER it."  
> **username_1**: In the first workflow, this is missing "Not every push builds image". Because, you didn't define conditions on whats kind of commits/pushs can trigger the image's build.  
> **username_0**: It's literally in the title "optional workflows", and the question says "Not every push builds image" but I did use the word "optional" now in the question too  

---

