|
| 1 | +name: Post |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + workflow_dispatch: {} |
| 8 | + |
| 9 | +concurrency: |
| 10 | + group: post-${{ github.ref }} |
| 11 | + cancel-in-progress: true |
| 12 | + |
| 13 | +jobs: |
| 14 | + generate-matrix: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + permissions: |
| 17 | + contents: read |
| 18 | + id-token: write |
| 19 | + outputs: |
| 20 | + matrix: ${{ steps.generate.outputs.matrix }} |
| 21 | + repositories: ${{ steps.repo-matrix.outputs.repositories }} |
| 22 | + artifacts: ${{ steps.generate.outputs.artifacts }} |
| 23 | + steps: |
| 24 | + - name: Checkout |
| 25 | + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 |
| 26 | + with: |
| 27 | + fetch-depth: 0 |
| 28 | + ref: ${{ github.sha }} |
| 29 | + - name: Compute diff refs |
| 30 | + id: compute-refs |
| 31 | + run: | |
| 32 | + set -e |
| 33 | +
|
| 34 | + BASE_SHA=${{ github.event.before }} |
| 35 | + HEAD_SHA=${{ github.sha }} |
| 36 | +
|
| 37 | + # Ensure we have both the base and head commits |
| 38 | + git fetch --depth=1 origin $BASE_SHA |
| 39 | + git fetch --depth=1 origin $HEAD_SHA |
| 40 | + echo "BASE_SHA=$BASE_SHA" >> $GITHUB_ENV |
| 41 | + echo "HEAD_SHA=$HEAD_SHA" >> $GITHUB_ENV |
| 42 | +
|
| 43 | + # echo "Diff will compare $BASE_SHA with $HEAD_SHA" |
| 44 | + echo "Diff will compare main ($BASE_SHA) with $HEAD_SHA" |
| 45 | + - name: Generate repository matrix |
| 46 | + id: repo-matrix |
| 47 | + uses: ./.github/actions/generate-repo-matrix |
| 48 | + with: |
| 49 | + repoRoot: "." |
| 50 | + - name: Build changed project matrix |
| 51 | + id: generate |
| 52 | + run: | |
| 53 | + set -e |
| 54 | +
|
| 55 | + # Get repository names and check for changes |
| 56 | + repositories_json='${{ steps.repo-matrix.outputs.repositories }}' |
| 57 | + changed_repos=() |
| 58 | +
|
| 59 | + # Check each repository for changes |
| 60 | + while IFS= read -r repo; do |
| 61 | + if git diff --name-only $BASE_SHA $HEAD_SHA | grep "^$repo/" > /dev/null 2>&1; then |
| 62 | + changed_repos+=("$repo") |
| 63 | + fi |
| 64 | + done < <(echo "$repositories_json" | jq -r 'keys[]') |
| 65 | +
|
| 66 | + if [ ${#changed_repos[@]} -eq 0 ]; then |
| 67 | + echo "No changes detected for any project" |
| 68 | + echo "matrix=" >> $GITHUB_OUTPUT |
| 69 | + echo "artifacts={}" >> $GITHUB_OUTPUT |
| 70 | + else |
| 71 | + # Convert changed repos array to JSON |
| 72 | + changed_repos_json=$(printf '%s\n' "${changed_repos[@]}" | jq -R . | jq -s .) |
| 73 | + matrixJson=$(echo "$changed_repos_json" | jq -c '{"repo": .}') |
| 74 | +
|
| 75 | + # Generate artifacts JSON with workspace prefix |
| 76 | + artifacts_json=$( |
| 77 | + echo "$repositories_json" | jq -c \ |
| 78 | + --argjson changed_repos "$changed_repos_json" \ |
| 79 | + --arg workspace "${{ github.workspace }}" \ |
| 80 | + ' |
| 81 | + to_entries |
| 82 | + | map(select(.key as $repo | $changed_repos | index($repo) != null)) |
| 83 | + | from_entries |
| 84 | + | with_entries( |
| 85 | + .key as $repo |
| 86 | + | .value = ( |
| 87 | + .value["e2e-artifacts"] |
| 88 | + | map($workspace + "/" + $repo + "/" + .) |
| 89 | + | join("\n") |
| 90 | + ) |
| 91 | + ) |
| 92 | + ' |
| 93 | + ) |
| 94 | +
|
| 95 | + echo "matrix=$matrixJson" >> $GITHUB_OUTPUT |
| 96 | + echo "artifacts=$artifacts_json" >> $GITHUB_OUTPUT |
| 97 | + fi |
| 98 | +
|
| 99 | + echo "Matrix: $matrixJson" |
| 100 | + echo "Artifacts: $artifacts_json" |
| 101 | +
|
| 102 | + images: |
| 103 | + name: Build and Push Images |
| 104 | + needs: generate-matrix |
| 105 | + if: needs.generate-matrix.outputs.matrix != '' |
| 106 | + runs-on: ubuntu-latest |
| 107 | + strategy: |
| 108 | + fail-fast: false |
| 109 | + matrix: |
| 110 | + repo: ${{ fromJson(needs.generate-matrix.outputs.matrix).repo }} |
| 111 | + arch: [amd64, arm64] |
| 112 | + steps: |
| 113 | + - name: Checkout code |
| 114 | + uses: actions/checkout@v4 |
| 115 | + - name: install Go |
| 116 | + uses: actions/setup-go@v5 |
| 117 | + with: |
| 118 | + go-version: '1.23' |
| 119 | + - name: install imagebuilder |
| 120 | + run: go install github.com/openshift/imagebuilder/cmd/imagebuilder@v1.2.16 |
| 121 | + - name: pull base image |
| 122 | + run: docker pull registry.access.redhat.com/ubi9/ubi-minimal:latest --platform=linux/${{ matrix.arch }} |
| 123 | + - name: build image |
| 124 | + run: | |
| 125 | + IMAGE_TAG=latest-${{ matrix.arch }} \ |
| 126 | + IMAGE_BUILD_EXTRA_FLAGS="--build-arg OS=linux --build-arg ARCH=${{ matrix.arch }}" \ |
| 127 | + cd ${{ matrix.repo }} && make image |
| 128 | + - name: push image |
| 129 | + run: | |
| 130 | + echo ${{ secrets.DOCKER_PASSWORD }} | docker login quay.io --username ${{ secrets.DOCKER_USER }} --password-stdin |
| 131 | + IMAGE_TAG=latest-${{ matrix.arch }} \ |
| 132 | + cd ${{ matrix.repo }} && make image-push |
| 133 | +
|
| 134 | + image-manifest: |
| 135 | + name: Create and Push Image Manifest |
| 136 | + needs: [images, generate-matrix] |
| 137 | + runs-on: ubuntu-latest |
| 138 | + strategy: |
| 139 | + fail-fast: false |
| 140 | + matrix: |
| 141 | + repo: ${{ fromJson(needs.generate-matrix.outputs.matrix).repo }} |
| 142 | + steps: |
| 143 | + - name: Checkout code |
| 144 | + uses: actions/checkout@v4 |
| 145 | + - name: create and push manifest |
| 146 | + run: | |
| 147 | + echo ${{ secrets.DOCKER_PASSWORD }} | docker login quay.io --username ${{ secrets.DOCKER_USER }} --password-stdin |
| 148 | + IMAGE_TAG=latest \ |
| 149 | + cd ${{ matrix.repo }} && make image-manifest |
0 commit comments