|
| 1 | +name: Docker |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + paths: |
| 6 | + - .github/workflows/build_test_containers.yml |
| 7 | + - ansible/docker/test/Dockerfile* |
| 8 | + branches: |
| 9 | + - master |
| 10 | + push: |
| 11 | + paths: |
| 12 | + - .github/workflows/build_test_containers.yml |
| 13 | + - ansible/docker/test/Dockerfile* |
| 14 | + branches: |
| 15 | + - master |
| 16 | + |
| 17 | +permissions: |
| 18 | + contents: read |
| 19 | + packages: write |
| 20 | + |
| 21 | +jobs: |
| 22 | + generate-matrix: |
| 23 | + name: Generate Matrix |
| 24 | + runs-on: ubuntu-latest |
| 25 | + if: github.repository_owner == 'adoptium' |
| 26 | + outputs: |
| 27 | + matrix: ${{ steps.generate_matrix.outputs.matrix }} |
| 28 | + steps: |
| 29 | + - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 |
| 30 | + with: |
| 31 | + fetch-depth: 0 |
| 32 | + |
| 33 | + # Get list of changed files in ansible/docker/test/Dockerfile* |
| 34 | + - name: Get list of changed Dockerfiles |
| 35 | + id: get_changed_files |
| 36 | + run: | |
| 37 | + changed_files=$(git diff --name-only ${{ github.event.before }} ${{ github.sha }} | grep ansible/docker/test/Dockerfile) |
| 38 | + echo "changed_files=$changed_files" >> "$GITHUB_OUTPUT" |
| 39 | +
|
| 40 | + # Generate matrix |
| 41 | + - name: Generate matrix |
| 42 | + id: generate_matrix |
| 43 | + run: | |
| 44 | + matrix=$(jq -n --arg files "$changed_files" '{ |
| 45 | + include: ($files | split("\n") | map(select(length > 0) | {dockerfile: .})) |
| 46 | + }') |
| 47 | + echo "matrix=$matrix" >> "$GITHUB_OUTPUT" |
| 48 | +
|
| 49 | + build-dockerfiles: |
| 50 | + name: Build Dockerfiles |
| 51 | + runs-on: ubuntu-latest |
| 52 | + needs: generate-matrix |
| 53 | + strategy: |
| 54 | + matrix: ${{fromJson(needs.generate-matrix.outputs.matrix)}} |
| 55 | + steps: |
| 56 | + - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 |
| 57 | + |
| 58 | + # Generate tag name based on the Dockerfile name |
| 59 | + - name: Set tag name |
| 60 | + run: echo "TAG_NAME=$(basename ${{ matrix.dockerfile }} | cut -d'.' -f2 | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV |
| 61 | + |
| 62 | + - name: Build Dockerfile |
| 63 | + run: docker build -t ghcr.io/${{ github.repository_owner }}/test-containers:$TAG_NAME -f ${{ matrix.dockerfile }} . |
| 64 | + |
| 65 | + - name: Push Dockerfile to ghcr.io |
| 66 | + if: github.event_name == 'push' |
| 67 | + run: | |
| 68 | + echo ${{ secrets.GITHUB_TOKEN }} | docker login ghcr.io -u ${{ github.repository_owner }} --password-stdin |
| 69 | + docker push ghcr.io/${{ github.repository_owner }}/test-containers:$TAG_NAME |
0 commit comments