Skip to content

Merge pull request #237 from cyber-dojo/fix-sonar-properties #325

Merge pull request #237 from cyber-dojo/fix-sonar-properties

Merge pull request #237 from cyber-dojo/fix-sonar-properties #325

Workflow file for this run

name: Main
on:
push:
env:
KOSLI_DRY_RUN: ${{ vars.KOSLI_DRY_RUN }} # false
KOSLI_HOST: ${{ vars.KOSLI_HOST }} # https://app.kosli.com
KOSLI_API_TOKEN: ${{ secrets.KOSLI_API_TOKEN }}
KOSLI_ORG: ${{ vars.KOSLI_ORG }} # cyber-dojo
KOSLI_FLOW: ${{ vars.KOSLI_FLOW }} # dashboard-ci
KOSLI_TRAIL: ${{ github.sha }}
KOSLI_SONAR_API_TOKEN: ${{ secrets.SONARCLOUD_TOKEN }}
AWS_ACCOUNT_ID: ${{ vars.AWS_ACCOUNT_ID }}
AWS_ECR_ID: ${{ vars.AWS_ECR_ID }}
AWS_REGION: ${{ vars.AWS_REGION }}
SERVICE_NAME: ${{ github.event.repository.name }} # dashboard
IMAGE_TAR_FILENAME: /tmp/${{ github.event.repository.name }}:${{ github.sha }}.tar
jobs:
setup:
runs-on: ubuntu-latest
outputs:
aws_account_id: ${{ steps.vars.outputs.aws_account_id }}
ecr_registry: ${{ steps.vars.outputs.ecr_registry }}
aws_region: ${{ steps.vars.outputs.aws_region }}
gh_actions_iam_role_name: ${{ steps.vars.outputs.gh_actions_iam_role_name }}
service_name: ${{ steps.vars.outputs.service_name }}
image_tag: ${{ steps.vars.outputs.image_tag }}
image_name: ${{ steps.vars.outputs.image_name }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 1
- name: Prepare outputs for workflow jobs
id: vars
run: |
IMAGE_TAG=${GITHUB_SHA:0:7}
ECR_REGISTRY="${AWS_ECR_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com"
IMAGE_NAME="${ECR_REGISTRY}/${SERVICE_NAME}:${IMAGE_TAG}"
echo "aws_account_id=${AWS_ACCOUNT_ID}" >> ${GITHUB_OUTPUT}
echo "ecr_registry=${ECR_REGISTRY}" >> ${GITHUB_OUTPUT}
echo "aws_region=${AWS_REGION}" >> ${GITHUB_OUTPUT}
echo "gh_actions_iam_role_name=gh_actions_services" >> ${GITHUB_OUTPUT}
echo "service_name=${SERVICE_NAME}" >> ${GITHUB_OUTPUT}
echo "image_tag=${IMAGE_TAG}" >> ${GITHUB_OUTPUT}
echo "image_name=${IMAGE_NAME}" >> ${GITHUB_OUTPUT}
- name: Setup Kosli CLI
if: ${{ github.ref == 'refs/heads/main' }}
uses: kosli-dev/setup-cli-action@v2
with:
version: ${{ vars.KOSLI_CLI_VERSION }}
- name: Begin Kosli Trail
if: ${{ github.ref == 'refs/heads/main' }}
run:
kosli begin trail "${{ env.KOSLI_TRAIL }}"
--flow="${{ env.KOSLI_FLOW }}"
--template-file=.kosli.yml
pull-request:
if: ${{ github.ref == 'refs/heads/main' }}
runs-on: ubuntu-latest
needs: [setup]
permissions:
id-token: write
contents: read
pull-requests: read
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 1
- name: Setup Kosli CLI
uses: kosli-dev/setup-cli-action@v2
with:
version: ${{ vars.KOSLI_CLI_VERSION }}
- name: Attest pull-request evidence to Kosli
run:
kosli attest pullrequest github
--github-token="${{ secrets.GITHUB_TOKEN }}"
--name=pull-request
rubocop-lint:
runs-on: ubuntu-latest
needs: [setup]
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 1
- name: Run Rubocop linter on source
run:
make rubocop_lint
- name: Setup Kosli CLI
if: ${{ github.ref == 'refs/heads/main' && (success() || failure()) }}
uses: kosli-dev/setup-cli-action@v2
with:
version: ${{ vars.KOSLI_CLI_VERSION }}
- name: Attest evidence to Kosli
if: ${{ github.ref == 'refs/heads/main' && (success() || failure()) }}
run:
kosli attest junit
--name=dashboard.rubocop-lint
--results-dir=./reports/rubocop
snyk-code-scan:
runs-on: ubuntu-latest
needs: [setup]
env:
SARIF_FILENAME: snyk.code.scan.json
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 1
- name: Setup Snyk
uses: snyk/actions/setup@master
- name: Run Snyk code scan
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
run:
snyk code test
--sarif
--sarif-file-output="${SARIF_FILENAME}"
--policy-path=.snyk
.
- name: Setup Kosli CLI
if: ${{ github.ref == 'refs/heads/main' && (success() || failure()) }}
uses: kosli-dev/setup-cli-action@v2
with:
version: ${{ vars.KOSLI_CLI_VERSION }}
- name: Attest evidence to Kosli
if: ${{ github.ref == 'refs/heads/main' && (success() || failure()) }}
run:
kosli attest snyk
--name=dashboard.snyk-code-scan
--scan-results="${SARIF_FILENAME}"
sonarcloud-scan:
runs-on: ubuntu-latest
needs: [setup]
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Run SonarCloud scan
env:
SONAR_TOKEN: ${{ env.KOSLI_SONAR_API_TOKEN }}
uses: sonarsource/sonarcloud-github-action@master
- name: Setup Kosli CLI
if: ${{ github.ref == 'refs/heads/main' && (success() || failure()) }}
uses: kosli-dev/setup-cli-action@v2
with:
version: ${{ vars.KOSLI_CLI_VERSION }}
- name: Attest evidence to Kosli
if: ${{ github.ref == 'refs/heads/main' && (success() || failure()) }}
run:
kosli attest sonar
--name=dashboard.sonarcloud-scan
build-image:
runs-on: ubuntu-latest
needs: [setup]
env:
IMAGE_NAME: ${{ needs.setup.outputs.image_name }}
permissions:
id-token: write
contents: write
outputs:
tag: ${{ steps.variables.outputs.tag }}
name: ${{ steps.variables.outputs.name }}
fingerprint: ${{ steps.variables.outputs.fingerprint }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 1
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: ${{ needs.setup.outputs.aws_region }}
role-duration-seconds: 900
role-session-name: ${{ github.event.repository.name }}
role-to-assume: arn:aws:iam::${{ needs.setup.outputs.aws_account_id }}:role/${{ needs.setup.outputs.gh_actions_iam_role_name }}
mask-aws-account-id: no
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Build and push image to ECR
uses: docker/build-push-action@v6
id: docker_build
with:
context: .
push: true
tags: ${{ env.IMAGE_NAME }}
build-args:
COMMIT_SHA=${{ github.sha }}
- name: Make variables available to following jobs
id: variables
run: |
TAG=${{ needs.setup.outputs.image_tag }}
FINGERPRINT=$(echo ${{ steps.docker_build.outputs.digest }} | sed 's/.*://')
echo "tag=${TAG}" >> ${GITHUB_OUTPUT}
echo "name=${IMAGE_NAME}" >> ${GITHUB_OUTPUT}
echo "fingerprint=${FINGERPRINT}" >> ${GITHUB_OUTPUT}
- name: Tar Docker image
run: |
docker pull ${{ env.IMAGE_NAME }}
docker image save ${{ env.IMAGE_NAME }} --output ${{ env.IMAGE_TAR_FILENAME }}
- name: Cache Docker image
uses: actions/cache@v4
with:
path: ${{ env.IMAGE_TAR_FILENAME }}
key: ${{ env.IMAGE_NAME }}
- name: Setup Kosli CLI
if: ${{ github.ref == 'refs/heads/main' }}
uses: kosli-dev/setup-cli-action@v2
with:
version: ${{ vars.KOSLI_CLI_VERSION }}
- name: Attest image provenance to Kosli
if: ${{ github.ref == 'refs/heads/main' }}
run:
kosli attest artifact "${IMAGE_NAME}"
--artifact-type=docker
--name=dashboard
unit-tests:
runs-on: ubuntu-latest
needs: [build-image]
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 1
- name: Retrieve Docker image from cache
uses: actions/cache@v4
with:
path: ${{ env.IMAGE_TAR_FILENAME }}
key: ${{ needs.build-image.outputs.name }}
- name: Load Docker image
run:
docker image load --input ${{ env.IMAGE_TAR_FILENAME }}
- name: Run unit tests
run:
make test_server
- name: Get Unit test coverage
id: coverage
run:
make coverage_server
- name: Setup Kosli CLI
if: ${{ github.ref == 'refs/heads/main' && (success() || failure()) }}
uses: kosli-dev/setup-cli-action@v2
with:
version: ${{ vars.KOSLI_CLI_VERSION }}
- name: Attest JUnit test evidence to Kosli
if: ${{ github.ref == 'refs/heads/main' && (success() || failure()) }}
env:
KOSLI_FINGERPRINT: ${{ needs.build-image.outputs.fingerprint }}
run:
kosli attest junit
--name=dashboard.unit-test
--results-dir=./reports/server/junit
- name: Attest coverage evidence to Kosli
if: ${{ github.ref == 'refs/heads/main' && (success() || failure()) }}
env:
KOSLI_FINGERPRINT: ${{ needs.build-image.outputs.fingerprint }}
run: |
KOSLI_COMPLIANT=$([ "${{ steps.coverage.outcome }}" == 'success' ] && echo true || echo false)
kosli attest generic \
--description="unit-test branch-coverage and metrics" \
--name=dashboard.unit-test-coverage \
--user-data=./reports/server/coverage_metrics.json
snyk-container-scan:
runs-on: ubuntu-latest
needs: [build-image]
env:
SARIF_FILENAME: snyk.container.scan.json
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 1
- name: Retrieve Docker image from cache
uses: actions/cache@v4
with:
path: ${{ env.IMAGE_TAR_FILENAME }}
key: ${{ needs.build-image.outputs.name }}
- name: Load Docker image
run:
docker image load --input ${{ env.IMAGE_TAR_FILENAME }}
- name: Setup Snyk
uses: snyk/actions/setup@master
- name: Run Snyk container scan
env:
IMAGE_NAME: ${{ needs.build-image.outputs.name }}
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
run:
snyk container test "${IMAGE_NAME}"
--file=Dockerfile
--sarif
--sarif-file-output="${SARIF_FILENAME}"
--policy-path=.snyk
- name: Setup Kosli CLI
if: ${{ github.ref == 'refs/heads/main' && (success() || failure()) }}
uses: kosli-dev/setup-cli-action@v2
with:
version: ${{ vars.KOSLI_CLI_VERSION }}
- name: Attest evidence to Kosli
if: ${{ github.ref == 'refs/heads/main' && (success() || failure()) }}
env:
KOSLI_FINGERPRINT: ${{ needs.build-image.outputs.fingerprint }}
run:
kosli attest snyk
--name=dashboard.snyk-container-scan
--scan-results="${SARIF_FILENAME}"
sdlc-control-gate:
if: ${{ github.ref == 'refs/heads/main' }}
runs-on: ubuntu-latest
needs: [build-image, rubocop-lint, pull-request, unit-tests, snyk-container-scan, snyk-code-scan, sonarcloud-scan]
steps:
- name: Setup Kosli CLI
uses: kosli-dev/setup-cli-action@v2
with:
version: ${{ vars.KOSLI_CLI_VERSION }}
- name: Kosli SDLC gate to short-circuit the workflow
env:
IMAGE_NAME: ${{ needs.build-image.outputs.name }}
KOSLI_FINGERPRINT: ${{ needs.build-image.outputs.fingerprint }}
run:
kosli assert artifact "${IMAGE_NAME}"
approve-deployment-to-beta:
runs-on: ubuntu-latest
needs: [build-image, sdlc-control-gate]
environment:
name: staging
url: https://beta.cyber-dojo.org
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Kosli CLI
uses: kosli-dev/setup-cli-action@v2
with:
version: ${{ vars.KOSLI_CLI_VERSION }}
- name: Report approval of deployment to Kosli
env:
IMAGE_NAME: ${{ needs.build-image.outputs.name }}
KOSLI_FINGERPRINT: ${{ needs.build-image.outputs.fingerprint }}
KOSLI_ENVIRONMENT: aws-beta
run:
kosli report approval "${IMAGE_NAME}"
--approver="${{ github.actor }}"
deploy-to-beta:
needs: [build-image, approve-deployment-to-beta]
uses: ./.github/workflows/sub_deploy_to_beta.yml
with:
IMAGE_TAG: ${{ needs.build-image.outputs.tag }}
approve-deployment-to-prod:
runs-on: ubuntu-latest
needs: [build-image, deploy-to-beta]
environment:
name: production
url: https://cyber-dojo.org
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Kosli CLI
uses: kosli-dev/setup-cli-action@v2
with:
version: ${{ vars.KOSLI_CLI_VERSION }}
- name: Report approval of deployment to Kosli
env:
IMAGE_NAME: ${{ needs.build-image.outputs.name }}
KOSLI_FINGERPRINT: ${{ needs.build-image.outputs.fingerprint }}
KOSLI_ENVIRONMENT: aws-prod
run:
kosli report approval "${IMAGE_NAME}"
--approver="${{ github.actor }}"
deploy-to-prod:
needs: [build-image, approve-deployment-to-prod]
uses: ./.github/workflows/sub_deploy_to_prod.yml
with:
IMAGE_TAG: ${{ needs.build-image.outputs.tag }}
# The cyberdojo/versioner refresh-env.sh script
# https://github.com/cyber-dojo/versioner/blob/master/sh/refresh-env.sh
# relies on being able to:
# - get the :latest image
# - extract the SHA env-var embedded inside it
# - use the 1st 7 chars of the SHA as a latest-equivalent tag
push-latest:
runs-on: ubuntu-latest
needs: [setup, deploy-to-prod]
permissions:
id-token: write
contents: write
steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: ${{ needs.setup.outputs.aws_region }}
role-duration-seconds: 900
role-session-name: ${{ github.event.repository.name }}
role-to-assume: arn:aws:iam::${{ needs.setup.outputs.aws_account_id }}:role/${{ needs.setup.outputs.gh_actions_iam_role_name }}
mask-aws-account-id: no
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USER }}
password: ${{ secrets.DOCKER_PASS }}
- name: Tag image to :latest and push to Dockerhub Registry
env:
IMAGE_NAME: ${{ needs.setup.outputs.image_name }}
IMAGE_TAG: ${{ needs.setup.outputs.image_tag }}
run: |
docker pull "${IMAGE_NAME}"
docker tag "${IMAGE_NAME}" cyberdojo/${SERVICE_NAME}:${IMAGE_TAG}
docker tag "${IMAGE_NAME}" cyberdojo/${SERVICE_NAME}:latest
docker push cyberdojo/${SERVICE_NAME}:${IMAGE_TAG}
docker push cyberdojo/${SERVICE_NAME}:latest