Navigation
Getting startedCreating a workflowBuilding code in a workflowRunning multiple jobs in parallel- Running jobs in sequence (this task)
- Deploying to GitHub Pages
- Using other events to run workflows
- Outputs from steps and jobs
- Keeping dependencies up to date with Dependabot
- Matrices
- Workflow dispatch inputs and security verification
- Learn more about GitHub Actions
As we learned in the previous task, we can run multiple jobs in parallel. Most jobs can be run at the same time, however in some cases jobs need to wait for each other to finish. For instance if we have a step that deploys the application, we might want to run end-to-end tests after the deployment has finished.
For this, we can use the needs
keyword:
jobs:
job1:
runs-on: ubuntu-latest
steps:
- name: Step 1
run: echo "Step 1"
- name: Step 2
run: echo "Step 2"
job2:
runs-on: ubuntu-latest
+ needs: job1
steps:
- name: Step 1
run: echo "Step 1"
- name: Step 2
run: echo "Step 2"
If we take a look through the GitHub Marketplace, we can see that there are thousands upon thousands of actions available for us to use. Some of these are created by GitHub themselves, some are created by other companies and some are created by individuals. Let's use peter-evans/create-or-update-comment to post a comment to the PR once all jobs are finished.
Create a new job which uses the peter-evans/create-or-update-comment
action:
jobs:
job1: ...
job2: ...
post-comment:
runs-on: ubuntu-latest
needs: [job1, job2]
steps:
- name: Post comment
uses: peter-evans/create-or-update-comment@v4
with:
issue-number: ${{ github.event.pull_request.number }}
body: |
All jobs have finished! 🎉
Here we've used github.event.pull_request.number
which is part of the context that is available to us.
Important
What event is used (in this case pull_request
) determines what context is available.
Run the workflow and see if it works. Doesn't it??? Perhaps we'll have to do some debugging 🤔
If we click the details
link in the workflow run, we can see that the post-comment
job is failing.
What does the log say?
It looks like some kind of permissions error, as we get a RequestError [HttpError]: Resource not accessible by integration
error which is one of the least explanatory errors I know of.
What's happening here?
Some things that workflows can do need permissions.
For instance, if we want to post a comment to a pull request in a public repository, we need to have the pull-requests: write
permission.
There are many different permissions we need to give our jobs if we want them to perform certain actions, and we can see which permissions are required in the documentation.
Permissions can be added to workflows or to jobs.
Best practices is to use as little as possible.
In our case we only need to add permissions to the post-comment
job, so let's do that:
post-comment:
runs-on: ubuntu-latest
+ permissions:
+ pull-requests: write
needs: [job1, job2]
steps:
- name: Post comment
uses: peter-evans/create-or-update-comment@v4
with:
issue-number: ${{ github.event.pull_request.number }}
body: |
All jobs have finished! 🎉
Now run the workflow again and see if it works.
It did?? You're learning fast! 🚀 Let's try deploying to production in task 5!