|
| 1 | +name: Build PHP Docker Image |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + paths: |
| 8 | + - php/** |
| 9 | + pull_request: |
| 10 | + branches: |
| 11 | + - main |
| 12 | + paths: |
| 13 | + - php/** |
| 14 | + |
| 15 | + |
| 16 | +env: |
| 17 | + # Use docker.io for Docker Hub if empty |
| 18 | + REGISTRY: ghcr.io |
| 19 | + # github.repository as <account>/<repo> |
| 20 | + IMAGE_NAME: yieldstudio/php |
| 21 | + |
| 22 | +jobs: |
| 23 | + matrix: |
| 24 | + runs-on: ubuntu-latest |
| 25 | + outputs: |
| 26 | + php_versions: ${{ steps.set.outputs.php_versions }} |
| 27 | + type: ${{ steps.set.outputs.type }} |
| 28 | + dockerfile: ${{ steps.set.outputs.dockerfile }} |
| 29 | + steps: |
| 30 | + - name: Set PHP versions |
| 31 | + id: set |
| 32 | + run: | |
| 33 | + echo 'php_versions=["8.2","8.3","8.4"]' >> $GITHUB_OUTPUT |
| 34 | + echo 'type=["unit"]' >> $GITHUB_OUTPUT |
| 35 | + echo 'dockerfile=["unit.Dockerfile"]' >> $GITHUB_OUTPUT |
| 36 | +
|
| 37 | + build: |
| 38 | + needs: matrix |
| 39 | + runs-on: ubuntu-latest |
| 40 | + permissions: |
| 41 | + contents: read |
| 42 | + packages: write |
| 43 | + id-token: write |
| 44 | + env: |
| 45 | + NODE_VERSION: '22' |
| 46 | + strategy: |
| 47 | + matrix: |
| 48 | + php_version: ${{ fromJSON(needs.matrix.outputs.php_versions) }} |
| 49 | + type: ${{ fromJSON(needs.matrix.outputs.type) }} |
| 50 | + dockerfile: ${{ fromJSON(needs.matrix.outputs.dockerfile) }} |
| 51 | + steps: |
| 52 | + - name: Checkout code |
| 53 | + uses: actions/checkout@v4 |
| 54 | + |
| 55 | + - name: Set up QEMU |
| 56 | + uses: docker/setup-qemu-action@v3 |
| 57 | + |
| 58 | + - name: Set up Docker Buildx |
| 59 | + uses: docker/setup-buildx-action@v3 |
| 60 | + |
| 61 | + - name: Log into registry ${{ env.REGISTRY }} |
| 62 | + if: github.event_name != 'pull_request' |
| 63 | + uses: docker/login-action@v3 |
| 64 | + with: |
| 65 | + registry: ${{ env.REGISTRY }} |
| 66 | + username: ${{ github.actor }} |
| 67 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 68 | + |
| 69 | + - name: Extract Docker metadata |
| 70 | + id: meta |
| 71 | + uses: docker/metadata-action@v4 |
| 72 | + with: |
| 73 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 74 | + tags: | |
| 75 | + type=ref,event=branch,prefix=${{ matrix.php_version }}-${{ matrix.type }}-latest- |
| 76 | + type=ref,event=tag,prefix=${{ matrix.php_version }}-${{ matrix.type }}-v |
| 77 | + type=sha |
| 78 | + type=raw,${{ matrix.php_version }}-${{ matrix.type }} |
| 79 | +
|
| 80 | + - name: Build docker image |
| 81 | + uses: docker/build-push-action@v6 |
| 82 | + with: |
| 83 | + context: ./php |
| 84 | + file: ./php/${{ matrix.dockerfile }} |
| 85 | + build-args: | |
| 86 | + PHP_VERSION=${{ matrix.php_version }} |
| 87 | + NODE_VERSION=${{ env.NODE_VERSION }} |
| 88 | + platforms: linux/amd64,linux/arm64 |
| 89 | + cache-from: type=gha,scope=php-${{ matrix.php_version }}-${{ matrix.type }} |
| 90 | + cache-to: type=gha,scope=php-${{ matrix.php_version }}-${{ matrix.type }},mode=max |
| 91 | + push: ${{ github.event_name != 'pull_request' }} |
| 92 | + tags: | |
| 93 | + ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ matrix.php_version }}-${{ matrix.type }} |
| 94 | + ${{ steps.meta.outputs.tags }} |
| 95 | + labels: ${{ steps.meta.outputs.labels }} |
| 96 | + |
| 97 | + scan: |
| 98 | + needs: |
| 99 | + - matrix |
| 100 | + - build |
| 101 | + runs-on: ubuntu-latest |
| 102 | + strategy: |
| 103 | + matrix: |
| 104 | + php_version: ${{ fromJSON(needs.matrix.outputs.php_versions) }} |
| 105 | + type: ${{ fromJSON(needs.matrix.outputs.type) }} |
| 106 | + dockerfile: ${{ fromJSON(needs.matrix.outputs.dockerfile) }} |
| 107 | + steps: |
| 108 | + - name: Checkout Code |
| 109 | + uses: actions/checkout@v4 |
| 110 | + |
| 111 | + - name: Run Trivy Vulnerability Scan |
| 112 | + uses: aquasecurity/trivy-action@0.31.0 |
| 113 | + with: |
| 114 | + image-ref: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ matrix.php_version }}-${{ matrix.type }} |
| 115 | + format: 'sarif' |
| 116 | + output: 'trivy-results.sarif' |
| 117 | + |
| 118 | + - name: Upload Trivy scan results to GitHub Security tab |
| 119 | + uses: github/codeql-action/upload-sarif@v3 |
| 120 | + if: always() |
| 121 | + with: |
| 122 | + sarif_file: 'trivy-results.sarif' |
| 123 | + |
| 124 | + |
0 commit comments