Build images and push to registry #999
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build images and push to registry | |
on: | |
schedule: | |
- cron: '16 6 * * *' | |
push: | |
branches: [ main ] | |
pull_request: | |
branches: [ main ] | |
env: | |
# Use docker.io for Docker Hub if empty | |
REGISTRY: ghcr.io | |
# github.repository as <account>/<repo> | |
IMAGE_NAME: ${{ github.repository }} | |
jobs: | |
lint: | |
runs-on: ubuntu-latest | |
permissions: | |
security-events: write | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Lint Dockerfile | |
id: hadolint | |
uses: hadolint/[email protected] | |
with: | |
dockerfile: Dockerfile | |
format: sarif | |
output-file: hadolint-results.sarif | |
no-fail: true | |
- name: Upload analysis results to GitHub | |
uses: github/codeql-action/upload-sarif@v3 | |
with: | |
sarif_file: hadolint-results.sarif | |
wait-for-processing: true | |
- name: Update Pull Request | |
uses: actions/github-script@v7 | |
if: github.event_name == 'pull_request' | |
with: | |
script: | | |
const output = ` | |
#### Hadolint: \`${{ steps.hadolint.outcome }}\` | |
\`\`\` | |
${process.env.HADOLINT_RESULTS} | |
\`\`\` | |
`; | |
github.rest.issues.createComment({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: output | |
}) | |
build: | |
runs-on: ubuntu-latest | |
needs: [ lint ] | |
permissions: | |
contents: read | |
packages: write | |
id-token: write | |
strategy: | |
fail-fast: false | |
matrix: | |
php_version: [ "7", "8", "81", "82" ] | |
include: | |
- php_version: 7 | |
alpine_version: 3.15 | |
- php_version: 8 | |
alpine_version: 3.16 | |
- php_version: 81 | |
alpine_version: 3.17 | |
- php_version: 82 | |
alpine_version: 3.18 | |
# - php_version: 83 | |
# alpine_version: edge | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
# Install the cosign tool except on PR | |
# https://github.com/sigstore/cosign-installer | |
- name: Install cosign | |
if: github.event_name != 'pull_request' | |
uses: sigstore/cosign-installer@main | |
- name: downcase REPO | |
run: | | |
echo "REPO=${GITHUB_REPOSITORY,,}" >>${GITHUB_ENV} | |
- name: Set up QEMU | |
uses: docker/setup-qemu-action@v3 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
# Login against a Docker registry except on PR | |
# https://github.com/docker/login-action | |
- name: Log into registry ${{ env.REGISTRY }} | |
if: github.event_name != 'pull_request' | |
uses: docker/login-action@v3 | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Login to DockerHub | |
if: github.event_name != 'pull_request' | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
# Extract metadata (tags, labels) for Docker | |
# https://github.com/docker/metadata-action | |
- name: Extract Docker metadata | |
id: meta | |
uses: docker/metadata-action@v5 | |
with: | |
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
# Build and push Docker image with Buildx (don't push on PR) | |
# https://github.com/docker/build-push-action | |
- name: Build and push php ${{ matrix.php_version }} image | |
uses: docker/build-push-action@v6 | |
id: build-and-push | |
with: | |
context: . | |
platforms: linux/amd64,linux/arm64 | |
push: ${{ github.event_name != 'pull_request' }} | |
cache-from: type=gha | |
cache-to: type=gha,mode=max | |
build-args: | | |
PHP_VERSION=${{ matrix.php_version }} | |
ALPINE_VERSION=${{ matrix.alpine_version }} | |
tags: | | |
${{ env.REGISTRY }}/${{ env.REPO }}:${{ matrix.php_version }} | |
${{ env.REPO }}:${{ matrix.php_version }} | |
# Sign the resulting Docker image digest except on PRs. | |
# This will only write to the public Rekor transparency log when the Docker | |
# repository is public to avoid leaking data. If you would like to publish | |
# transparency data even for private images, pass --force to cosign below. | |
# https://github.com/sigstore/cosign | |
- name: Sign the published Docker image | |
if: ${{ github.event_name != 'pull_request' }} | |
env: | |
COSIGN_YES: "true" | |
# This step uses the identity token to provision an ephemeral certificate | |
# against the sigstore community Fulcio instance. | |
run: | | |
cosign sign ${{ env.REGISTRY }}/${{ env.REPO }}:${{ matrix.php_version }}@${{ steps.build-and-push.outputs.digest }} | |
cosign sign ${{ env.REPO }}:${{ matrix.php_version }}@${{ steps.build-and-push.outputs.digest }} | |
scan_image: | |
runs-on: ubuntu-latest | |
permissions: | |
security-events: write | |
if: ${{ always() }} | |
needs: | |
- build | |
strategy: | |
fail-fast: false | |
matrix: | |
php_version: [ "7", "8", "81", "82" ] | |
steps: | |
- name: downcase REPO | |
run: | | |
echo "REPO=${GITHUB_REPOSITORY,,}" >>${GITHUB_ENV} | |
- name: Run Trivy vulnerability scanner for php ${{ matrix.php_version }} | |
uses: aquasecurity/trivy-action@master | |
with: | |
image-ref: 'ghcr.io/${{ env.REPO }}:${{ matrix.php_version }}' | |
format: 'sarif' | |
output: 'trivy-php${{ matrix.php_version }}-results.sarif' | |
- name: Upload Trivy scan results to GitHub Security tab | |
uses: github/codeql-action/upload-sarif@v3 | |
if: always() | |
with: | |
sarif_file: 'trivy-php${{ matrix.php_version }}-results.sarif' |