-
Notifications
You must be signed in to change notification settings - Fork 4
120 lines (112 loc) · 4.38 KB
/
molecule_playbooks.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
---
name: Molecule Playbooks
on:
push:
branches:
- main
pull_request:
branches:
- main
types:
- opened
- reopened
- synchronize
- ready_for_review
jobs:
define_scenarios:
if: github.event.pull_request.draft == false
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Get changed scenarios
id: changed-scenarios
uses: tj-actions/changed-files@v41
with:
dir_names: true
dir_names_max_depth: 2
files: |
molecule/playbook-*/*.yml
- name: Get changed playbooks
id: changed-playbooks
uses: tj-actions/changed-files@v41
with:
files: |
playbooks/*.yml
- name: List all changed files
run: echo '${{ steps.changed-playbooks.outputs.all_changed_files }}' '${{ steps.changed-scenarios.outputs.all_changed_files }}'
- name: Set matrix based on changed files
id: set-scenario-matrix
run: |
PLAYBOOKS=(${{steps.changed-playbooks.outputs.all_changed_files}})
PLAYBOOKS=("${PLAYBOOKS[@]/#playbooks\//playbook-}") # playbooks/foo.yml -> playbook-foo.yml
PLAYBOOKS=("${PLAYBOOKS[@]/%.yml/}") # playbook-foo.yml -> playbook-foo
echo "DEBUG PLAYBOOKS: ${PLAYBOOKS[*]}"
SCENARIOS=(${{ steps.changed-scenarios.outputs.all_changed_files }})
SCENARIOS=("${SCENARIOS[@]/#molecule\//}") # molecule/playbook-foo -> playbook-foo
echo "DEBUG SCENARIOS: ${SCENARIOS[*]}"
ALL_CHANGES="${PLAYBOOKS[*]} ${SCENARIOS[*]}"
echo "DEBUG ALL_CHANGES: $ALL_CHANGES"
# Construct a unique bash array out of ALL_CHANGES
MATRIX=($(echo "$ALL_CHANGES" | xargs -n 1 | sort -u))
# Create a valid json array out of the bash array using jq
MATRIX=$(printf '%s\n' "${MATRIX[@]}" | jq -R . | jq -c -s .)
# Store the results
echo matrix={\"scenarios\": $MATRIX} >> $GITHUB_OUTPUT
echo scenarios=$MATRIX >> $GITHUB_OUTPUT
echo "DEBUG SCENARIOS: $MATRIX"
outputs:
matrix: ${{ steps.set-scenario-matrix.outputs.matrix }}
scenarios: ${{ steps.set-scenario-matrix.outputs.scenarios }}
molecule:
needs: define_scenarios
runs-on: ubuntu-latest
if: ${{ needs.define_scenarios.outputs.scenarios != '[""]' && needs.define_scenarios.outputs.scenarios != '' }}
env:
DOCKER_USER: ${{ github.actor }}
DOCKER_PW: ${{ secrets.GITHUB_TOKEN }}
DOCKER_REGISTRY: ghcr.io
ANSIBLE_FORCE_COLOR: '1'
ANSIBLE_STDOUT_CALLBACK: yaml
MOLECULE_CONFIG: molecule/ext/molecule-src/molecule.yml
REQUIREMENTS_FILE: molecule/ext/molecule-src/requirements.txt
ANSIBLE_REQUIREMENTS_FILE: molecule/ext/molecule-src/requirements.yml
CRUN_VER: 1.11.2
strategy:
fail-fast: false
matrix: ${{ fromJSON(needs.define_scenarios.outputs.matrix) }}
steps:
- name: Workaround crun issue on ubuntu # https://github.com/UtrechtUniversity/SRC-catalog-items/issues/3
run: |
mkdir -p "${HOME}/.local/bin"
curl -L "https://github.com/containers/crun/releases/download/${CRUN_VER}/crun-${CRUN_VER}-linux-amd64" -o "${HOME}/.local/bin/crun"
chmod +x "${HOME}/.local/bin/crun"
crun --version
mkdir -p "${HOME}/.config/containers"
cat << EOF > "${HOME}/.config/containers/containers.conf"
[engine.runtimes]
crun = [
"${HOME}/.local/bin/crun",
"/usr/bin/crun"
]
EOF
- name: Checkout
uses: actions/checkout@v4
- uses: actions/setup-python@v4
with:
python-version: '3.11'
- run: pip install -r ${{ env.REQUIREMENTS_FILE }}
- run: ansible-galaxy install -r ${{ env.ANSIBLE_REQUIREMENTS_FILE }}
- run: ansible-galaxy install -r requirements.yml
- name: Check if scenario exists
id: check_scenario_exists
run: |
if [[ -d molecule/${{ matrix.scenarios }} ]]; then
echo "exitcode=true" >> $GITHUB_OUTPUT
else
echo "exitcode=false" >> $GITHUB_OUTPUT
fi
- name: Molecule tests for playbook
if: ${{ steps.check_scenario_exists.outputs.exitcode == 'true' }}
run: |
molecule -c ${{ env.MOLECULE_CONFIG }} test --scenario-name ${{ matrix.scenarios }}