1

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?

Evan Carroll
  • 2,921
  • 6
  • 37
  • 85

2 Answers2

1
# .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
YassineLbk
  • 160
  • 7
1

You can run a step after a previous step has run successfully with "needs" and "if"

Here dorny/paths-filter is sused to check if a version file changed. If so then outputs.client == true and successful() is also true

jobs:
  changes:
    runs-on: ubuntu-latest
    outputs:
      client: ${{ steps.changes.outputs.client }}
    steps:
    - uses: actions/checkout@v4
    - uses: dorny/paths-filter@v3
      id: changes
      with:
        filters: |
          client:
            - 'versions/mongodbapi/client'

clientnuget: runs-on: ubuntu-latest needs: [ changes ] if: ${{ needs.changes.outputs.client == 'true' }}
steps: - uses: actions/checkout@v4 .......................

Serve Laurijssen
  • 594
  • 2
  • 8
  • 17