Skip to content

Commit

Permalink
feat: suppport passing custom environment variables
Browse files Browse the repository at this point in the history
...to service workflows.

Also refactored service workflows to use one common base workflow
similar to the library builds.
  • Loading branch information
stempler committed Nov 15, 2023
1 parent a867ddb commit ee341f2
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 125 deletions.
65 changes: 9 additions & 56 deletions .github/workflows/gradle-service-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,62 +23,15 @@ on:
DOCKER_HUB_EMAIL:
WETF_ARTIFACTORY_USER:
WETF_ARTIFACTORY_PASSWORD:
ENV_VARS: # secret for passing on additional env variables based on https://github.com/orgs/community/discussions/26671#discussioncomment-6776498

jobs:
check:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

#
# Build, test and create Docker image
#

- uses: actions/setup-java@v3
with:
distribution: temurin
java-version: ${{ inputs.java-version }}

- name: Setup Gradle
uses: gradle/gradle-build-action@v2

- name: Build and test with Gradle
env:
ORG_GRADLE_PROJECT_wetfArtifactoryUser: ${{ secrets.WETF_ARTIFACTORY_USER }}
ORG_GRADLE_PROJECT_wetfArtifactoryPassword: ${{ secrets.WETF_ARTIFACTORY_PASSWORD }}
ORG_GRADLE_PROJECT_dockerHubUsername: ${{ secrets.DOCKER_HUB_USERNAME }}
ORG_GRADLE_PROJECT_dockerHubPassword: ${{ secrets.DOCKER_HUB_PASSWORD }}
ORG_GRADLE_PROJECT_dockerHubEmail: ${{ secrets.DOCKER_HUB_EMAIL }}
ORG_GRADLE_PROJECT_dockerHost: "unix:///var/run/docker.sock"
run: ./gradlew clean check dockerTagLatest

#
# Security scans
#

- name: Vulnerability scan
uses: wetransform/gha-trivy@master
with:
image-ref: 'docker.io/${{ inputs.image-tag }}'
junit-test-output: "${{ inputs.multi-module && 'trivy-gha-scan/build/test-results/trivy.xml' || 'build/test-results/trivy.xml' }}" # added to unit test report
report-retention-days: 14

#
# Report on unit tests and critical vulnerabilities
#

# https://github.com/marketplace/actions/junit-report-action
- name: Publish Test Report
uses: mikepenz/action-junit-report@v3
if: always() # always run even if the previous step fails
with:
report_paths: "${{ inputs.multi-module && '*/test-results/**/*.xml' || 'build/test-results/**/*.xml' }}"
require_tests: true

# Workaround for check that is additionally created being associated
# to the wrong workflow/run. Instead not additional check is created.
# See https://github.com/mikepenz/action-junit-report/issues/40
annotate_only: true
detailed_summary: true
fail_on_failure: true # in case of critical security vulnerabilities
uses: ./.github/workflows/gradle-service.yml
with:
gradle-tasks: 'clean check dockerTagLatest'
java-version: ${{ inputs.java-version }}
image-tag: ${{ inputs.image-tag }}
multi-module: ${{ inputs.multi-module }}
notify-failure: false
secrets: inherit
77 changes: 8 additions & 69 deletions .github/workflows/gradle-service-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,75 +25,14 @@ on:
WETF_ARTIFACTORY_PASSWORD:
SLACK_NOTIFICATIONS_BOT_TOKEN:
required: true
ENV_VARS: # secret for passing on additional env variables based on https://github.com/orgs/community/discussions/26671#discussioncomment-6776498

jobs:
publish:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

#
# Build, test and push Docker image
#

- uses: actions/setup-java@v3
with:
distribution: temurin
java-version: ${{ inputs.java-version }}
- name: Setup Gradle
uses: gradle/gradle-build-action@v2
- name: Build and test with Gradle
env:
ORG_GRADLE_PROJECT_wetfArtifactoryUser: ${{ secrets.WETF_ARTIFACTORY_USER }}
ORG_GRADLE_PROJECT_wetfArtifactoryPassword: ${{ secrets.WETF_ARTIFACTORY_PASSWORD }}
ORG_GRADLE_PROJECT_dockerHubUsername: ${{ secrets.DOCKER_HUB_USERNAME }}
ORG_GRADLE_PROJECT_dockerHubPassword: ${{ secrets.DOCKER_HUB_PASSWORD }}
ORG_GRADLE_PROJECT_dockerHubEmail: ${{ secrets.DOCKER_HUB_EMAIL }}
ORG_GRADLE_PROJECT_dockerHost: "unix:///var/run/docker.sock"
run: ./gradlew clean check dockerPushImage dockerPushLatest

#
# Security scans
#

- name: Vulnerability scan
uses: wetransform/gha-trivy@master
with:
image-ref: 'docker.io/${{ inputs.image-tag }}'
junit-test-output: "${{ inputs.multi-module && 'trivy-gha-scan/build/test-results/trivy.xml' || 'build/test-results/trivy.xml' }}" # added to unit
report-retention-days: 90 # default retention

#
# Report on unit tests and critical vulnerabilities
#

# https://github.com/marketplace/actions/junit-report-action
- name: Publish Test Report
uses: mikepenz/action-junit-report@v3
if: always() # always run even if the previous step fails
with:
report_paths: "${{ inputs.multi-module && '*/test-results/**/*.xml' || 'build/test-results/**/*.xml' }}"
require_tests: true
fail_on_failure: true # required for slack notification if only tests fail

