DSF POC CLI - AWS #208
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: 'DSF POC CLI - AWS' | |
on: | |
workflow_call: | |
inputs: | |
use_modules_from_terraform_registry: | |
required: true | |
type: boolean | |
explicit_ref: | |
required: true | |
type: string | |
secrets: | |
AWS_ACCESS_KEY_ID: | |
required: true | |
AWS_SECRET_ACCESS_KEY: | |
required: true | |
SLACK_WEBHOOK_URL: | |
required: true | |
DAM_LICENSE: | |
required: true | |
ALLOWED_SSH_CIDRS: | |
required: true | |
DEPLOYMENT_TAGS: | |
required: true | |
workflow_dispatch: | |
inputs: | |
use_modules_from_terraform_registry: | |
type: boolean | |
required: false | |
delay_destroy: | |
description: 'Delay the destroy step and subsequent steps to allow investigation' | |
type: boolean | |
default: false | |
required: false | |
push: | |
branches: | |
- 'dev' | |
paths: | |
- 'modules/aws/**' | |
- '!modules/aws/sonar-upgrader/**' | |
- '!modules/aws/statistics/*' | |
- 'modules/null/**' | |
- '!modules/null/statistics/*' | |
- 'examples/aws/poc/dsf_deployment/*' | |
pull_request: | |
types: | |
- 'opened' | |
- 'reopened' | |
branches: | |
- 'dev' | |
paths: | |
- 'modules/aws/**' | |
- '!modules/aws/sonar-upgrader/*' | |
- '!modules/aws/statistics/*' | |
- 'modules/null/**' | |
- '!modules/null/statistics/*' | |
- 'examples/aws/poc/dsf_deployment/*' | |
env: | |
TF_CLI_ARGS: "-no-color" | |
TF_INPUT: 0 | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
DESTROY_DELAY_SECONDS: 7200 | |
TF_VAR_additional_tags: ${{ secrets.DEPLOYMENT_TAGS }} | |
permissions: | |
contents: read | |
jobs: | |
prepare-matrix: | |
runs-on: ubuntu-latest | |
outputs: | |
matrix: ${{ steps.set-matrix.outputs.matrix }} | |
steps: | |
- name: Set Matrix | |
run: | | |
MATRIX=$(jq -n --compact-output '{ | |
"include": [ | |
{"name":"DSF POC","workspace":"dsf_cli-all-","enable_sonar":true,"enable_dam":true,"enable_dra":true,"enabled":false}, | |
{"name":"DSF POC - SONAR","workspace":"dsf_cli-sonar-","enable_sonar":true,"enable_dam":false,"enable_dra":false,"enabled":false}, | |
{"name":"DSF POC - DAM","workspace":"dsf_cli-dam-","enable_sonar":false,"enable_dam":true,"enable_dra":false,"enabled":true}, | |
{"name":"DSF POC - DRA","workspace":"dsf_cli-dra-","enable_sonar":false,"enable_dam":false,"enable_dra":true,"enabled":true} | |
] | |
}') | |
echo "matrix=$MATRIX" >> $GITHUB_ENV | |
terraform: | |
needs: prepare-matrix | |
strategy: | |
fail-fast: false | |
matrix: ${{ fromJson(needs.prepare-matrix.outputs.matrix) }} | |
name: '${{ matrix.name }}' | |
runs-on: ubuntu-latest | |
env: | |
EXAMPLE_DIR: ./examples/aws/poc/dsf_deployment | |
AWS_REGION: eu-west-2 | |
TF_VAR_enable_sonar: ${{ matrix.enable_sonar }} | |
TF_VAR_enable_dam: ${{ matrix.enable_dam }} | |
TF_VAR_enable_dra: ${{ matrix.enable_dra }} | |
TF_VAR_allowed_ssh_cidrs: ${{ secrets.ALLOWED_SSH_CIDRS }} | |
environment: test | |
# Use the Bash shell regardless whether the GitHub Actions runner is ubuntu-latest, macos-latest, or windows-latest | |
defaults: | |
run: | |
shell: bash | |
steps: | |
- name: Pick ref | |
run: | | |
if [ -z "${{ inputs.explicit_ref }}" ]; then | |
echo REF=${{ github.ref }} >> $GITHUB_ENV; | |
else | |
echo REF=${{ inputs.explicit_ref }} >> $GITHUB_ENV; | |
fi | |
- name: Set Workspace Name | |
run: | | |
echo "Event Name: ${{ github.event_name }}" | |
if [ ${{ github.event_name }} == 'schedule' ]; then | |
echo TF_WORKSPACE=${{ matrix.workspace }}${{ github.event_name }}-$REF >> $GITHUB_ENV | |
else | |
echo TF_WORKSPACE=${{ matrix.workspace }}${{ github.run_number }}-${{ github.run_attempt }}-${{ inputs.explicit_ref }} >> $GITHUB_ENV | |
echo TMP_WORKSPACE_NAME=${{ matrix.workspace }}${{ github.run_number }}-${{ github.run_attempt }}-${{ inputs.explicit_ref }} >> $GITHUB_ENV | |
fi | |
# Checkout the repository to the GitHub Actions runner | |
- name: Checkout | |
uses: actions/checkout@v3 | |
with: | |
ref: ${{ env.REF }} | |
- name: Change the modules source to local | |
if: ${{ inputs.use_modules_from_terraform_registry == false }} | |
run: | | |
find ./examples/ -type f -exec sed -i -f sed.expr {} \; | |
- name: Create terraform backend file | |
run: | | |
cat << EOF > $EXAMPLE_DIR/backend.tf | |
terraform { | |
backend "s3" { | |
bucket = "terraform-state-bucket-dsfkit-github-tests" | |
key = "states/terraform.tfstate" | |
dynamodb_table = "terraform-state-lock" | |
region = "us-east-1" | |
} | |
} | |
EOF | |
# Install the latest version of Terraform CLI and configure the Terraform CLI configuration file with a Terraform Cloud user API token | |
- name: Setup Terraform | |
uses: hashicorp/setup-terraform@v2 | |
with: | |
terraform_wrapper: false | |
terraform_version: ~1.7.0 | |
- name: Setup jq | |
uses: sergeysova/jq-action@v2 | |
- name: Create License File | |
env: | |
MY_SECRET: ${{ secrets.DAM_LICENSE }} | |
run: | | |
echo "${{ secrets.DAM_LICENSE }}" | base64 -d > $EXAMPLE_DIR/license.mprv | |
cat $EXAMPLE_DIR/license.mprv | |
# Initialize a new or existing Terraform working directory by creating initial files, loading any remote state, downloading modules, etc. | |
- name: Terraform Init | |
run: terraform -chdir=$EXAMPLE_DIR init | |
env: | |
TF_WORKSPACE: default | |
- name: Cleaning environment | |
run: | | |
if [ ${{ github.event_name }} == 'schedule' ]; then | |
mv $EXAMPLE_DIR/main.tf{,_} | |
mv $EXAMPLE_DIR/outputs.tf{,_} | |
mv $EXAMPLE_DIR/dam.tf{,_} | |
mv $EXAMPLE_DIR/dra.tf{,_} | |
mv $EXAMPLE_DIR/sonar.tf{,_} | |
mv $EXAMPLE_DIR/networking.tf{,_} | |
mv $EXAMPLE_DIR/agentless_sources.tf{,_} | |
mv $EXAMPLE_DIR/agent_sources.tf{,_} | |
terraform -chdir=$EXAMPLE_DIR destroy -var dam_license=license.mprv -auto-approve | |
mv $EXAMPLE_DIR/main.tf{_,} | |
mv $EXAMPLE_DIR/outputs.tf{_,} | |
mv $EXAMPLE_DIR/dam.tf{_,} | |
mv $EXAMPLE_DIR/dra.tf{_,} | |
mv $EXAMPLE_DIR/sonar.tf{_,} | |
mv $EXAMPLE_DIR/networking.tf{_,} | |
mv $EXAMPLE_DIR/agentless_sources.tf{_,} | |
mv $EXAMPLE_DIR/agent_sources.tf{_,} | |
fi | |
- name: Terraform Validate | |
run: terraform -chdir=$EXAMPLE_DIR validate | |
# Generates an execution plan for Terraform | |
- name: Terraform Plan | |
run: | | |
terraform -chdir=$EXAMPLE_DIR workspace list | |
terraform -chdir=$EXAMPLE_DIR plan -var dam_license=license.mprv | |
# Send job failure to Slack | |
- name: Send Slack When Failure | |
run: | | |
if [ ${{ env.REF }} == 'master' ]; then | |
curl -X POST -H 'Content-type: application/json' --data '{"text":":exclamation: :exclamation: :exclamation:\n*${{ matrix.name }} Prod ${{ inputs.workspace }} automation Failed*\n<https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}|Please check the job!>\nRun by: ${{ steps.check-trigger.outputs.run-by }}", "channel": "#dsfkit-prod"}' ${{ secrets.SLACK_WEBHOOK_URL }} | |
elif [ ${{ env.REF }} == 'dev' ]; then | |
curl -X POST -H 'Content-type: application/json' --data '{"text":":exclamation: :exclamation: :exclamation:\n*${{ matrix.name }} dev ${{ inputs.workspace }} automation Failed*\n<https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}|Please check the job!>\nRun by: ${{ steps.check-trigger.outputs.run-by }}", "channel": "#edsf_automation"}' ${{ secrets.SLACK_WEBHOOK_URL }} | |
else | |
curl -X POST -H 'Content-type: application/json' --data '{"text":":exclamation: :exclamation: :exclamation:\n*${{ matrix.name }} private branch ${{ inputs.workspace }} automation Failed*\n<https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}|Please check the job!>\nRun by: ${{ steps.check-trigger.outputs.run-by }}", "channel": "#edsf_automation"}' ${{ secrets.SLACK_WEBHOOK_URL }} | |
fi | |
if: ${{ failure() }} | |
# This step allows time for investigation of the failed resources before destroying them | |
- name: Conditional Delay | |
run: | | |
echo "delay_destroy: ${{ inputs.delay_destroy }}" | |
if [ "${{ inputs.delay_destroy }}" == "true" ]; then | |
echo "Terraform workspace: $TF_WORKSPACE" | |
curl -X POST -H 'Content-type: application/json' --data '{"text":":exclamation: :exclamation: :exclamation:\n*${{ github.workflow }} ${{ env.TF_WORKSPACE }} automation Failed*\n You have ${{ env.DESTROY_DELAY_SECONDS }} seconds to investigate the environment before it is destroyed :alarm_clock:\n<https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}|Please check the job!>\nRun by: ${{ steps.check-trigger.outputs.run-by }}", "channel": "#edsf_automation"}' ${{ secrets.SLACK_WEBHOOK_URL }} | |
echo "" | |
echo "Sleeping for $((DESTROY_DELAY_SECONDS / 60)) minutes before destroying the environment" | |
sleep $DESTROY_DELAY_SECONDS | |
fi | |