GitHub Actions Workflow Functions: Understanding always
with Examples
Hello, GitHub enthusiasts! Today, we’ll be exploring a key function used in GitHub Actions workflows – always
. This function enables us to control workflow progression, ensuring certain steps are executed regardless of the status of prior steps or jobs.
Understanding the always
Function
In GitHub Actions, the always
function is used in conditional checks to ensure that a step or a job always runs, no matter the outcome of the previous operation.
The syntax is straightforward:
always()
: Returnstrue
, ensuring that the step or job it’s placed in will always run.
This function is typically used with the if
conditional keyword to manage the execution of steps or jobs within your workflows.
Let’s dig into some examples to see how this function can be applied in practical scenarios.
Using always
in a Step
Suppose you have a workflow where, regardless of the outcome of the previous step, you want to run a clean-up operation. Here’s how you can achieve that:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
jobs: build: runs-on: ubuntu-latest steps: - name: Run tests id: tests run: | # Commands to run your tests... - name: Cleanup if: always() run: | # Commands to clean up resources... |
In this case, the Cleanup
step will run irrespective of whether the Run tests
step was successful or failed, due to the if: always()
condition.
Using always
in a Job
The always
function can also manage the execution of an entire job, irrespective of the outcome of a prior job. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
jobs: build: runs-on: ubuntu-latest steps: - name: Build run: | # Commands to build your application... cleanup: needs: build runs-on: ubuntu-latest if: always() steps: - name: Cleanup run: | # Commands to clean up resources... |
Here, the cleanup
job will run regardless of whether the build
job was successful or failed, as indicated by the if: always()
condition.
Conclusion
The always
function in GitHub Actions provides you with a means to ensure certain operations are performed in your workflows, regardless of the success or failure of preceding steps or jobs. By mastering this function, you can design more resilient and controlled CI/CD pipelines.
Remember, GitHub Actions offers a wide array of functions to make your workflows more powerful and flexible. By learning how to use these effectively, you can create workflows that are more robust and can adapt to your project’s unique requirements. Happy coding!