update trivy (#24) #19
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
# This workflow will build a docker container, publish it to Azure Container Registry, and deploy it to Azure Kubernetes Service using a helm chart. | |
# | |
# https://github.com/Azure/actions-workflow-samples/tree/master/Kubernetes | |
# | |
# To configure this workflow: | |
# | |
# 1. Set up the following secrets in your workspace: | |
# a. REGISTRY_USERNAME with ACR username | |
# b. REGISTRY_PASSWORD with ACR Password | |
# | |
# 2. Change the values for the REGISTRY_NAME environment variables (below). | |
name: publish | |
on: | |
push: | |
branches: | |
- master | |
# Environment variables available to all jobs and steps in this workflow | |
env: | |
REGISTRY_NAME: k8scc01covidacr | |
TRIVY_VERSION: "v0.57.0" | |
TRIVY_DATABASES: '"ghcr.io/aquasecurity/trivy-db:2","public.ecr.aws/aquasecurity/trivy-db"' | |
TRIVY_JAVA_DATABASES: '"ghcr.io/aquasecurity/trivy-java-db:1","public.ecr.aws/aquasecurity/trivy-java-db"' | |
TRIVY_MAX_RETRIES: 5 | |
TRIVY_RETRY_DELAY: 20 | |
jobs: | |
build: | |
services: | |
registry: | |
image: registry:2 | |
ports: | |
- 5000:5000 | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@master | |
# Connect to Azure Container registry (ACR) | |
- uses: azure/docker-login@v1 | |
with: | |
login-server: ${{ env.REGISTRY_NAME }}.azurecr.io | |
username: ${{ secrets.REGISTRY_USERNAME }} | |
password: ${{ secrets.REGISTRY_PASSWORD }} | |
# Container build | |
- name: Build image | |
run: | | |
docker build -f Dockerfile -t localhost:5000/profile-state-controller:${{ github.sha }} . | |
docker push localhost:5000/profile-state-controller:${{ github.sha }} | |
docker image prune | |
# Scan image for vulnerabilities | |
- name: Aqua Security Trivy image scan | |
run: | | |
curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b /usr/local/bin ${{ env.TRIVY_VERSION }} | |
set +e # Lets trivy return an error without it being fatal | |
for ((i=0; i<${{ env.TRIVY_MAX_RETRIES }}; i++)); do | |
echo "Attempt $((i + 1)) of ${{ env.TRIVY_MAX_RETRIES }}..." | |
trivy image \ | |
--db-repository ${{ env.TRIVY_DATABASES }} \ | |
--java-db-repository ${{ env.TRIVY_JAVA_DATABASES }} \ | |
localhost:5000/profile-state-controller:${{ github.sha }} \ | |
--exit-code 10 --timeout=20m --scanners vuln --severity CRITICAL \ | |
EXIT_CODE=$? | |
if [[ $EXIT_CODE -eq 0 ]]; then | |
echo "Trivy scan completed successfully." | |
exit 0 | |
elif [[ $EXIT_CODE -eq 10 ]]; then | |
echo "Trivy scan completed successfully. Some vulnerabilities were found." | |
exit 10 | |
elif [[ $i -lt $(( ${{ env.TRIVY_MAX_RETRIES }} - 1)) ]]; then | |
echo "Encountered unexpected error. Retrying in ${{ env.TRIVY_RETRY_DELAY }} seconds..." | |
sleep ${{ env.TRIVY_RETRY_DELAY }} | |
else | |
echo "Unexpected error persists after ${{ env.TRIVY_MAX_RETRIES }} attempts. Exiting." | |
exit 1 | |
fi | |
done | |
# Run Dockle | |
- name: Run dockle | |
uses: goodwithtech/dockle-action@main | |
with: | |
image: localhost:5000/profile-state-controller:${{ github.sha }} | |
format: 'list' | |
exit-code: '1' | |
exit-level: 'fatal' | |
ignore: 'DKL-DI-0006' | |
# Push to the ACR | |
- name: Push image to registry | |
run: | | |
docker pull localhost:5000/profile-state-controller:${{ github.sha }} | |
docker tag localhost:5000/profile-state-controller:${{ github.sha }} ${{ env.REGISTRY_NAME }}.azurecr.io/profile-state-controller:${{ github.sha }} | |
docker push ${{ env.REGISTRY_NAME }}.azurecr.io/profile-state-controller:${{ github.sha }} |