From 5e8ab2e5db29069525dc1fc2470a0bfd87d56bcb Mon Sep 17 00:00:00 2001 From: Alexander Gorelyshev Date: Mon, 8 Dec 2025 10:14:03 +0400 Subject: [PATCH 1/8] Try an approach with `github-script` calling Github API --- .github/workflows/molecule.yml | 90 +++++++++++++++++++++++----------- 1 file changed, 62 insertions(+), 28 deletions(-) diff --git a/.github/workflows/molecule.yml b/.github/workflows/molecule.yml index f88ec75..e77d56d 100644 --- a/.github/workflows/molecule.yml +++ b/.github/workflows/molecule.yml @@ -8,37 +8,71 @@ on: branches: [ "master" ] jobs: - - molecule: - name: molecule + detect: + name: Detect changed roles runs-on: ubuntu-latest - timeout-minutes: 10 - - strategy: - matrix: - distro: - - ubuntu2404 - - ubuntu2204 + outputs: + matrix: ${{ steps.make-matrix.outputs.matrix }} steps: - - uses: actions/checkout@v6 - - - uses: mamba-org/setup-micromamba@4d84239119b14cba1690b971f05c0bab912bea04 + - name: Get PR files and build role matrix + id: make-matrix + uses: actions/github-script@v6 with: - micromamba-version: '2.4.0-0' - environment-file: ci/conda.yml - init-shell: bash - post-cleanup: 'all' - cache-environment: true + script: | + // List files changed in the PR and extract top-level role folders under `roles/` + const files = await github.paginate(github.rest.pulls.listFiles, { + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: context.payload.pull_request.number, + per_page: 100 + }); - - name: "Install role dependencies" - run: ansible-galaxy install -r requirements.yml - shell: micromamba-shell {0} + const roles = Array.from(new Set(files.map(f => { + const m = f.filename.match(/^roles\/([^/]+)/); + return m ? `roles/${m[1]}` : null; + }).filter(Boolean))); - - name: "Run Molecule tests" - run: molecule test - shell: micromamba-shell {0} - env: - PY_COLORS: '1' - ANSIBLE_FORCE_COLOR: '1' - MOLECULE_DISTRO: ${{ matrix.distro }} + // If nothing changed, use a sentinel so the matrix is never empty (empty matrices fail the runner) + if (roles.length === 0) { + roles.push('__no_role__'); + } + + const matrix = { include: roles.map(r => ({ role: r })) }; + core.setOutput('matrix', JSON.stringify(matrix)); + + console.log(matrix); + + # molecule: + # name: molecule + # runs-on: ubuntu-latest + # timeout-minutes: 10 + + # strategy: + # matrix: + # distro: + # - ubuntu2404 + # - ubuntu2204 + + # steps: + # - uses: actions/checkout@v6 + + # - uses: mamba-org/setup-micromamba@4d84239119b14cba1690b971f05c0bab912bea04 + # with: + # micromamba-version: '2.4.0-0' + # environment-file: ci/conda.yml + # init-shell: bash + # post-cleanup: 'all' + # cache-environment: true + + # - name: "Install role dependencies" + # run: ansible-galaxy install -r requirements.yml + # shell: micromamba-shell {0} + + # - name: "Run Molecule tests" + # run: molecule test + # shell: micromamba-shell {0} + # env: + # PY_COLORS: '1' + # ANSIBLE_FORCE_COLOR: '1' + # MOLECULE_DISTRO: ${{ matrix.distro }} From 7160e7dc16f8aadcf405ba1bfd788a77b48b8244 Mon Sep 17 00:00:00 2001 From: Alexander Gorelyshev Date: Mon, 8 Dec 2025 10:24:01 +0400 Subject: [PATCH 2/8] Trigger on any branch --- .github/workflows/molecule.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/molecule.yml b/.github/workflows/molecule.yml index e77d56d..af34ca5 100644 --- a/.github/workflows/molecule.yml +++ b/.github/workflows/molecule.yml @@ -3,9 +3,7 @@ name: molecule on: pull_request: - branches: [ "master" ] push: - branches: [ "master" ] jobs: detect: From f84031c1d1506be4f866da05ce065767fe95c7d5 Mon Sep 17 00:00:00 2001 From: Alexander Gorelyshev Date: Mon, 8 Dec 2025 10:35:36 +0400 Subject: [PATCH 3/8] Add a guard protecting against non-PR triggers --- .github/workflows/molecule.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/molecule.yml b/.github/workflows/molecule.yml index af34ca5..f3b9966 100644 --- a/.github/workflows/molecule.yml +++ b/.github/workflows/molecule.yml @@ -18,6 +18,11 @@ jobs: uses: actions/github-script@v6 with: script: | + if (!context.payload.pull_request?.number) { + core.setFailed('This workflow must be triggered by a pull_request event. No pull_request found in context.'); + return; + } + // List files changed in the PR and extract top-level role folders under `roles/` const files = await github.paginate(github.rest.pulls.listFiles, { owner: context.repo.owner, From 4c9a3ad62b2b8ffa6c6f9daa780938735699629e Mon Sep 17 00:00:00 2001 From: Alexander Gorelyshev Date: Mon, 8 Dec 2025 10:37:34 +0400 Subject: [PATCH 4/8] Remove `push` trigger from the Molecule workflow --- .github/workflows/molecule.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/molecule.yml b/.github/workflows/molecule.yml index f3b9966..ceecea5 100644 --- a/.github/workflows/molecule.yml +++ b/.github/workflows/molecule.yml @@ -3,7 +3,6 @@ name: molecule on: pull_request: - push: jobs: detect: From a13b5ecc16238067e0924a63579128cb5043817e Mon Sep 17 00:00:00 2001 From: Alexander Gorelyshev Date: Mon, 8 Dec 2025 10:41:39 +0400 Subject: [PATCH 5/8] Introduce a change to a role to test change detection --- roles/mount_device/README.md | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/roles/mount_device/README.md b/roles/mount_device/README.md index ff4c994..0f6194d 100644 --- a/roles/mount_device/README.md +++ b/roles/mount_device/README.md @@ -23,16 +23,7 @@ None Example Playbook ---------------- -```yaml -- role: mount_device - devices: - - what: /dev/vdb - where: "/somewhere" - fstype: ext4 - opts: - - noatime - state: mounted -``` +See: [converge.yml](molecule/default/converge.yml) License ------- From 5616ff630297100d724617872d98db60326bc24d Mon Sep 17 00:00:00 2001 From: Alexander Gorelyshev Date: Mon, 8 Dec 2025 10:57:23 +0400 Subject: [PATCH 6/8] Test running Molecule in the detected role folder --- .github/workflows/molecule.yml | 40 ++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/.github/workflows/molecule.yml b/.github/workflows/molecule.yml index ceecea5..640633c 100644 --- a/.github/workflows/molecule.yml +++ b/.github/workflows/molecule.yml @@ -45,6 +45,46 @@ jobs: console.log(matrix); + + molecule: + name: Run Molecule for changed roles + needs: detect + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: ${{ fromJson(needs.detect.outputs.matrix) }} + + # Skip the job when only the sentinel is present + if: ${{ matrix.role != '__no_role__' }} + + steps: + - name: Checkout the repo + uses: actions/checkout@v6 + with: + fetch-depth: 0 + + - uses: mamba-org/setup-micromamba@4d84239119b14cba1690b971f05c0bab912bea04 + with: + micromamba-version: '2.4.0-0' + environment-file: ci/conda.yml + init-shell: bash + post-cleanup: 'all' + cache-environment: true + + - name: "Install collection dependencies" + run: ansible-galaxy install -r requirements.yml + shell: micromamba-shell {0} + + - name: "Run Molecule tests" + working-directory: ${{ matrix.role }} + run: molecule test + shell: micromamba-shell {0} + env: + PY_COLORS: '1' + ANSIBLE_FORCE_COLOR: '1' + # MOLECULE_DISTRO: ${{ matrix.distro }} + + # molecule: # name: molecule # runs-on: ubuntu-latest From db64b9f1140617bb431c551c36d113c2f1394ba6 Mon Sep 17 00:00:00 2001 From: Alexander Gorelyshev Date: Mon, 8 Dec 2025 11:12:01 +0400 Subject: [PATCH 7/8] Fix an issue with job-level `if` evaluating before `strategy` --- .github/workflows/molecule.yml | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/.github/workflows/molecule.yml b/.github/workflows/molecule.yml index 640633c..66f6faa 100644 --- a/.github/workflows/molecule.yml +++ b/.github/workflows/molecule.yml @@ -54,16 +54,16 @@ jobs: fail-fast: false matrix: ${{ fromJson(needs.detect.outputs.matrix) }} - # Skip the job when only the sentinel is present - if: ${{ matrix.role != '__no_role__' }} - steps: - name: Checkout the repo + if: ${{ matrix.role != '__no_role__' }} uses: actions/checkout@v6 with: fetch-depth: 0 - - uses: mamba-org/setup-micromamba@4d84239119b14cba1690b971f05c0bab912bea04 + - name: "Set up micromamba" + uses: mamba-org/setup-micromamba@4d84239119b14cba1690b971f05c0bab912bea04 + if: ${{ matrix.role != '__no_role__' }} with: micromamba-version: '2.4.0-0' environment-file: ci/conda.yml @@ -72,10 +72,12 @@ jobs: cache-environment: true - name: "Install collection dependencies" + if: ${{ matrix.role != '__no_role__' }} run: ansible-galaxy install -r requirements.yml shell: micromamba-shell {0} - name: "Run Molecule tests" + if: ${{ matrix.role != '__no_role__' }} working-directory: ${{ matrix.role }} run: molecule test shell: micromamba-shell {0} From d50809e981ac3a0be28898e27f1d2271a57dcb9a Mon Sep 17 00:00:00 2001 From: Alexander Gorelyshev Date: Mon, 8 Dec 2025 11:44:15 +0400 Subject: [PATCH 8/8] Shorten a job name --- .github/workflows/molecule.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/molecule.yml b/.github/workflows/molecule.yml index 66f6faa..d067da9 100644 --- a/.github/workflows/molecule.yml +++ b/.github/workflows/molecule.yml @@ -47,7 +47,7 @@ jobs: molecule: - name: Run Molecule for changed roles + name: Run Molecule needs: detect runs-on: ubuntu-latest strategy: