|
| 1 | +name: Docker Build - PR Validation |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: [master, main] |
| 6 | + paths: |
| 7 | + - 'Dockerfile' |
| 8 | + - '.dockerignore' |
| 9 | + - 'docker-build.sh' |
| 10 | + - 'docker-run.sh' |
| 11 | + - 'subscriber.go' |
| 12 | + - 'go.mod' |
| 13 | + - 'go.sum' |
| 14 | + - 'Makefile' |
| 15 | + - '.github/workflows/docker-build-pr.yml' |
| 16 | + |
| 17 | +env: |
| 18 | + IMAGE_NAME: pubsub-sub-bench-pr |
| 19 | + |
| 20 | +jobs: |
| 21 | + docker-build-test: |
| 22 | + runs-on: ubuntu-latest |
| 23 | + permissions: |
| 24 | + contents: read |
| 25 | + pull-requests: write |
| 26 | + |
| 27 | + steps: |
| 28 | + - name: Checkout repository |
| 29 | + uses: actions/checkout@v4 |
| 30 | + with: |
| 31 | + fetch-depth: 0 # Fetch full history for Git info |
| 32 | + |
| 33 | + - name: Set up Docker Buildx |
| 34 | + uses: docker/setup-buildx-action@v3 |
| 35 | + |
| 36 | + - name: Extract Git metadata |
| 37 | + id: meta |
| 38 | + run: | |
| 39 | + GIT_SHA=$(git rev-parse HEAD) |
| 40 | + GIT_DIRTY=$(git diff --no-ext-diff 2>/dev/null | wc -l) |
| 41 | + echo "git_sha=${GIT_SHA}" >> $GITHUB_OUTPUT |
| 42 | + echo "git_dirty=${GIT_DIRTY}" >> $GITHUB_OUTPUT |
| 43 | + echo "short_sha=${GIT_SHA:0:7}" >> $GITHUB_OUTPUT |
| 44 | +
|
| 45 | + - name: Check Docker Hub credentials |
| 46 | + id: check_credentials |
| 47 | + run: | |
| 48 | + if [[ -n "${{ secrets.DOCKER_USERNAME }}" && -n "${{ secrets.DOCKER_PASSWORD }}" ]]; then |
| 49 | + echo "credentials_available=true" >> $GITHUB_OUTPUT |
| 50 | + echo "✅ Docker Hub credentials are configured" |
| 51 | + else |
| 52 | + echo "credentials_available=false" >> $GITHUB_OUTPUT |
| 53 | + echo "⚠️ Docker Hub credentials not configured (DOCKER_USERNAME and/or DOCKER_PASSWORD secrets missing)" |
| 54 | + echo "This is expected for forks and external PRs. Docker build validation will still work." |
| 55 | + fi |
| 56 | +
|
| 57 | + - name: Build Docker image (single platform) |
| 58 | + uses: docker/build-push-action@v5 |
| 59 | + with: |
| 60 | + context: . |
| 61 | + platforms: linux/amd64 |
| 62 | + push: false |
| 63 | + load: true |
| 64 | + tags: ${{ env.IMAGE_NAME }}:pr-${{ github.event.number }} |
| 65 | + build-args: | |
| 66 | + GIT_SHA=${{ steps.meta.outputs.git_sha }} |
| 67 | + GIT_DIRTY=${{ steps.meta.outputs.git_dirty }} |
| 68 | + cache-from: type=gha |
| 69 | + cache-to: type=gha,mode=max |
| 70 | + |
| 71 | + - name: Test Docker image |
| 72 | + run: | |
| 73 | + echo "Testing Docker image functionality..." |
| 74 | +
|
| 75 | + # Verify image was built |
| 76 | + if docker images | grep -q "${{ env.IMAGE_NAME }}"; then |
| 77 | + echo "✅ Docker image built successfully" |
| 78 | + else |
| 79 | + echo "❌ Docker image not found" |
| 80 | + exit 1 |
| 81 | + fi |
| 82 | +
|
| 83 | + # Test help command |
| 84 | + echo "Testing --help command..." |
| 85 | + docker run --rm ${{ env.IMAGE_NAME }}:pr-${{ github.event.number }} --help |
| 86 | +
|
| 87 | + # Test version output |
| 88 | + echo "Testing --version command..." |
| 89 | + docker run --rm ${{ env.IMAGE_NAME }}:pr-${{ github.event.number }} --version |
| 90 | +
|
| 91 | + echo "✅ Docker image tests passed!" |
| 92 | +
|
| 93 | + - name: Build multi-platform image (validation only) |
| 94 | + uses: docker/build-push-action@v5 |
| 95 | + with: |
| 96 | + context: . |
| 97 | + platforms: linux/amd64,linux/arm64 |
| 98 | + push: false |
| 99 | + tags: ${{ env.IMAGE_NAME }}:pr-${{ github.event.number }}-multiplatform |
| 100 | + build-args: | |
| 101 | + GIT_SHA=${{ steps.meta.outputs.git_sha }} |
| 102 | + GIT_DIRTY=${{ steps.meta.outputs.git_dirty }} |
| 103 | + cache-from: type=gha |
| 104 | + cache-to: type=gha,mode=max |
| 105 | + |
| 106 | + - name: Generate PR comment |
| 107 | + if: github.event_name == 'pull_request' |
| 108 | + uses: actions/github-script@v7 |
| 109 | + with: |
| 110 | + script: | |
| 111 | + const credentialsStatus = '${{ steps.check_credentials.outputs.credentials_available }}' === 'true' |
| 112 | + ? '✅ Docker Hub credentials configured' |
| 113 | + : '⚠️ Docker Hub credentials not configured (expected for forks)'; |
| 114 | +
|
| 115 | + const output = `## 🐳 Docker Build Validation |
| 116 | +
|
| 117 | + ✅ **Docker build successful!** |
| 118 | +
|
| 119 | + **Platforms tested:** |
| 120 | + - ✅ linux/amd64 (built and tested) |
| 121 | + - ✅ linux/arm64 (build validated) |
| 122 | +
|
| 123 | + **Git SHA:** \`${{ steps.meta.outputs.git_sha }}\` |
| 124 | +
|
| 125 | + **Docker Hub Status:** ${credentialsStatus} |
| 126 | +
|
| 127 | + **Image details:** |
| 128 | + - Single platform: \`${{ env.IMAGE_NAME }}:pr-${{ github.event.number }}\` |
| 129 | + - Multi-platform: \`${{ env.IMAGE_NAME }}:pr-${{ github.event.number }}-multiplatform\` |
| 130 | +
|
| 131 | + **Tests performed:** |
| 132 | + - ✅ Docker Hub credentials check |
| 133 | + - ✅ Help command execution |
| 134 | + - ✅ Version output validation |
| 135 | + - ✅ Multi-platform build validation |
| 136 | +
|
| 137 | + The Docker image is ready for deployment! 🚀`; |
| 138 | +
|
| 139 | + github.rest.issues.createComment({ |
| 140 | + issue_number: context.issue.number, |
| 141 | + owner: context.repo.owner, |
| 142 | + repo: context.repo.repo, |
| 143 | + body: output |
| 144 | + }); |
| 145 | +
|
| 146 | + - name: Clean up test images |
| 147 | + if: always() |
| 148 | + run: | |
| 149 | + docker rmi ${{ env.IMAGE_NAME }}:pr-${{ github.event.number }} || true |
| 150 | + echo "Cleanup completed" |
0 commit comments