When discussing automation in CI/CD pipelines, many developers often overlook GitHub Actions loops. However, they provide a clean and efficient way to handle repetitive tasks within your workflows. In this blog, you’ll explore loops through hands-on examples and step-by-step explanations to simplify automation using native shell scripting.
What Are Loops in GitHub Actions?
Developers use loops in GitHub Actions to repeat a series of steps without duplicating code. Typically, they implement these loops using shell scripting within workflow steps or apply the matrix strategy to run tasks in parallel. This article focuses on the basic shell-based looping mechanisms that are ideal for straightforward tasks like printing messages or processing files.
Instead of rewriting similar steps multiple times, a loop enables automation to be more maintainable and readable. For example, when you need to perform the same action on multiple inputs, a loop becomes essential.
Simple Loop Example in GitHub Actions
A basic example can help in understanding how loops work within a GitHub Actions workflow. In the following example, the workflow dynamically prints greetings for a list of users.
# .github/workflows/loop-example.yml
name: Loop Example
on:
push:
jobs:
greet-people:
runs-on: ubuntu-latest
steps:
- name: Print Greetings
run: |
for name in Alice Bob Charlie
do
echo "Hello, $name!"
done
Explanation:
- The workflow triggers on every push event.
- A job named
greet-people
runs on the latest Ubuntu runner. - A shell
for
loop iterates over the names Alice, Bob, and Charlie, printing a greeting for each.
Once committed and pushed, the output will show individual greetings in the Actions log, demonstrating how easily GitHub Actions loops can reduce code redundancy.
Processing Multiple Files Using Loops
Another common use case for loops is to process multiple files with the same script or command set. The example below demonstrates how you can achieve this behavior..
jobs:
process-files:
runs-on: ubuntu-latest
steps:
- name: Create Dummy Files
run: |
echo "This is file1" > file1.txt
echo "This is file2" > file2.txt
echo "This is file3" > file3.txt
- name: Process Files
run: |
for file in file1.txt file2.txt file3.txt
do
echo "Processing $file"
cat $file
done
Explanation:
- Dummy files are generated using
echo
and saved with unique names. - A loop iterates through each file, prints its name, and displays its content.
- You can extend this logic to run linters, execute scripts, or deploy to multiple targets.
This technique makes your workflows highly scalable without requiring complex constructs or external dependencies.
You can find the complete code here.
Why Use Loops in GitHub Workflows?
Using loops within GitHub Actions:
- Minimizes redundancy: Steps are written once and reused dynamically.
- Improves readability: Logic is easier to maintain and scale.
- Reduces workflow size: Especially helpful in larger automation pipelines.
- Avoids matrix overkill: Not every repetitive task requires parallel execution.
Although the matrix strategy is great for parallel jobs, basic shell loops offer a lightweight solution for sequential repetitive tasks.
Final Thoughts
Loops are an underutilized yet powerful feature in GitHub Actions that can significantly improve your workflow auunderutilisedtomation strategies. By integrating loops in GitHub Actions, you handle repetitive scripting with minimal configuration and maximum clarity.
For more advanced scenarios, matrix builds and reusable workflows are recommended.
Check other blogs on Devops – click here