|
11 | 11 | REGISTRY: ghcr.io |
12 | 12 |
|
13 | 13 | jobs: |
14 | | - setup: |
| 14 | + prepare: |
15 | 15 | runs-on: ubuntu-latest |
16 | 16 | outputs: |
17 | | - matrix: ${{ steps.set-matrix.outputs.matrix }} |
18 | | - filters: ${{ steps.set-matrix.outputs.filters }} |
| 17 | + matrix: ${{ steps.generate-matrix.outputs.matrix }} |
19 | 18 | steps: |
20 | 19 | - uses: actions/checkout@v4 |
21 | | - - id: set-matrix |
| 20 | + |
| 21 | + - id: set-filters |
22 | 22 | run: | |
23 | 23 | python3 -c " |
24 | | - import yaml, json, os |
| 24 | + import yaml, os |
25 | 25 | with open('config.yml', 'r') as f: |
26 | 26 | config = yaml.safe_load(f) |
27 | 27 | images = config.get('images', []) |
28 | | - filters = {} |
29 | | - for img in images: |
30 | | - path = img['context'] |
31 | | - if path.startswith('./'): path = path[2:] |
32 | | - filters[img['name']] = [f'{path}/**'] |
| 28 | + filters = {img['name']: [f\"{img['context'].lstrip('./')}/**\"] for img in images} |
33 | 29 | with open(os.environ['GITHUB_OUTPUT'], 'a') as f: |
34 | | - f.write(f'matrix={json.dumps(images)}\n') |
35 | 30 | f.write('filters<<EOF\n') |
36 | 31 | f.write(yaml.dump(filters)) |
37 | 32 | f.write('EOF\n') |
38 | 33 | " |
39 | 34 |
|
40 | | - filter-changes: |
41 | | - needs: setup |
42 | | - runs-on: ubuntu-latest |
43 | | - outputs: |
44 | | - changes: ${{ steps.filter.outputs.changes }} |
45 | | - steps: |
46 | | - - uses: actions/checkout@v4 |
47 | 35 | - uses: dorny/paths-filter@v3 |
48 | 36 | id: filter |
49 | 37 | with: |
50 | | - filters: ${{ needs.setup.outputs.filters }} |
| 38 | + filters: ${{ steps.set-filters.outputs.filters }} |
| 39 | + |
| 40 | + - id: generate-matrix |
| 41 | + run: | |
| 42 | + python3 -c " |
| 43 | + import yaml, json, os |
| 44 | + with open('config.yml', 'r') as f: |
| 45 | + config = yaml.safe_load(f) |
| 46 | + images = config.get('images', []) |
| 47 | + |
| 48 | + changes = json.loads(os.environ.get('CHANGES', '[]')) |
| 49 | + event = os.environ.get('EVENT_NAME') |
| 50 | + ref_type = os.environ.get('REF_TYPE') |
| 51 | + is_build_all = event in ['schedule', 'workflow_dispatch'] or ref_type == 'tag' |
| 52 | + |
| 53 | + if is_build_all: |
| 54 | + matrix = images |
| 55 | + else: |
| 56 | + matrix = [img for img in images if img['name'] in changes] |
| 57 | + |
| 58 | + with open(os.environ['GITHUB_OUTPUT'], 'a') as f: |
| 59 | + f.write(f'matrix={json.dumps(matrix)}\n') |
| 60 | + " |
| 61 | + env: |
| 62 | + CHANGES: ${{ steps.filter.outputs.changes }} |
| 63 | + EVENT_NAME: ${{ github.event_name }} |
| 64 | + REF_TYPE: ${{ github.ref_type }} |
51 | 65 |
|
52 | 66 | build: |
53 | 67 | name: Build ${{ matrix.name }} Dockerfile and Push |
54 | | - needs: [setup, filter-changes] |
55 | | - if: | |
56 | | - github.event_name == 'schedule' || |
57 | | - github.event_name == 'workflow_dispatch' || |
58 | | - github.ref_type == 'tag' || |
59 | | - contains(fromJSON(needs.filter-changes.outputs.changes), matrix.name) |
| 68 | + needs: prepare |
| 69 | + if: needs.prepare.outputs.matrix != '[]' |
60 | 70 | strategy: |
61 | 71 | fail-fast: false |
62 | 72 | matrix: |
63 | | - include: ${{ fromJSON(needs.setup.outputs.matrix) }} |
| 73 | + include: ${{ fromJSON(needs.prepare.outputs.matrix) }} |
64 | 74 | runs-on: ubuntu-latest |
65 | 75 | permissions: |
66 | 76 | contents: read |
|
0 commit comments