# Workaround for check that is additionally created being associated
# to the wrong workflow/run. Instead not additional check is created.
# See https://github.com/mikepenz/action-junit-report/issues/40
annotate_only: true
detailed_summary: true

#
# Report build failure to Slack
#

# https://github.com/marketplace/actions/slack-notify-build
- name: Notify slack fail
if: failure()
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_NOTIFICATIONS_BOT_TOKEN }}
uses: voxmedia/github-action-slack-notify-build@v1
with:
channel: build-failures
status: FAILED
color: danger
uses: ./.github/workflows/gradle-service.yml
with:
gradle-tasks: 'clean check dockerPushImage dockerPushLatest'
java-version: ${{ inputs.java-version }}
image-tag: ${{ inputs.image-tag }}
multi-module: ${{ inputs.multi-module }}
secrets: inherit
144 changes: 144 additions & 0 deletions .github/workflows/gradle-service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
on:
workflow_call:
inputs:
gradle-tasks:
description: Tasks to run
required: true
type: string
java-version:
description: Java version to use for build
required: true
type: string
notify-failure:
description: Notify on build failure to Slack
default: true
type: boolean
expect-tests:
description: If JUnit test results are expected
default: true
type: boolean
image-tag:
description: Image tag to scan after build
required: true
type: string
multi-module:
description: If this is a multi-module project
type: boolean
default: false
secrets:
# GH_PAT:
# required: true
DOCKER_HUB_USERNAME:
required: true
DOCKER_HUB_PASSWORD:
required: true
DOCKER_HUB_EMAIL:
WETF_ARTIFACTORY_USER:
WETF_ARTIFACTORY_PASSWORD:
ENV_VARS: # secret for passing on additional env variables based on https://github.com/orgs/community/discussions/26671#discussioncomment-6776498

jobs:
run:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

# For each environment variable's key-value pair, mask the value from logs.
- name: Populate custom environment variables
id: environment_variables
env:
env_vars: ${{ secrets.ENV_VARS }}
run: |
if [ -z "$env_vars" ]; then
exit 0
else
# Iterate over all key-value pairs passed into this workflow and
# add them as environment variables for use in subsequent steps.
# Keys prefixed with "BASE64_" will be decoded from base64 and the
# prefix will be removed. E.g., "BASE64_KEY=value" becomes "KEY=value".
for i in $env_vars; do
if [[ $i == BASE64_* ]]; then i=$(echo $i | sed 's/^BASE64_//g' | sed 's/=.*//g')=$(echo ${i#*=} | base64 -di | base64 -di); fi
echo ::add-mask::${i#*=}
printf '%s\n' "$i" >> $GITHUB_ENV
done
fi
#
# Build, test and create Docker image
#

- uses: actions/setup-java@v3
with:
distribution: temurin
java-version: ${{ inputs.java-version }}

- name: Setup Gradle
uses: gradle/gradle-build-action@v2

- name: Build and test with Gradle
env:
ORG_GRADLE_PROJECT_wetfArtifactoryUser: ${{ secrets.WETF_ARTIFACTORY_USER }}
ORG_GRADLE_PROJECT_wetfArtifactoryPassword: ${{ secrets.WETF_ARTIFACTORY_PASSWORD }}
ORG_GRADLE_PROJECT_dockerHubUsername: ${{ secrets.DOCKER_HUB_USERNAME }}
ORG_GRADLE_PROJECT_dockerHubPassword: ${{ secrets.DOCKER_HUB_PASSWORD }}
ORG_GRADLE_PROJECT_dockerHubEmail: ${{ secrets.DOCKER_HUB_EMAIL }}
ORG_GRADLE_PROJECT_dockerHost: "unix:///var/run/docker.sock"
run: ./gradlew ${{ inputs.gradle-tasks }}

- name: Upload Gradle test reports
uses: actions/upload-artifact@v3
if: always()
with:
name: Gradle test reports
retention-days: 7
path: |
${{ inputs.multi-module && '*/build/reports/tests' || 'build/reports/tests' }}
#
# Security scans
#

- name: Make sure test-results folder exists
run: mkdir -p ${{ inputs.multi-module && 'trivy-gha-scan/build/test-results' || 'build/test-results' }}

- name: Vulnerability scan
uses: wetransform/gha-trivy@master
with:
image-ref: 'docker.io/${{ inputs.image-tag }}'
junit-test-output: "${{ inputs.multi-module && 'trivy-gha-scan/build/test-results/trivy.xml' || 'build/test-results/trivy.xml' }}" # added to unit test report
report-retention-days: 30

#
# Report on unit tests and critical vulnerabilities
#

# https://github.com/marketplace/actions/junit-report-action
- name: Publish Test Report
uses: mikepenz/action-junit-report@v3
if: always() # always run even if the previous step fails
with:
report_paths: "${{ inputs.multi-module && '*/build/test-results/**/*.xml' || 'build/test-results/**/*.xml' }}"
require_tests: ${{ inputs.expect-tests }}

# Workaround for check that is additionally created being associated
# to the wrong workflow/run. Instead not additional check is created.
# See https://github.com/mikepenz/action-junit-report/issues/40
annotate_only: true
detailed_summary: true
fail_on_failure: true # in case of critical security vulnerabilities, also required for Slack notification if only tests fail

#
# Report build failure to Slack
#

# https://github.com/marketplace/actions/slack-notify-build
- name: Notify slack fail
if: ${{ inputs.notify-failure && failure() }}
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_NOTIFICATIONS_BOT_TOKEN }}
uses: voxmedia/github-action-slack-notify-build@v1
with:
channel: build-failures
status: FAILED
color: danger

0 comments on commit ee341f2

Please sign in to comment.