Reusable Workflow and Composite Action
If you are working with continuous integration and continuous deployment, you often have to repeat the same steps in your workflows. For example, for your deployment workflow in Azure, you need to set up Azure CLI, configure the environment and other credentials, and run the actual deployment process. Additionally, you need to clean up afterward.
To streamline this process, you can use workflow templates
and composite actions
in GitHub Actions. Let’s see what these are and how you can leverage them in your workflows.
What is workflow template?
A workflow template is a pre-configured workflow that you can use as a starting point for your own workflows. Templates save you time and effort by providing a basic structure that you can customize to fit your needs. They are defined in YAML files and stored in a separate repository. This means you can share them with others or reuse them in different projects.
Here are some benefits of using workflow templates in GitHub Actions:
- Save time: Workflow templates provide a basic structure that you can tailor to your needs, saving you significant time.
- Increase consistency: Using templates ensures your workflows are consistent and follow best practices.
- Improve collaboration: Workflow templates make it easier to collaborate on workflows with others.
- Reduce errors: Templates help reduce errors by providing well-tested and documented workflows.
Example of Workflow Template
Main workflow:
This main workflow calls a template file stored in the repository mayanksoni/github-actions
, specifically at the path .github/workflows/template.yml
.
Template Workflow:
This template workflow receives inputs from the main workflow. To use these inputs, you can reference them as shown in the ENVIRONMENT
variable.
In this way, you have a reusable template that builds your image based on different inputs. You can store this template file in the same repository as the main workflow or in a different repository.
What is composite action?
A composite action in GitHub Actions is a single action that runs multiple other actions. This is useful for grouping related tasks or creating more complex workflows. Composite actions are defined in YAML files and stored in the same repository as the workflow using them, meaning they are not reusable in other repositories but can simplify and modularize your workflows.
Composite actions can have inputs and outputs and support nesting up to 10 layers, allowing you to call other composite actions within them.
Example of Composite Action
Workflow Using Composite Action:
Here, ./.github/actions/docker-build
denotes the folder containing the action.yml
file for the composite action. Unlike workflow templates, you specify the folder name in uses
.
Composite Action (action.yml):
In the composite action file, we define the inputs that the main workflow will provide and the outputs that the composite action will return. The output variable first needs to be defined and then assigned a value.
Feel free to watch our video for more detailed information. If you find the content useful, please subscribe to the channel.
Conclusion:
These powerful features allow you to streamline your continuous integration and continuous deployment processes, saving time and effort while ensuring consistency and reducing errors.
By using workflow templates, you can create a reusable structure that can be customized for different projects, facilitating collaboration and maintaining best practices. Composite actions, on the other hand, enable you to modularize your workflows by grouping related tasks together, making your CI/CD pipeline more efficient and manageable.
Both workflow templates and composite actions are essential tools for anyone looking to optimize their GitHub Actions workflows. We hope this blog post has provided you with valuable insights and practical examples to help you get started with these features.
Important links