You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
GitHub Actions can run multiple workflows simultaneously, but sometimes you want to control this behavior to prevent conflicts or resource contention. Workflow concurrency allows you to limit the number of workflow runs that can execute at the same time.
4
+
5
+
:bulb: Concurrency is particularly useful for deployment workflows where you don't want multiple deployments running simultaneously.
6
+
7
+
## Learning Goals
4
8
5
-
1.**Navigate to Your Repository**
6
-
Open your GitHub repository where you want to add the workflow.
9
+
- Understand how to configure workflow concurrency groups
10
+
- Learn how to cancel in-progress workflows when new ones start
11
+
- Practice using workflow artifacts to pass data between jobs
12
+
- Experience workflow summaries and output grouping
7
13
8
-
2.**Create the Workflow Directory**
9
-
Ensure there is a `.github/workflows` directory in the root of your repository. If it does not exist, create it.
14
+
## Understanding Workflow Concurrency
10
15
11
-
3.**Add a New Workflow File**
12
-
Inside the `.github/workflows` directory, create a new file. You can name it, for example, `concurency-lab.yml`.
16
+
In this exercise, we will create a workflow that demonstrates concurrency control by:
13
17
14
-
4.**Set the Workflow Name**
15
-
At the top of the file, specify a name for the workflow, such as “Concurrency Demo”.
18
+
- Setting up a concurrency group to manage workflow runs
19
+
- Creating jobs that generate and process artifacts
20
+
- Using workflow summaries to display results
21
+
- Simulating work with delays to observe concurrency behavior
22
+
23
+
The workflow will have two jobs that work together: one that generates a directory tree listing and uploads it as an artifact, and another that downloads the artifact and creates a workflow summary.
Set the workflow to be triggered manually using the `workflow_dispatch` event.
27
+
## Task
19
28
20
-
6.**Define Concurrency Settings**
29
+
1.**Set Up the Workflow File**
30
+
Create a new workflow file named `concurrency-lab.yml` in the `.github/workflows` directory of your repository. Set the workflow name to "Concurrency Demo" and configure it to be triggered manually using the `workflow_dispatch` event.
31
+
32
+
2.**Define Concurrency Settings**
21
33
Add a concurrency group that uses the workflow name and reference. Enable the option to cancel any in-progress runs if a new one starts.
22
34
23
35
<details>
@@ -31,41 +43,60 @@ concurrency:
31
43
32
44
</details>
33
45
34
-
7. **Add the 'upload-tree' Job**
35
-
Add a job that runs on the latest Ubuntu runner. Include step to check out the repository, list the files in the repository and add it to a `.txt` file, group the output with the `::group::` workflow command. Add a step to upload the `.txt` file as an artifact and simulate work by pausing for 10 seconds.
46
+
3. **Create the 'upload-tree' Job**
47
+
Add a job that runs on the latest Ubuntu runner with the following steps:
48
+
- Check out the repository
49
+
- Generate directory tree using the provided script
50
+
- Upload the tree output as an artifact
51
+
- Simulate work by pausing for 10 seconds
36
52
37
53
<details>
38
54
<summary>Solution</summary>
39
55
40
56
```YAML
41
-
- name: List files in the repository
42
-
run: |
43
-
echo "::group::The repository ${{ github.repository }} contains the following files"
44
-
tree > tree.txt
45
-
cat tree.txt
46
-
echo "::endgroup::"
47
-
48
-
- name: Upload tree output
49
-
uses: actions/upload-artifact@v4
50
-
with:
51
-
name: tree-output
52
-
path: tree.txt
57
+
upload-tree:
58
+
runs-on: ubuntu-latest
59
+
steps:
60
+
- name: Checkout
61
+
uses: actions/checkout@v4
62
+
63
+
- name: Generate directory tree
64
+
run: helper-scripts/generate-tree.sh
65
+
66
+
- name: Upload tree output
67
+
uses: actions/upload-artifact@v4
68
+
with:
69
+
name: tree-output
70
+
path: tree.txt
71
+
72
+
- name: Simulate work
73
+
run: sleep 10
53
74
```
54
75
55
76
</details>
56
77
57
-
8. **Add the 'add-summary' Job**
58
-
Add a second job that also runs on the latest Ubuntu runner. Download the artifact with the `.txt` file, Include a step to simulate work by pausing for 10 seconds, and then add a [workflow summary](https://github.blog/news-insights/product-news/supercharging-github-actions-with-job-summaries/) to the job showing the contents of the `.txt` file.
78
+
4. **Create the 'add-summary' Job**
79
+
Add a second job that depends on the first job and runs on the latest Ubuntu runner with the following steps:
80
+
- Download the tree artifact
81
+
- Simulate work by pausing for 10 seconds
82
+
- Add a [workflow summary](https://github.blog/news-insights/product-news/supercharging-github-actions-with-job-summaries/) showing the directory tree
Save the workflow file and commit it to your repository.
111
+
5. **Test the Concurrency Behavior**
112
+
Save and commit the workflow file, then go to the Actions tab in your GitHub repository. Trigger the workflow twice in quick succession to observe that only the jobs from the last triggered workflow will run, as earlier runs will be cancelled due to the concurrency settings.
82
113
83
-
10. **Run the Workflow**
84
-
Go to the Actions tab in your GitHub repository, select the workflow, and trigger it twice in quick succession. Observe that only the jobs from the last triggered workflow will run, as earlier runs will be cancelled due to the concurrency settings.
114
+
<details>
115
+
<summary>:bulb: Git commands to commit the files</summary>
You should see that when you trigger the workflow multiple times rapidly:
128
+
- Only the most recent workflow run completes
129
+
- Previous runs are cancelled automatically
130
+
- The workflow summary displays the project directory tree
131
+
- The concurrency group prevents multiple simultaneous runs
85
132
86
133
<details>
87
-
<summary>Solution</summary>
134
+
<summary>Complete Solution</summary>
88
135
89
136
```YAML
90
137
name: Concurrency Demo
@@ -103,12 +150,8 @@ jobs:
103
150
- name: Checkout
104
151
uses: actions/checkout@v4
105
152
106
-
- name: List files in the repository
107
-
run: |
108
-
echo "::group::The repository ${{ github.repository }} contains the following files"
109
-
tree > tree.txt
110
-
cat tree.txt
111
-
echo "::endgroup::"
153
+
- name: Generate directory tree
154
+
run: helper-scripts/generate-tree.sh
112
155
113
156
- name: Upload tree output
114
157
uses: actions/upload-artifact@v4
@@ -142,3 +185,14 @@ jobs:
142
185
143
186
</details>
144
187
188
+
## Summary
189
+
190
+
Congratulations! You have successfully implemented workflow concurrency controls. You've learned how to:
191
+
192
+
- Configure concurrency groups to prevent simultaneous workflow runs
193
+
- Use the `cancel-in-progress` option to automatically cancel outdated runs
194
+
- Work with artifacts to pass data between jobs
195
+
- Create workflow summaries for better visibility into your pipeline results
196
+
197
+
This concurrency pattern is especially valuable for deployment workflows where you want to ensure only one deployment happens at a time, preventing conflicts and ensuring consistency.
0 commit comments