|
| 1 | +name: Build container image |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + pull_request: |
| 6 | + push: |
| 7 | + branches: |
| 8 | + - main |
| 9 | + |
| 10 | +concurrency: |
| 11 | + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} |
| 12 | + cancel-in-progress: true |
| 13 | + |
| 14 | +env: |
| 15 | + # Use docker.io for Docker Hub if empty |
| 16 | + REGISTRY: ghcr.io |
| 17 | + # github.repository as <account>/<repo> |
| 18 | + IMAGE_NAME: ${{ github.repository }} |
| 19 | + |
| 20 | + |
| 21 | +jobs: |
| 22 | + test: |
| 23 | + name: Run Tests |
| 24 | + runs-on: ubuntu-latest |
| 25 | + steps: |
| 26 | + - name: Checkout repository |
| 27 | + uses: actions/checkout@v6 |
| 28 | + |
| 29 | + - name: Set up Go |
| 30 | + uses: actions/setup-go@v6 |
| 31 | + with: |
| 32 | + go-version-file: 'go.mod' |
| 33 | + |
| 34 | + - name: Install kind |
| 35 | + uses: helm/kind-action@v1 |
| 36 | + with: |
| 37 | + install_only: true |
| 38 | + |
| 39 | + - name: Run all tests |
| 40 | + run: make test |
| 41 | + |
| 42 | + build: |
| 43 | + needs: test |
| 44 | + runs-on: ubuntu-latest |
| 45 | + permissions: |
| 46 | + contents: read |
| 47 | + packages: write |
| 48 | + |
| 49 | + steps: |
| 50 | + - name: Checkout repository |
| 51 | + uses: actions/checkout@v6 |
| 52 | + |
| 53 | + # Login against a Docker registry except on PR |
| 54 | + # https://github.com/docker/login-action |
| 55 | + - name: Log into registry ${{ env.REGISTRY }} |
| 56 | + if: github.event_name != 'pull_request' |
| 57 | + uses: docker/login-action@v4 |
| 58 | + with: |
| 59 | + registry: ${{ env.REGISTRY }} |
| 60 | + username: ${{ github.actor }} |
| 61 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 62 | + |
| 63 | + # Extract metadata (tags, labels) for Docker |
| 64 | + # https://github.com/docker/metadata-action |
| 65 | + - name: Extract Docker metadata |
| 66 | + id: meta |
| 67 | + uses: docker/metadata-action@v6 |
| 68 | + with: |
| 69 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 70 | + tags: | |
| 71 | + type=semver,pattern=v{{version}} |
| 72 | + type=semver,pattern=v{{major}}.{{minor}} |
| 73 | + type=semver,pattern=v{{major}} |
| 74 | + type=ref,event=branch |
| 75 | + type=ref,event=pr |
| 76 | + type=ref,event=tag |
| 77 | + type=sha |
| 78 | + type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'main') }} |
| 79 | +
|
| 80 | +
|
| 81 | + # Build and push Docker image with Buildx (don't push on PR) |
| 82 | + # https://github.com/docker/build-push-action |
| 83 | + - name: Build and push Docker image |
| 84 | + id: build-operator |
| 85 | + uses: docker/build-push-action@v7 |
| 86 | + with: |
| 87 | + context: . |
| 88 | + file: Containerfile |
| 89 | + push: ${{ github.event_name != 'pull_request' }} |
| 90 | + tags: ${{ steps.meta.outputs.tags }} |
| 91 | + labels: ${{ steps.meta.outputs.labels }} |
| 92 | + |
| 93 | + # Extract the commit SHA tag for manifest generation |
| 94 | + - name: Extract commit SHA tag |
| 95 | + id: sha-tag |
| 96 | + if: github.event_name != 'pull_request' |
| 97 | + run: | |
| 98 | + # Extract the sha tag from the metadata output |
| 99 | + FULL_SHA_TAG=$(echo "${{ steps.meta.outputs.tags }}" | grep -E "sha-[0-9a-f]{7}" | head -n1) |
| 100 | + SHA_TAG=$(echo "$FULL_SHA_TAG" | sed "s/.*://") |
| 101 | + echo "sha-tag=$SHA_TAG" >> $GITHUB_OUTPUT |
| 102 | + echo "full-sha-tag=$FULL_SHA_TAG" >> $GITHUB_OUTPUT |
| 103 | + echo "manifest-tag=$SHA_TAG-manifests" >> $GITHUB_OUTPUT |
| 104 | + echo "Using SHA tag: $SHA_TAG" |
| 105 | + echo "Full SHA tag: $FULL_SHA_TAG" |
| 106 | + echo "Manifest tag: $SHA_TAG-manifests" |
| 107 | +
|
| 108 | + # Set up Go for kustomize operations |
| 109 | + - name: Set up Go for manifest generation |
| 110 | + if: github.event_name != 'pull_request' |
| 111 | + uses: actions/setup-go@v6 |
| 112 | + with: |
| 113 | + go-version-file: 'go.mod' |
| 114 | + |
| 115 | + # Generate manifests with kustomize pointing to the SHA-tagged image |
| 116 | + - name: Generate manifests with kustomize |
| 117 | + if: github.event_name != 'pull_request' |
| 118 | + run: | |
| 119 | + # Install kustomize |
| 120 | + make kustomize |
| 121 | +
|
| 122 | + # Create dist directory |
| 123 | + mkdir -p dist |
| 124 | +
|
| 125 | + # Set the manager image to the SHA-tagged version |
| 126 | + cd config/manager && ../../bin/kustomize edit set image controller=${{ steps.sha-tag.outputs.full-sha-tag }} |
| 127 | +
|
| 128 | + # Generate the complete manifest |
| 129 | + cd ../.. |
| 130 | + ./bin/kustomize build config/default > dist/install.yaml |
| 131 | +
|
| 132 | + echo "Generated manifest with image: ${{ steps.sha-tag.outputs.full-sha-tag }}" |
| 133 | + echo "Manifest content preview:" |
| 134 | + head -20 dist/install.yaml |
| 135 | +
|
| 136 | + # Create Dockerfile for manifest container |
| 137 | + - name: Create manifest container Dockerfile |
| 138 | + if: github.event_name != 'pull_request' |
| 139 | + run: | |
| 140 | + cat > Dockerfile.manifests << 'EOF' |
| 141 | + FROM scratch |
| 142 | + COPY dist/install.yaml /manifests/install.yaml |
| 143 | + EOF |
| 144 | +
|
| 145 | + echo "Created Dockerfile.manifests:" |
| 146 | + cat Dockerfile.manifests |
| 147 | +
|
| 148 | + # Extract metadata for manifest container |
| 149 | + - name: Extract manifest container metadata |
| 150 | + id: meta-manifests |
| 151 | + if: github.event_name != 'pull_request' |
| 152 | + uses: docker/metadata-action@v6 |
| 153 | + with: |
| 154 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 155 | + tags: | |
| 156 | + type=raw,value=${{ steps.sha-tag.outputs.manifest-tag }} |
| 157 | +
|
| 158 | + # Build and push manifest container |
| 159 | + - name: Build and push manifest container |
| 160 | + if: github.event_name != 'pull_request' |
| 161 | + uses: docker/build-push-action@v7 |
| 162 | + with: |
| 163 | + context: . |
| 164 | + file: Dockerfile.manifests |
| 165 | + push: true |
| 166 | + tags: ${{ steps.meta-manifests.outputs.tags }} |
| 167 | + labels: ${{ steps.meta-manifests.outputs.labels }} |
0 commit comments