|
| 1 | +name: Build, Test, and Release |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ main ] |
| 6 | + tags: [ 'v*' ] |
| 7 | + pull_request: |
| 8 | + branches: [ main ] |
| 9 | + release: |
| 10 | + types: [ published ] |
| 11 | + workflow_dispatch: |
| 12 | + inputs: |
| 13 | + push: |
| 14 | + description: "Actually push image to GHCR" |
| 15 | + type: boolean |
| 16 | + default: false |
| 17 | + tag: |
| 18 | + description: "Optional tag to apply when testing (e.g., test-123)" |
| 19 | + type: string |
| 20 | + required: false |
| 21 | + |
| 22 | +jobs: |
| 23 | + build-test: |
| 24 | + name: Build and Test |
| 25 | + runs-on: ubuntu-latest |
| 26 | + steps: |
| 27 | + - name: Checkout code |
| 28 | + uses: actions/checkout@v4 |
| 29 | + with: |
| 30 | + fetch-depth: 0 # Required for version determination from git tags |
| 31 | + |
| 32 | + - name: Set up Go |
| 33 | + uses: actions/setup-go@v5 |
| 34 | + with: |
| 35 | + go-version: '1.21' |
| 36 | + check-latest: true |
| 37 | + cache: true |
| 38 | + |
| 39 | + - name: Install dependencies |
| 40 | + run: make mod-download |
| 41 | + |
| 42 | + - name: Format check |
| 43 | + run: | |
| 44 | + make fmt |
| 45 | + git diff --exit-code || (echo "Code is not properly formatted. Run 'make fmt' locally." && exit 1) |
| 46 | +
|
| 47 | + - name: Vet check |
| 48 | + run: make vet |
| 49 | + |
| 50 | + - name: Run tests |
| 51 | + run: make test |
| 52 | + |
| 53 | + - name: Run tests with coverage |
| 54 | + run: make test-coverage |
| 55 | + |
| 56 | + - name: Upload coverage report |
| 57 | + uses: actions/upload-artifact@v4 |
| 58 | + with: |
| 59 | + name: coverage-report |
| 60 | + path: coverage.html |
| 61 | + |
| 62 | + - name: Build |
| 63 | + run: make build |
| 64 | + |
| 65 | + release: |
| 66 | + name: Release |
| 67 | + needs: build-test |
| 68 | + if: ${{ (github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')) || (github.event_name == 'release' && startsWith(github.event.release.tag_name, 'v')) }} |
| 69 | + runs-on: ubuntu-latest |
| 70 | + permissions: |
| 71 | + contents: write # Required for creating GitHub releases |
| 72 | + steps: |
| 73 | + - name: Checkout code |
| 74 | + uses: actions/checkout@v4 |
| 75 | + with: |
| 76 | + fetch-depth: 0 # Required for version determination from git tags |
| 77 | + |
| 78 | + - name: Set up Go |
| 79 | + uses: actions/setup-go@v5 |
| 80 | + with: |
| 81 | + go-version: '1.21' |
| 82 | + check-latest: true |
| 83 | + cache: true |
| 84 | + |
| 85 | + - name: Install dependencies |
| 86 | + run: make mod-download |
| 87 | + |
| 88 | + - name: Build multi-platform binaries |
| 89 | + run: make build-all |
| 90 | + |
| 91 | + - name: Create release |
| 92 | + id: create_release |
| 93 | + uses: softprops/action-gh-release@v2 |
| 94 | + with: |
| 95 | + files: | |
| 96 | + ./bin/prox-linux-amd64 |
| 97 | + ./bin/prox-linux-arm64 |
| 98 | + ./bin/prox-darwin-amd64 |
| 99 | + ./bin/prox-darwin-arm64 |
| 100 | + ./bin/prox-windows-amd64.exe |
| 101 | + draft: false |
| 102 | + prerelease: false |
| 103 | + generate_release_notes: true |
| 104 | + |
| 105 | + docker: |
| 106 | + name: Docker Build and Push |
| 107 | + needs: build-test |
| 108 | + if: ${{ (github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')) || (github.event_name == 'release' && startsWith(github.event.release.tag_name, 'v')) || (github.event_name == 'workflow_dispatch') }} |
| 109 | + runs-on: ubuntu-latest |
| 110 | + permissions: |
| 111 | + contents: read |
| 112 | + packages: write # Required for pushing to GitHub Container Registry |
| 113 | + steps: |
| 114 | + - name: Checkout code |
| 115 | + uses: actions/checkout@v4 |
| 116 | + |
| 117 | + - name: Set up QEMU |
| 118 | + uses: docker/setup-qemu-action@v3 |
| 119 | + |
| 120 | + - name: Set up Docker Buildx |
| 121 | + uses: docker/setup-buildx-action@v3 |
| 122 | + |
| 123 | + - name: Login to GitHub Container Registry |
| 124 | + if: ${{ github.event_name != 'workflow_dispatch' }} |
| 125 | + uses: docker/login-action@v3 |
| 126 | + with: |
| 127 | + registry: ghcr.io |
| 128 | + username: ${{ github.repository_owner }} |
| 129 | + password: ${{ secrets.GHCR_TOKEN || secrets.GITHUB_TOKEN }} |
| 130 | + |
| 131 | + - name: Login to GitHub Container Registry (manual) |
| 132 | + if: ${{ github.event_name == 'workflow_dispatch' && inputs.push }} |
| 133 | + uses: docker/login-action@v3 |
| 134 | + with: |
| 135 | + registry: ghcr.io |
| 136 | + username: ${{ github.repository_owner }} |
| 137 | + password: ${{ secrets.GHCR_TOKEN || secrets.GITHUB_TOKEN }} |
| 138 | + |
| 139 | + - name: Create GHCR package namespace if needed |
| 140 | + if: ${{ github.event_name != 'workflow_dispatch' || inputs.push }} |
| 141 | + run: | |
| 142 | + # Use a more explicit token approach |
| 143 | + TOKEN="${{ secrets.GHCR_TOKEN }}" |
| 144 | + if [ -z "$TOKEN" ]; then |
| 145 | + TOKEN="${{ secrets.GITHUB_TOKEN }}" |
| 146 | + echo "Using GITHUB_TOKEN (may have limited permissions for first push)" |
| 147 | + else |
| 148 | + echo "Using GHCR_TOKEN" |
| 149 | + fi |
| 150 | + |
| 151 | + # Check if package exists |
| 152 | + if ! docker manifest inspect ghcr.io/${{ github.repository_owner }}/prox:latest >/dev/null 2>&1; then |
| 153 | + echo "Package doesn't exist, creating namespace with a test push..." |
| 154 | + |
| 155 | + # Create a minimal image and push it to establish the package |
| 156 | + cat > Dockerfile.init << 'EOF' |
| 157 | + FROM alpine:latest |
| 158 | + RUN echo "Package initialization" > /tmp/init |
| 159 | + EOF |
| 160 | + |
| 161 | + docker build -f Dockerfile.init -t ghcr.io/${{ github.repository_owner }}/prox:init . |
| 162 | + |
| 163 | + # Try to push the init image to create the package namespace |
| 164 | + if docker push ghcr.io/${{ github.repository_owner }}/prox:init; then |
| 165 | + echo "✅ Package namespace created successfully" |
| 166 | + # Clean up the init tag |
| 167 | + rm -f Dockerfile.init |
| 168 | + else |
| 169 | + echo "⚠️ Failed to create package namespace, but continuing..." |
| 170 | + echo "This might be due to token permissions. Consider using a PAT with write:packages scope." |
| 171 | + fi |
| 172 | + else |
| 173 | + echo "✅ Package already exists, proceeding..." |
| 174 | + fi |
| 175 | +
|
| 176 | + - name: Docker metadata |
| 177 | + id: meta |
| 178 | + uses: docker/metadata-action@v5 |
| 179 | + with: |
| 180 | + images: ghcr.io/${{ github.repository_owner }}/prox |
| 181 | + tags: | |
| 182 | + type=semver,pattern={{version}} |
| 183 | + type=raw,value=latest |
| 184 | +
|
| 185 | + - name: Build image (manual) |
| 186 | + if: ${{ github.event_name == 'workflow_dispatch' }} |
| 187 | + uses: docker/build-push-action@v5 |
| 188 | + with: |
| 189 | + context: . |
| 190 | + push: ${{ inputs.push }} |
| 191 | + platforms: linux/amd64,linux/arm64 |
| 192 | + tags: | |
| 193 | + ${{ steps.meta.outputs.tags }} |
| 194 | + ${{ inputs.tag != '' && format('ghcr.io/{0}/prox:{1}', github.repository_owner, inputs.tag) || '' }} |
| 195 | + labels: ${{ steps.meta.outputs.labels }} |
| 196 | + |
| 197 | + - name: Build and push Docker image (tags/releases) |
| 198 | + if: ${{ github.event_name != 'workflow_dispatch' }} |
| 199 | + uses: docker/build-push-action@v5 |
| 200 | + with: |
| 201 | + context: . |
| 202 | + push: true |
| 203 | + platforms: linux/amd64,linux/arm64 |
| 204 | + tags: ${{ steps.meta.outputs.tags }} |
| 205 | + labels: ${{ steps.meta.outputs.labels }} |
| 206 | + |
| 207 | + - name: Verify push success |
| 208 | + if: ${{ (github.event_name != 'workflow_dispatch' || inputs.push) && success() }} |
| 209 | + run: | |
| 210 | + echo "✅ Images pushed successfully!" |
| 211 | + echo "Available tags:" |
| 212 | + docker manifest inspect ghcr.io/${{ github.repository_owner }}/prox:latest || echo "❌ Latest tag verification failed" |
| 213 | + |
| 214 | + - name: Push failure guidance |
| 215 | + if: ${{ (github.event_name != 'workflow_dispatch' || inputs.push) && failure() }} |
| 216 | + run: | |
| 217 | + echo "❌ Push failed with 403 Forbidden error" |
| 218 | + echo "" |
| 219 | + echo "🔧 To fix this issue:" |
| 220 | + echo "1. Create a Personal Access Token (PAT) at: https://github.com/settings/tokens" |
| 221 | + echo "2. Grant these permissions: write:packages, read:packages" |
| 222 | + echo "3. Add it as repository secret 'GHCR_TOKEN' at:" |
| 223 | + echo " https://github.com/${{ github.repository }}/settings/secrets/actions" |
| 224 | + echo "4. Re-run this workflow" |
| 225 | + echo "" |
| 226 | + echo "📖 More info: https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-container-registry" |
0 commit comments