How to make a job wait for another job if it's running or run it anyway

I have a workflow for the deployment of my staging branch that is triggered at every push at the staging branch. This workflow:

  • Builds a Docker image (using a base Docker image)
  • Deploy the application

I want to add a workflow that detects changes in a Dockerfile base file and builds this base image and push it to the registry. This workflow should be triggered every time that a push to staging happens (a PR was approved and merged onto the staging branch).

But because the staging deployment workflow builds an image that depends on the base image, when a push to staging is done, both workflows will be triggered and I fear that the staging deployment finishes before the new base image is pushed, so they will be out of sync.

Essentially what I would like to do is combine both workflows into one, so every time a push to staging happens, the workflow will detect the changes in files, if the Dockerfile is changed, it builds the base image and only after it finishes, it proceeds with the rest of deployment. However, if the Dockerfile is not changed (any other PR that is merged) it should skip the job of building the image and build the staging instead.

I have managed to succeed on the change detection step but the staging build is dependant on the base build and I don’t know how to bypass it if the base job can be skipped. I am using this action for the change detection.

name: ci-staging

      - staging
    name: Detect changes

    runs-on: ubuntu-latest

      docker: ${{ steps.changes.outputs.docker }}

      - name: Checkout repository
        uses: actions/checkout@v2

      - name: Check modified source
        uses: dorny/paths-filter@v2
        id: changes
          filters: |
              - 'dockerfiles/Dockerfile.base'
              - 'requirements.txt'

  # This job will run only if requirements.txt or Dockerfile.base have changed
    needs: [detect-changes]

    name: Build base image

    if: ${{ needs.detect-changes.outputs.docker == 'true' }}
    runs-on: ubuntu-latest

      - name: Checkout repository
        uses: actions/checkout@v2

  # Build the staging Docker image. However, if the base Dockerfile has been changed,
  # this job needs to wait until a new version of the base image is built and pushed
    needs: [build-base]

    name: Build Staging Image

    runs-on: ubuntu-latest