Hey everyone, I’m trying to figure out how to make my GitHub Actions workflows depend on each other. I’ve got a monorepo with two workflow files: one for testing and another for deployment.
The test workflow runs on every push and pull request, while the deploy workflow should only run on tags. I want the deploy workflow to run after the test workflow, but only when I push a tag.
Here’s what I’ve tried:
# test.yml
name: Run Tests
on: [push, pull_request]
jobs:
run-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: npm test
# deploy.yml
name: Deploy
on:
push:
tags: ['*']
jobs:
deploy-app:
runs-on: ubuntu-latest
needs: run-tests
steps:
- uses: actions/checkout@v2
- run: npm run deploy
But this doesn’t work. I get an error saying the pipeline isn’t valid because it needs at least one job with no dependencies.
Is there a way to make workflows depend on each other? I don’t want to copy the test job into my deploy workflow. Any ideas on how to set this up correctly?
As someone who’s wrestled with this exact problem, I can tell you there’s a clever way to handle it using GitHub Actions’ workflow_dispatch event. Here’s what I’ve found works well:
Keep your test.yml as is. For deploy.yml, use this approach:
name: Deploy
on:
push:
tags: ['*']
workflow_dispatch:
jobs:
run-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: npm test
deploy-app:
needs: run-tests
runs-on: ubuntu-latest
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
steps:
- uses: actions/checkout@v2
- run: npm run deploy
This setup runs tests on every tag push, then deploys if tests pass. The workflow_dispatch event lets you manually trigger the workflow if needed. It’s been rock-solid for my projects, ensuring deployments only happen after successful tests on tagged releases.
I’ve tackled a similar issue in my projects, and I found a workaround that might help you. Instead of trying to make workflows depend on each other directly, which GitHub Actions doesn’t support, you can use a combination of workflow triggers and job conditions.
Here’s what worked for me:
-
Keep your test workflow as is, running on every push and pull request.
-
Modify your deploy workflow to run on both pushes to main and tag creation:
name: Deploy
on:
push:
branches: [main]
tags: ['*']
jobs:
run-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: npm test
deploy-app:
needs: run-tests
if: startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: npm run deploy
This setup ensures tests run on every push, including tag creation. The deploy job only executes if the tests pass and the trigger was a tag push. It’s not perfect, but it achieves the desired workflow without duplicating test steps across files.
I’ve encountered this issue before, and there’s a neat solution using GitHub Actions’ workflow_run event. Here’s how you can restructure your workflows:
Keep your test.yml as is, running on every push and pull request.
Modify your deploy.yml to use the workflow_run event:
name: Deploy
on:
workflow_run:
workflows: ["Run Tests"]
types:
- completed
branches:
- main
jobs:
deploy-app:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' && startsWith(github.ref, 'refs/tags/') }}
steps:
- uses: actions/checkout@v2
- run: npm run deploy
This setup ensures the deploy workflow only runs after the test workflow completes successfully, and only on tag pushes to the main branch. It avoids duplicating test steps and maintains the dependency you’re looking for between workflows.
hey, i’ve dealt with this before. try using github’s concurrency feature. it lets u run workflows in sequence. add this to ur deploy.yml:
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
this’ll make sure deploy waits for tests to finish before running. hope it helps!