refactor build workflow, support signing

This commit is contained in:
Wonderfall 2022-03-05 18:01:06 +01:00 committed by hoellen
parent d451bcf1ef
commit bce0750e6e

View File

@ -8,44 +8,66 @@ on:
# Build the image regularly (each Friday) # Build the image regularly (each Friday)
- cron: '23 04 * * 5' - cron: '23 04 * * 5'
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository_owner }}/nextcloud
jobs: jobs:
build: build:
name: Build, scan & push name: Build, push & sign
runs-on: "ubuntu-20.04" runs-on: "ubuntu-latest"
permissions:
contents: read
packages: write
id-token: write
steps: steps:
- name: Checkout code - name: Checkout code
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: Build an image from Dockerfile - name: Extract version for tags
run: | run: |
docker build \ echo "FULL_VERSION=$(grep -oP '(?<=NEXTCLOUD_VERSION=).*' Dockerfile | head -c6)" >> $GITHUB_ENV
-t ghcr.io/${{ github.actor }}/nextcloud \ echo "MAJOR_VERSION=$(grep -oP '(?<=NEXTCLOUD_VERSION=).*' Dockerfile | head -c2)" >> $GITHUB_ENV
-t ghcr.io/${{ github.actor }}/nextcloud:$(grep -oP '(?<=NEXTCLOUD_VERSION=).*' Dockerfile | head -c6) \
-t ghcr.io/${{ github.actor }}/nextcloud:$(grep -oP '(?<=NEXTCLOUD_VERSION=).*' Dockerfile | head -c2) \
.
- name: Run Trivy vulnerability scanner - name: Install cosign
uses: aquasecurity/trivy-action@master if: github.event_name != 'pull_request'
uses: sigstore/cosign-installer@main
with: with:
image-ref: 'ghcr.io/${{ github.actor }}/nextcloud' cosign-release: 'v1.6.0'
format: 'template'
template: '@/contrib/sarif.tpl'
output: 'trivy-results.sarif'
severity: 'CRITICAL,HIGH'
vuln-type: "os"
- name: Upload Trivy scan results to GitHub Security tab - name: Set up Docker Buildx
uses: github/codeql-action/upload-sarif@v1 uses: docker/setup-buildx-action@v1
- name: Login to registry
if: github.event_name != 'pull_request'
uses: docker/login-action@v1
with: with:
sarif_file: 'trivy-results.sarif' registry: ${{ env.REGISTRY }}
username: ${{ github.repository_owner }}
password: ${{ secrets.GHCR_TOKEN }}
- name: Login to the registry - name: Set Docker metadata
run: >- id: meta
echo "${{ secrets.GITHUB_TOKEN }}" uses: docker/metadata-action@v3
| docker login -u "${{ github.actor }}" --password-stdin ghcr.io with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
latest
${{ env.FULL_VERSION }}
${{ env.MAJOR_VERSION }}
- name: Push image to GitHub - name: Build and push Docker image
run: | id: build-and-push
docker push ghcr.io/${{ github.actor }}/nextcloud uses: docker/build-push-action@v2
docker push ghcr.io/${{ github.actor }}/nextcloud:$(grep -oP '(?<=NEXTCLOUD_VERSION=).*' Dockerfile | head -c6) with:
docker push ghcr.io/${{ github.actor }}/nextcloud:$(grep -oP '(?<=NEXTCLOUD_VERSION=).*' Dockerfile | head -c2) context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
- name: Sign the published Docker image
if: ${{ github.event_name != 'pull_request' }}
env:
COSIGN_EXPERIMENTAL: "true"
run: cosign sign ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ steps.build-and-push.outputs.digest }}