Managing Workflows with GitHub Actions: needs

| 2 min read

What is the “needs” syntax?

While watching the ‘Learning GitHub Actions’ course on LinkedIn Learning, I stumbled upon the needs syntax. This revelation proved incredibly useful. In GitHub Actions, ‘needs’ defines dependencies between jobs, ensuring a job waits for the completion (success or failure) of specified tasks before starting.

Example:

name: runner

on:
  pull_request:
    branches: [main]

jobs:
  job1:
    runs-on: ubuntu-latest
    steps:
			# do stuff
	job2:
    needs: [job1]
    runs-on: ubuntu-latest
    steps:
			# If job1 is completed then do stuff

This syntax ensures that job2 will only start its execution if job1 has been completed successfully. Furthermore, if you have job3, which depends on both job1 and job2, you can express this dependency by adding needs: [job1, job2] to the configuration of job3.

Example:

  job3:
    needs: [job1, job2]
		runs-on: ubuntu-latest
    steps:
			# If job1and job2 are completed then do stuff

Resources

GitHub Docs - needs

Thank you!

Thank you for your time and for reading this!