Skip to content

Improve build and test performance #883

Improve build and test performance

Improve build and test performance #883

Workflow file for this run

name: Build and push docker image
on:
push:
tags: ['*']
branches: ["dev"]
paths-ignore:
- README.md
- .github/workflows/update-dockerhub-desc.yml
pull_request:
branches: ["dev"]
env:
IMAGE_REGISTRY: ${{ vars.REGISTRY_PUBLIC }}
#<account>/<repo>
IMAGE_NAME: ${{ github.repository }}
TEST_TAG: test-build
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- uses: step-security/harden-runner@v2
with:
egress-policy: audit
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Prepare Meta Data
id: meta
uses: docker/metadata-action@v5
with:
images: |
${{ github.repository }}
quay.io/${{ github.repository }}
tags: |
type=ref,event=tag
type=ref,event=branch
type=ref,event=pr
- name: Prepare Meta Data (Slim)
id: meta-slim
uses: docker/metadata-action@v5
with:
images: |
${{ github.repository }}
quay.io/${{ github.repository }}
flavor: |
suffix=-slim,onLatest=true
tags: |
type=ref,event=tag
type=ref,event=branch
type=ref,event=pr
- name: Prepare
id: prepare
run: |
DOCKER_PLATFORMS=${{ vars.CONTAINER_ARCH }}
VERSION=${GITHUB_REF#refs/*/}
TAGS="${{ env.IMAGE_NAME }}:${VERSION}"
TAGS_SLIM="${{ env.IMAGE_NAME }}:${VERSION}-slim"
if [[ $GITHUB_REF == refs/tags/* ]]; then
TAGS="$TAGS,${{ env.IMAGE_NAME }}:latest"
TAGS_SLIM="$TAGS_SLIM,${{ env.IMAGE_NAME }}:latest-slim"
fi
for TAG in $(echo $TAGS | sed 's/,/ /g'); do
TAGS="$TAGS,quay.io/$TAG"
TAGS_SLIM="$TAGS_SLIM,quay.io/$TAG-slim"
done
echo "platforms=${DOCKER_PLATFORMS}" >> $GITHUB_OUTPUT
echo "tags=${TAGS}" >> $GITHUB_OUTPUT
echo "tags_slim=${TAGS_SLIM}" >> $GITHUB_OUTPUT
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to DockerHub
uses: docker/login-action@v3
with:
registry: ${{ env.IMAGE_REGISTRY }}
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Login to Quay.io
uses: docker/login-action@v3
with:
registry: quay.io
username: ${{ secrets.QUAYIO_USERNAME }}
password: ${{ secrets.QUAYIO_TOKEN }}
- name: Try build
uses: docker/build-push-action@v6
with:
context: .
push: false
tags: ${{ env.IMAGE_NAME }}:${{ env.TEST_TAG }}
provenance: false
load: true
build-args: |
ARG_MOODLE_PLUGIN_LIST=report_benchmark
ARG_ALLOW_INCOMPATIBLE_PLUGIN=true
- name: Try build (Slim)
uses: docker/build-push-action@v6
with:
context: .
push: false
tags: ${{ env.IMAGE_NAME }}:${{ env.TEST_TAG }}
provenance: false
load: true
build-args: |
ARG_MOODLE_PLUGIN_LIST=report_benchmark
ARG_ALLOW_INCOMPATIBLE_PLUGIN=true
ARG_ENABLE_GIT_CLONE='false'
- name: Test
run: |
export TEST_IMAGE_NAME=${{ env.IMAGE_NAME }}:${{ env.TEST_TAG }}
docker compose --file docker-compose.test.yml up --exit-code-from sut -t 10
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@master
with:
image-ref: '${{ env.IMAGE_NAME }}:${{ env.TEST_TAG }}'
format: 'sarif'
output: 'trivy-results.sarif'
severity: 'CRITICAL,HIGH'
exit-code: 0
env:
TRIVY_DB_REPOSITORY: ghcr.io/aquasecurity/trivy-db,public.ecr.aws/aquasecurity/trivy-db
- name: Upload Trivy scan results to GitHub Security tab
if: github.event_name != 'pull_request'
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: 'trivy-results.sarif'
- name: Run the Anchore scan action
uses: anchore/scan-action@v6
id: anchore-scan
with:
image: '${{ env.IMAGE_NAME }}:${{ env.TEST_TAG }}'
output-format: sarif
fail-build: false
- name: Upload Anchore Scan Report
if: github.event_name != 'pull_request'
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: ${{ steps.anchore-scan.outputs.sarif }}
- name: Get changed container files
id: changed-files
uses: tj-actions/changed-files@v46
with:
files: |
rootfs/**
Dockerfile
- name: List changed container files
run: |
echo "Any container files changed? ${{ steps.changed-files.outputs.any_changed }}"
echo "Changed files: ${{ steps.changed-files.outputs.all_changed_files }}"
- name: Build and push
if: steps.changed-files.outputs.any_changed == 'true'
uses: docker/build-push-action@v6
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
# tags: ${{ steps.prepare.outputs.tags }}
#platforms: ${{ steps.prepare.outputs.platforms }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
annotations: ${{ steps.meta.outputs.annotations }}
platforms: ${{ vars.CONTAINER_ARCH }}
provenance: false
- name: Build and push (slim version)
if: steps.changed-files.outputs.any_changed == 'true'
uses: docker/build-push-action@v6
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
# tags: ${{ steps.prepare.outputs.tags_slim }}
#platforms: ${{ steps.prepare.outputs.platforms }}
tags: ${{ steps.meta-slim.outputs.tags }}
labels: ${{ steps.meta-slim.outputs.labels }}
annotations: ${{ steps.meta-slim.outputs.annotations }}
platforms: ${{ vars.CONTAINER_ARCH }}
provenance: false
build-args: |
ARG_ENABLE_GIT_CLONE='false'