|
| 1 | +# How to Implement This Workflow |
| 2 | + |
| 3 | +[Documentation](https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/control-the-concurrency-of-workflows-and-jobs) |
| 4 | + |
| 5 | +1. **Navigate to Your Repository** |
| 6 | + Open your GitHub repository where you want to add the workflow. |
| 7 | + |
| 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. |
| 10 | + |
| 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`. |
| 13 | + |
| 14 | +4. **Set the Workflow Name** |
| 15 | + At the top of the file, specify a name for the workflow, such as “Concurrency Demo”. |
| 16 | + |
| 17 | +5. **Configure the Trigger** |
| 18 | + Set the workflow to be triggered manually using the `workflow_dispatch` event. |
| 19 | + |
| 20 | +6. **Define Concurrency Settings** |
| 21 | + 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 | + |
| 23 | +<details> |
| 24 | + <summary>Solution</summary> |
| 25 | + |
| 26 | +```YAML |
| 27 | +concurrency: |
| 28 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 29 | + cancel-in-progress: true |
| 30 | +``` |
| 31 | +
|
| 32 | +</details> |
| 33 | +
|
| 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. |
| 36 | + |
| 37 | +<details> |
| 38 | + <summary>Solution</summary> |
| 39 | + |
| 40 | +```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 |
| 53 | +``` |
| 54 | + |
| 55 | +</details> |
| 56 | + |
| 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. |
| 59 | + |
| 60 | +<details> |
| 61 | + <summary>Solution</summary> |
| 62 | + |
| 63 | +```YAML |
| 64 | + - name: Download tree output |
| 65 | + uses: actions/download-artifact@v4 |
| 66 | + with: |
| 67 | + name: tree-output |
| 68 | +
|
| 69 | + - name: Add job summary with tree output |
| 70 | + run: | |
| 71 | + echo "### Job completed! :rocket:" >> $GITHUB_STEP_SUMMARY |
| 72 | + echo '### Project Directory Tree' >> $GITHUB_STEP_SUMMARY |
| 73 | + echo '```' >> $GITHUB_STEP_SUMMARY |
| 74 | + cat tree.txt >> $GITHUB_STEP_SUMMARY |
| 75 | + echo '```' >> $GITHUB_STEP_SUMMARY |
| 76 | +``` |
| 77 | + |
| 78 | +</details> |
| 79 | + |
| 80 | +9. **Save and Commit** |
| 81 | + Save the workflow file and commit it to your repository. |
| 82 | + |
| 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. |
| 85 | + |
| 86 | +<details> |
| 87 | + <summary>Solution</summary> |
| 88 | + |
| 89 | +```YAML |
| 90 | +name: Concurrency Demo |
| 91 | +
|
| 92 | +on: |
| 93 | + workflow_dispatch: |
| 94 | +
|
| 95 | +concurrency: |
| 96 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 97 | + cancel-in-progress: true |
| 98 | +
|
| 99 | +jobs: |
| 100 | + upload-tree: |
| 101 | + runs-on: ubuntu-latest |
| 102 | + steps: |
| 103 | + - name: Checkout |
| 104 | + uses: actions/checkout@v4 |
| 105 | +
|
| 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::" |
| 112 | +
|
| 113 | + - name: Upload tree output |
| 114 | + uses: actions/upload-artifact@v4 |
| 115 | + with: |
| 116 | + name: tree-output |
| 117 | + path: tree.txt |
| 118 | +
|
| 119 | + - name: Simulate work |
| 120 | + run: sleep 10 |
| 121 | +
|
| 122 | + add-summary: |
| 123 | + runs-on: ubuntu-latest |
| 124 | + needs: upload-tree |
| 125 | + steps: |
| 126 | + - name: Download tree output |
| 127 | + uses: actions/download-artifact@v4 |
| 128 | + with: |
| 129 | + name: tree-output |
| 130 | +
|
| 131 | + - name: Simulate work |
| 132 | + run: sleep 10 |
| 133 | +
|
| 134 | + - name: Add job summary with tree output |
| 135 | + run: | |
| 136 | + echo "### Job completed! :rocket:" >> $GITHUB_STEP_SUMMARY |
| 137 | + echo '### Project Directory Tree' >> $GITHUB_STEP_SUMMARY |
| 138 | + echo '```' >> $GITHUB_STEP_SUMMARY |
| 139 | + cat tree.txt >> $GITHUB_STEP_SUMMARY |
| 140 | + echo '```' >> $GITHUB_STEP_SUMMARY |
| 141 | +``` |
| 142 | + |
| 143 | +</details> |
| 144 | + |
0 commit comments