diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 55d1505..48f411b 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -23,19 +23,51 @@ Install [Pre-commit](https://pre-commit.com/#install) software Do the changes in your own GitHub namespace. -## 5. Test the changes +## 5. Test the changes with Molecule -**Note:** I will test the role again, so you don't need to do that if you don't want to. +[Molecule](https://molecule.readthedocs.io/) is a testing framework for Ansible that we use to test the `monolithprojects.github_actions_runner` role. -Install [molecule](https://molecule.readthedocs.io/en/stable/) and run the test: +### Prerequisites + +- Python +- Docker +- Ansible +- Molecule + +### Installation + +1. Install Python, Docker, and Ansible if you haven't already. +2. Install Molecule and its Docker driver with pip: + +```bash +pip install molecule[docker] +``` + +### Running Tests + +1. Navigate to the role's directory: + +```bash +cd path/to/monolithprojects.github_actions_runner +``` + +2. Set Environment variables + +```bash +export PERSONAL_ACCESS_TOKEN=your_github_pat # Your Personal Access Token to Github +export GITHUB_ACCOUNT=your_account # Your Github Account +export GITHUB_ACCOUNT=your_repository # Github Repository where you want to setup the Runner +``` + +3. Run Molecule: ```bash -pip install molecule molecule-docker ansible-lint docker -cd ansible-github_actions_runner molecule test ``` -> You will need to edit the files in `molecule/default` directory (please do not commit those changes) +This will run the molecule test, create a Docker container, run the role against it, run any associated `default` tests (see [molecule/default](../molecule/default) directory), and then destroy the container. + +For more information on using Molecule, see the [Molecule documentation](https://molecule.readthedocs.io/). ## 6. Create a pull request diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index b16625c..342fdc1 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -1,11 +1,13 @@ name: molecule test on: + push: + branches: + - develop pull_request: branches: - master - main - - devel* types: [opened, synchronize, reopened] paths: - 'defaults/**' diff --git a/README.md b/README.md index 6457cb3..d4abea0 100644 --- a/README.md +++ b/README.md @@ -165,6 +165,7 @@ Same example as above, but runner will be added to an organization and deployed ``` If you have a Github Enterprise Cloud license and you want to manage all the self-hosted runners from the enterprise: + ```yaml --- - name: Install GitHub Actions Runner @@ -221,6 +222,78 @@ In this example the Ansible role will uninstall the runner service and unregiste - role: monolithprojects.github_actions_runner ``` +## Testing with Molecule + +[Molecule](https://molecule.readthedocs.io/) is a testing framework for Ansible. This section is for code contributors. + +### Prerequisites + +* Python +* Docker +* Ansible +* Molecule + +### Installation + +1. Install Python, Docker, and Ansible if you haven't already. +2. Install Molecule and its Docker driver with pip: + +```bash +pip install molecule[docker] +``` +Sure, here's a basic example of how you might structure a README to explain how to test the `monolithprojects.github_actions_runner` Ansible role with Molecule: + +```markdown +# monolithprojects.github_actions_runner + +This is an Ansible role for setting up GitHub Actions runners. + +## Testing with Molecule + +[Molecule](https://molecule.readthedocs.io/) is a testing framework for Ansible that we use to test the `monolithprojects.github_actions_runner` role. + +### Prerequisites + +- Python +- Docker +- Ansible +- Molecule + +### Installation + +1. Install Python, Docker, and Ansible if you haven't already. +2. Install Molecule and its Docker driver with pip: + +```bash +pip install molecule[docker] +``` + +### Running Tests + +1. Navigate to the role's directory: + +```bash +cd path/to/monolithprojects.github_actions_runner +``` + +2. Set Environment variables + +```bash +export PERSONAL_ACCESS_TOKEN=your_github_pat # Your Personal Access Token to Github +export GITHUB_ACCOUNT=your_account # Your Github Account +export GITHUB_ACCOUNT=your_repository # Github Repository where you want to setup the Runner +``` + +3. Run Molecule: + +```bash +molecule test +``` + +This will run the molecule test, create a Docker container, run the role against it, run any associated `default` tests (see [molecule/default](./molecule/default) directory), and then destroy the container. + +For more information on using Molecule, see the [Molecule documentation](https://molecule.readthedocs.io/). + ## License MIT diff --git a/molecule/custom_env/cleanup.yml b/molecule/custom_env/cleanup.yml index 8969cf2..34bc208 100644 --- a/molecule/custom_env/cleanup.yml +++ b/molecule/custom_env/cleanup.yml @@ -8,5 +8,6 @@ github_repo: "{{ lookup('env', 'GITHUB_REPO') }}" github_account: "{{ lookup('env', 'GITHUB_ACCOUNT') }}" runner_state: absent + runner_name: test_name roles: - monolithprojects.github_actions_runner diff --git a/molecule/custom_env/converge.yml b/molecule/custom_env/converge.yml index 3d9afd0..bd8f486 100644 --- a/molecule/custom_env/converge.yml +++ b/molecule/custom_env/converge.yml @@ -9,6 +9,7 @@ github_repo: "{{ lookup('env', 'GITHUB_REPO') }}" github_account: "{{ lookup('env', 'GITHUB_ACCOUNT') }}" runner_version: "latest" + runner_name: test_name runner_labels: - label1 - repo-runner diff --git a/molecule/custom_env/verify.yml b/molecule/custom_env/verify.yml index 95b8028..cba84e6 100644 --- a/molecule/custom_env/verify.yml +++ b/molecule/custom_env/verify.yml @@ -10,7 +10,7 @@ github_account: "{{ lookup('env', 'GITHUB_ACCOUNT') }}" github_api_url: "https://api.github.com" access_token: "{{ lookup('env', 'PERSONAL_ACCESS_TOKEN') }}" - runner_name: ubuntu16-latest + runner_name: test_name tasks: - name: Check currently registered runners @@ -32,7 +32,7 @@ ansible.builtin.assert: that: - runner_name in registered_runners.json.runners|map(attribute='name')|list - - registered_runners.json.runners|map(attribute='status') == ["online"] + - registered_runners.json.runners.0.status == "online" quiet: true - name: Check Labels (skipped if labels are OK) diff --git a/molecule/default/verify.yml b/molecule/default/verify.yml index dd95a09..a987064 100644 --- a/molecule/default/verify.yml +++ b/molecule/default/verify.yml @@ -10,7 +10,7 @@ github_account: "{{ lookup('env', 'GITHUB_ACCOUNT') }}" github_api_url: "https://api.github.com" access_token: "{{ lookup('env', 'PERSONAL_ACCESS_TOKEN') }}" - runner_name: ubuntu16-latest + runner_name: test_name tasks: - name: Check currently registered runners @@ -31,6 +31,7 @@ - name: Check Runner ansible.builtin.assert: that: + - runner_name in registered_runners.json.runners|map(attribute='name')|list - registered_runners.json.runners.0.status == "online" quiet: true