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, package, and Trivy scanner, Checkov, Deploy demo-app using Terraform to AWS.' | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
build-and-push: | |
runs-on: ubuntu-latest | |
outputs: | |
image_tag: ${{ steps.set_timestamp.outputs.image_tag }} | |
steps: | |
- uses: actions/checkout@v2 | |
- name: Log in to GitHub Container Registry.. | |
uses: docker/login-action@v1 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.GHCR_PAT }} | |
- name: Generate timestamp-based image tag | |
id: set_timestamp | |
run: | | |
REPO_OWNER=$(echo "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]') | |
DATE=$(date +'%Y%m%d%H%M%S') | |
IMAGE_TAG=ghcr.io/${REPO_OWNER}/ghcr-democicdapp:v${DATE} | |
echo "REPO_OWNER=${REPO_OWNER}" >> $GITHUB_ENV | |
echo "IMAGE_TAG=${IMAGE_TAG}" >> $GITHUB_ENV | |
echo "::set-output name=image_tag::${IMAGE_TAG}" | |
- name: Build and push Docker image with timestamp tag | |
run: | | |
docker build . -t ${{ steps.set_timestamp.outputs.image_tag }} | |
docker push ${{ steps.set_timestamp.outputs.image_tag }} | |
- name: Scan the Docker image with Trivy | |
uses: aquasecurity/trivy-action@master | |
with: | |
image-ref: ${{ steps.set_timestamp.outputs.image_tag }} | |
format: 'table' | |
output: 'trivy-report.txt' | |
severity: 'CRITICAL,HIGH' | |
continue-on-error: true | |
- name: Upload Trivy scan report | |
uses: actions/upload-artifact@v2 | |
with: | |
name: Trivy-scan-report | |
path: trivy-report.txt | |
- name: Set up Python | |
uses: actions/setup-python@v2 | |
with: | |
python-version: '3.x' | |
- name: Install Checkov | |
run: pip install checkov | |
- name: Run Checkov scan | |
run: checkov -d . --quiet --output-file checkov-report.txt | |
continue-on-error: true # This ensures the step doesn't cause the workflow to fail | |
- name: Upload Checkov scan report | |
uses: actions/upload-artifact@v2 | |
with: | |
name: Checkov-scan-report | |
path: checkov-report.txt | |
terraform: | |
name: 'Terraform' | |
runs-on: ubuntu-latest | |
needs: build-and-push | |
defaults: | |
run: | |
working-directory: terraform | |
env: | |
DOCKER_IMAGE_TAG: ${{ needs.build-and-push.outputs.image_tag }} | |
steps: | |
- uses: actions/checkout@v2 | |
- name: Set up AWS credentials | |
uses: aws-actions/configure-aws-credentials@v1 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
aws-region: us-east-1 | |
- name: Set up Terraform | |
uses: hashicorp/setup-terraform@v1 | |
- name: Terraform Initialize | |
run: terraform init | |
- name: Terraform Format and Write Changes | |
run: terraform fmt -recursive | |
- name: Terraform Plan | |
run: terraform plan -var="docker_image_tag=${{ env.DOCKER_IMAGE_TAG }}" -var="repo_owner=${{ github.repository_owner }}" -lock=false | |
- name: Terraform Apply | |
run: terraform apply -var="docker_image_tag=${{ env.DOCKER_IMAGE_TAG }}" -var="repo_owner=${{ github.repository_owner }}" -auto-approve -lock=false |