[Workflow] Cleanup PR Workspaces #21181
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: "[Workflow] Cleanup PR Workspaces" | |
on: | |
schedule: | |
# every hour | |
- cron: '0 * * * *' | |
permissions: | |
contents: read | |
security-events: none | |
pull-requests: none | |
actions: none | |
checks: none | |
deployments: none | |
issues: none | |
packages: none | |
repository-projects: none | |
statuses: none | |
jobs: | |
terraform_environment_cleanup: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v2 | |
with: | |
fetch-depth: '0' | |
- name: Extract branch name | |
run: | | |
if [ "${{ github.head_ref }}" == "" ]; then | |
echo BRANCH_NAME=main >> $GITHUB_ENV | |
else | |
echo BRANCH_NAME=${{ github.head_ref }} >> $GITHUB_ENV | |
fi | |
id: extract_branch | |
- uses: unfor19/install-aws-cli-action@v1 | |
- uses: hashicorp/setup-terraform@v2 | |
with: | |
terraform_version: 1.2.2 | |
terraform_wrapper: false | |
- name: Configure AWS Credentials For Terraform | |
uses: aws-actions/configure-aws-credentials@v1 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_ACTIONS }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_ACTIONS }} | |
aws-region: eu-west-1 | |
role-duration-seconds: 3600 | |
role-session-name: OPGMaintenanceTerraformGithubAction | |
- uses: webfactory/[email protected] | |
with: | |
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY_ALLOW_LIST_REPOSITORY }} | |
- name: Install Terraform Workspace Manager | |
run: | | |
wget https://github.com/ministryofjustice/opg-terraform-workspace-manager/releases/download/v0.3.2/opg-terraform-workspace-manager_Linux_x86_64.tar.gz -O $HOME/terraform-workspace-manager.tar.gz | |
sudo tar -xvf $HOME/terraform-workspace-manager.tar.gz -C /usr/local/bin | |
sudo chmod +x /usr/local/bin/terraform-workspace-manager | |
- name: Terraform Init | |
run: terraform init -input=false | |
working-directory: ./terraform/environment | |
- name: Destroy PR Terraform Workspaces | |
working-directory: ./terraform/environment | |
run: | | |
../../scripts/workspace_cleanup.sh $(terraform-workspace-manager -protected-workspaces=true -aws-account-id=679638075911 -aws-iam-role=opg-maintenance-ci) |