Automating Collection Testing in GitHub
Streamlining Ansible Collection Testing with GitHub Actions
Introduction
GitHub Actions provides a powerful and flexible platform for automating workflows directly within your GitHub repository. One common use case is automating the testing of collections in Ansible, ensuring that each component functions as expected. This article will guide you through the setup of a GitHub Actions workflow for testing Ansible collections using the .github/test.yml
configuration.
Understanding the Configuration
Let’s break down the structure of the .github/test.yml
file:
name: GHA for foo.bar
concurrency:
group: ${{ github.head_ref || github.run_id }}
cancel-in-progress: true
on:
pull_request:
branches: [main]
workflow_dispatch:
schedule:
- cron: '0 0 * * *'
name: Describes the name of the GitHub Actions workflow.
concurrency: Specifies concurrency settings, ensuring that workflows do not interfere with each other. The
group
attribute combines workflows under the same conditions, andcancel-in-progress
cancels any currently running workflow if a new one is triggered.on: Defines the events that trigger the workflow. In this case, the workflow is triggered on pull requests targeting the
main
branch, manually using workflow_dispatch, and on a daily schedule.
The Best Resources For Ansible
Certifications
- Coursera Pro - Unlimited access to 7,000+ world-class courses, hands-on projects, and job-ready certificate programs—all included in your subscription
Video Course
Printed Book
-
Ansible For VMware by Examples
-
Ansible for Kubernetes by Example
-
Hands-on Ansible Automation
-
Red Hat Ansible Automation Platform
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- Ansible Cookbook: A Comprehensive Guide to Unleashing the Power of Ansible via Best Practices, Troubleshooting, and Linting Rules with Luca Berton
- Terraform By Example: A Practical Approach for Beginners to Learn Cloud Infrastructure with Terraform
- Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
- Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
- Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
- Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
- Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
- Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
- Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
- Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
- Ansible Automation Platform By Example: A step-by-step guide for the most common user scenarios
Now, let’s look at the job definitions:
jobs:
ansible-lint:
uses: ansible-network/github_actions/.github/workflows/ansible-lint.yml@main
changelog:
uses: ansible-network/github_actions/.github/workflows/changelog.yml@main
integration:
uses: ansible-network/github_actions/.github/workflows/integration_simple.yml@main
sanity:
uses: ansible-network/github_actions/.github/workflows/sanity.yml@main
unit-galaxy:
uses: ansible-network/github_actions/.github/workflows/unit_galaxy.yml@main
jobs: Lists the individual jobs that will be executed. Each job is defined separately and utilizes existing workflows from the
ansible-network/github_actions
repository.ansible-lint
,changelog
,integration
,sanity
, andunit-galaxy
are examples of jobs that can be run independently.
Next is the all_green
job:
all_green:
if: ${{ always() && (github.event_name != 'schedule') }}
needs:
- changelog
- integration
- sanity
- unit-galaxy
runs-on: ubuntu-latest
steps:
- run: >-
python -c "assert set([
'${{ needs.changelog.result }}',
'${{ needs.integration.result }}',
'${{ needs.sanity.result }}',
'${{ needs.unit-galaxy.result }}'
]) == {'success'}"
all_green: This job runs only if certain conditions are met. It runs on the latest version of Ubuntu and depends on the successful completion of the
changelog
,integration
,sanity
, andunit-galaxy
jobs.steps: Specifies the steps to be executed within the job. In this case, it runs a Python script to assert that the results of the dependent jobs are all ‘success’. If any job fails, the assertion will fail, and the workflow will not proceed.
Conclusion
By using the GitHub Actions workflow defined in the .github/test.yml
file, you can automate the testing of Ansible collections, ensuring that changes to your repository are thoroughly validated. This setup promotes efficiency, consistency, and reliability in your development process. Feel free to customize the workflow to fit the specific needs of your project.
Academy
Learn the Ansible automation technology with some real-life examples in my
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate