Deploy bastion destroy to intg #916
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: Deploy bastion | |
on: | |
workflow_dispatch: | |
inputs: | |
environment: | |
type: choice | |
description: 'The stage to deploy the bastion to' | |
required: true | |
options: | |
- intg | |
- staging | |
- prod | |
default: 'intg' | |
command: | |
required: true | |
description: Whether to apply or destroy the bastion | |
options: | |
- apply | |
- destroy | |
type: choice | |
public-key: | |
required: false | |
description: Public key. This is not needed if you're not going to use ssh tunnelling | |
type: string | |
connect-to-export-efs: | |
required: false | |
description: Allow the bastion to connect to the export EFS volume | |
type: boolean | |
default: false | |
connect-to-database: | |
required: false | |
description: Allow the bastion to connect to the database | |
type: boolean | |
default: false | |
run-name: Deploy bastion ${{inputs.command}} to ${{inputs.environment}} | |
permissions: | |
id-token: write | |
jobs: | |
deploy: | |
environment: ${{ github.event.inputs.environment }} | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
with: | |
submodules: recursive | |
token: ${{ secrets.WORKFLOW_PAT }} | |
- id: set-environment-names | |
run: | | |
import os | |
with open(os.environ['GITHUB_OUTPUT'], 'a') as fh: | |
print(f'title-environment={"${{ github.event.inputs.environment }}".title()}', file=fh) | |
shell: python | |
- uses: nationalarchives/tdr-github-actions/.github/actions/run-git-secrets@main | |
- id: configure-aws-credentials | |
if: ${{ github.event.inputs.command == 'destroy' }} | |
uses: aws-actions/configure-aws-credentials@v1 | |
continue-on-error: true | |
with: | |
role-to-assume: arn:aws:iam::${{ secrets.ACCOUNT_NUMBER }}:role/TDRGithubActionsRunDocumentRole${{ steps.set-environment-names.outputs.title-environment }} | |
aws-region: eu-west-2 | |
role-session-name: DeleteUserRole | |
- name: Run delete user script | |
if: ${{ steps.configure-aws-credentials.outcome == 'success' }} | |
run: aws ssm send-command --targets Key=tag:Name,Values=bastion-ec2-instance-${{ github.event.inputs.environment }} --document-name deleteuser | |
- uses: aws-actions/configure-aws-credentials@v1 | |
if: ${{ github.event.inputs.command == 'apply' && github.event.inputs.connect-to-database == 'true' }} | |
with: | |
role-to-assume: arn:aws:iam::${{ secrets.ACCOUNT_NUMBER }}:role/TDRGithubActionsInvokeCreateUserLambdaRole${{ steps.set-environment-names.outputs.title-environment }} | |
aws-region: eu-west-2 | |
role-session-name: CreateUserRole | |
- name: Create user if connecting to the database | |
if: ${{ github.event.inputs.command == 'apply' && github.event.inputs.connect-to-database == 'true' }} | |
run: aws lambda invoke --function-name tdr-create-bastion-user-${{ github.event.inputs.environment }} out | |
- uses: aws-actions/configure-aws-credentials@v1 | |
with: | |
role-to-assume: arn:aws:iam::${{ secrets.MANAGEMENT_ACCOUNT }}:role/TDRGithubTerraformAssumeRole${{ steps.set-environment-names.outputs.title-environment }} | |
aws-region: eu-west-2 | |
role-session-name: TerraformRole | |
- name: Run Terraform | |
env: | |
TF_VAR_tdr_account_number: ${{ secrets.ACCOUNT_NUMBER }} | |
TF_VAR_public_key: ${{ github.event.inputs.public-key }} | |
TF_VAR_connect_to_database: ${{ github.event.inputs.connect-to-database }} | |
TF_VAR_connect_to_export_efs: ${{ github.event.inputs.connect-to-export-efs }} | |
run: | | |
cd terraform/bastion | |
terraform --version | |
terraform init | |
terraform workspace new ${{ github.event.inputs.environment }} || true | |
terraform workspace select ${{ github.event.inputs.environment }} | |
terraform ${{ github.event.inputs.command }} -auto-approve > /dev/null | |
- uses: nationalarchives/tdr-github-actions/.github/actions/slack-send@main | |
if: failure() | |
with: | |
message: "*Bastion script* :warning: The script to ${{ github.event.inputs.command }} the bastion, run by ${{ github.actor }}, has failed" | |
slack-url: ${{ secrets.SLACK_WEBHOOK }} | |
- uses: nationalarchives/tdr-github-actions/.github/actions/slack-send@main | |
if: success() | |
with: | |
message: "*Bastion instance* The ${{ github.event.inputs.command }} script has been run for the ${{ github.event.inputs.environment }} environment by ${{ github.actor }}" | |
slack-url: ${{ secrets.SLACK_WEBHOOK }} |