Skip to content

Populating additional tags (#437) #129

Populating additional tags (#437)

Populating additional tags (#437) #129

Workflow file for this run

name: 'Sonar 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
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:
- 'examples/aws/poc/dsf_deployment/*'
- 'examples/aws/poc/sonar_basic_deployment/*'
- 'examples/aws/poc/sonar_hadr_deployment/*'
pull_request:
types:
- 'opened'
- 'reopened'
branches:
- 'dev'
paths:
- 'examples/aws/poc/dsf_deployment/*'
- 'examples/aws/poc/sonar_basic_deployment/*'
- 'examples/aws/poc/sonar_hadr_deployment/*'
env:
TF_CLI_ARGS: "-no-color"
TF_INPUT: 0
TF_VAR_gw_count: 1
TF_VAR_allowed_ssh_cidrs: ${{ secrets.ALLOWED_SSH_CIDRS }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
DESTROY_DELAY_SECONDS: 1800
permissions:
contents: read
jobs:
terraform:
strategy:
fail-fast: false
matrix:
include:
- name: Sonar Basic CLI
workspace: sonar_basic_cli-${{ github.run_number }}-${{ github.run_attempt }}
example_dir: ./examples/aws/poc/sonar_basic_deployment
aws_region: eu-west-2
hub_output: '.dsf_hub.value.public_dns'
gw_output: '.dsf_agentless_gw.value."agentless-gw-0".private_ip'
- name: Sonar HADR CLI
workspace: sonar_hadr_cli-${{ github.run_number }}-${{ github.run_attempt }}
example_dir: ./examples/aws/poc/sonar_hadr_deployment
aws_region: eu-west-1
hub_output: '.dsf_hubs.value.main.public_dns'
gw_output: '.dsf_agentless_gw.value."agentless-gw-0".main.private_ip'
name: 'Terraform ${{ matrix.workspace }}'
runs-on: ubuntu-latest
env:
EXAMPLE_DIR: ${{ matrix.example_dir }}
TF_WORKSPACE: ${{ matrix.workspace }}
AWS_REGION: ${{ matrix.aws_region }}
HUB_OUTPUT: ${{ matrix.hub_output }}
GW_OUTPUT: ${{ matrix.gw_output }}
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
# 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 $EXAMPLE_DIR -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
# 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: Terraform Create Workspace
run: terraform -chdir=$EXAMPLE_DIR workspace new ${{ matrix.workspace }}
- 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
# On push to "main", build or change infrastructure according to Terraform configuration files
# Note: It is recommended to set up a required "strict" status check in your repository for "Terraform Cloud". See the documentation on "strict" required status checks for more information: https://help.github.com/en/github/administering-a-repository/types-of-required-status-checks
- name: Terraform Apply
# if: github.ref == 'refs/heads/"master"' && github.event_name == 'push' || github.event_name == 'workflow_dispatch'
run: terraform -chdir=$EXAMPLE_DIR apply -auto-approve
- name: Terraform Output
if: always()
run: terraform -chdir=$EXAMPLE_DIR output -json
- name: Collect Artifacts
id: collect-artifacts
if: always()
uses: actions/upload-artifact@v4
with:
name: collected-keys-${{ env.TF_WORKSPACE }}
path: |
${{ env.EXAMPLE_DIR }}/ssh_keys
- name: Set Environment Variables
run: |
echo hub_ip=$(terraform -chdir=$EXAMPLE_DIR output -json | jq -r $HUB_OUTPUT) >> $GITHUB_ENV
echo agentless_gw_ip=$(terraform -chdir=$EXAMPLE_DIR output -json | jq -r $GW_OUTPUT) >> $GITHUB_ENV
echo dsf_ssh_key_file_name=$EXAMPLE_DIR/$(terraform -chdir=$EXAMPLE_DIR output -json | jq -r '.dsf_private_ssh_key_file_path.value') >> $GITHUB_ENV
# Test the ssh connectivity to the Hub and GW, it checks both of them because the connection to the GW is allowed from the Hub only via proxy
- name: Check SSH Connectivity
uses: appleboy/[email protected]
with:
proxy_host: ${{ env.hub_ip }}
proxy_username: ec2-user
proxy_key_path: ${{ env.dsf_ssh_key_file_name }}
proxy_port: 22
host: ${{ env.agentless_gw_ip }}
username: ec2-user
key_path: ${{ env.dsf_ssh_key_file_name }}
port: 22
script: ip addr | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'
- name: Check how was the workflow run
id: check-trigger
if: ${{ failure() }}
run: |
if [ "${{ github.event_name }}" == "schedule" ]; then
echo "run-by=Automation" >> $GITHUB_OUTPUT
else
echo "run-by=${{ github.actor }}" >> $GITHUB_OUTPUT
fi
# This step allows time for investigation of the failed resources before destroying them
- name: Conditional Delay
if: ${{ failure() }}
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
- name: Terraform Destroy
id: destroy
if: always()
run: terraform -chdir=$EXAMPLE_DIR destroy -auto-approve
- name: Terraform Delete Workspace
if: always()
run: |
if [ '${{ steps.destroy.conclusion }}' == 'success' ]
then
terraform -chdir=$EXAMPLE_DIR workspace delete ${{ matrix.workspace }}
fi
env:
TF_WORKSPACE: default
# Send job failure to Slack
- name: Send Slack by action-slack
uses: 8398a7/action-slack@v3
with:
status: ${{ job.status }}
fields: author,action,eventName,ref,workflow,pullRequest
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
if: ${{ failure() }}