Matrix Docker Ansible eploy
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

151 righe
4.8 KiB

  1. # SPDX-FileCopyrightText: 2026 Slavi Pantaleev
  2. #
  3. # SPDX-License-Identifier: AGPL-3.0-or-later
  4. ---
  5. name: Molecule
  6. # Unlike the MASH role repositories, where one repository holds one role, every
  7. # role here lives in the same repository. Running every scenario on every push
  8. # would be unaffordable, so a first job works out which roles the push actually
  9. # touched and the matrix is built from that. A push that changes documentation,
  10. # or a role with no scenario yet, runs nothing at all.
  11. on: # yamllint disable-line rule:truthy
  12. push:
  13. paths:
  14. - "roles/custom/**"
  15. - ".github/workflows/molecule.yml"
  16. pull_request:
  17. paths:
  18. - "roles/custom/**"
  19. - ".github/workflows/molecule.yml"
  20. workflow_dispatch:
  21. inputs:
  22. role:
  23. description: "Single role to test (directory name under roles/custom), or empty for all roles that have a scenario"
  24. required: false
  25. type: string
  26. permissions:
  27. contents: read
  28. jobs:
  29. detect:
  30. name: Work out which roles to test
  31. runs-on: ubuntu-latest
  32. # Same rule as the MASH repositories: a pull request from a branch of this
  33. # repository would otherwise run everything twice, once for the push and
  34. # once for the pull request.
  35. if: >-
  36. github.event_name != 'pull_request'
  37. || github.event.pull_request.head.repo.full_name != github.repository
  38. outputs:
  39. roles: ${{ steps.detect.outputs.roles }}
  40. steps:
  41. - name: Check out
  42. uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
  43. with:
  44. fetch-depth: 0
  45. - name: Detect roles with a Molecule scenario that this change touches
  46. id: detect
  47. env:
  48. EVENT_NAME: ${{ github.event_name }}
  49. BASE_SHA: ${{ github.event.pull_request.base.sha }}
  50. BEFORE_SHA: ${{ github.event.before }}
  51. INPUT_ROLE: ${{ inputs.role }}
  52. run: |
  53. set -euo pipefail
  54. have_scenario() {
  55. [ -f "roles/custom/$1/molecule/default/molecule.yml" ]
  56. }
  57. # An explicit request through workflow_dispatch wins over detection.
  58. if [ -n "${INPUT_ROLE}" ]; then
  59. if have_scenario "${INPUT_ROLE}"; then
  60. printf 'roles=["%s"]\n' "${INPUT_ROLE}" >> "$GITHUB_OUTPUT"
  61. else
  62. echo "No scenario at roles/custom/${INPUT_ROLE}/molecule/default" >&2
  63. exit 1
  64. fi
  65. exit 0
  66. fi
  67. # A hand-triggered run with no role named, and any run where the diff
  68. # base is unusable (a new branch, a force push, the very first commit),
  69. # falls back to every role that has a scenario. That is the safe
  70. # direction to fail in: too much testing rather than too little.
  71. base=""
  72. case "${EVENT_NAME}" in
  73. pull_request) base="${BASE_SHA}" ;;
  74. push)
  75. if [ -n "${BEFORE_SHA}" ] && [ "${BEFORE_SHA}" != "0000000000000000000000000000000000000000" ] \
  76. && git cat-file -e "${BEFORE_SHA}^{commit}" 2>/dev/null; then
  77. base="${BEFORE_SHA}"
  78. fi
  79. ;;
  80. esac
  81. if [ -n "${base}" ]; then
  82. changed="$(git diff --name-only "${base}" HEAD -- 'roles/custom/*' || true)"
  83. candidates="$(printf '%s\n' "${changed}" | awk -F/ 'NF>2 {print $3}' | sort -u)"
  84. echo "Changed roles: ${candidates:-none}"
  85. else
  86. candidates="$(find roles/custom -mindepth 1 -maxdepth 1 -type d -printf '%f\n' | sort)"
  87. echo "No usable diff base; considering every role"
  88. fi
  89. selected=""
  90. for role in ${candidates}; do
  91. if have_scenario "${role}"; then
  92. selected="${selected} ${role}"
  93. fi
  94. done
  95. if [ -z "${selected}" ]; then
  96. echo "Nothing to test"
  97. echo 'roles=[]' >> "$GITHUB_OUTPUT"
  98. exit 0
  99. fi
  100. echo "Testing:${selected}"
  101. json="$(printf '%s\n' ${selected} | jq -R . | jq -c -s .)"
  102. echo "roles=${json}" >> "$GITHUB_OUTPUT"
  103. molecule:
  104. name: "Molecule: ${{ matrix.role }}"
  105. runs-on: ubuntu-latest
  106. needs: detect
  107. if: needs.detect.outputs.roles != '[]'
  108. strategy:
  109. matrix:
  110. role: ${{ fromJson(needs.detect.outputs.roles) }}
  111. fail-fast: false
  112. env:
  113. MOLECULE_DISTRO: ubuntu2604
  114. PY_COLORS: "1"
  115. ANSIBLE_FORCE_COLOR: "1"
  116. steps:
  117. - name: Check out
  118. uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
  119. - name: Set up Python
  120. uses: actions/setup-python@v6
  121. with:
  122. python-version: "3.x"
  123. - name: Install test dependencies
  124. run: python3 -m pip install -r molecule-shared/requirements.txt
  125. - name: Run Molecule
  126. working-directory: roles/custom/${{ matrix.role }}
  127. run: molecule test --scenario-name default