fix: on-dispatch only works for default branch xd #6
Workflow file for this run
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: Manage Kubernetes Cluster | |
on: | |
push: | |
tags: | |
- "destroy-*" # This pattern will match tags starting with 'destroy-' | |
jobs: | |
deploy-terraform: | |
runs-on: ubuntu-latest | |
defaults: | |
run: | |
working-directory: ./terraform | |
steps: | |
- uses: actions/checkout@v2 | |
- name: Install Terraform | |
id: install-terraform | |
run: wget -O terraform.zip https://releases.hashicorp.com/terraform/1.5.5/terraform_1.5.5_linux_amd64.zip && unzip terraform.zip && chmod +x terraform && sudo mv terraform /usr/local/bin | |
- name: Set Operation Mode | |
id: set-operation-mode | |
run: | | |
if [[ "${GITHUB_REF}" == refs/tags/destroy-* ]]; then | |
echo "OPERATION=destroy" >> $GITHUB_ENV | |
else | |
echo "OPERATION=apply" >> $GITHUB_ENV | |
- name: Terraform Init | |
id: terraform-init | |
run: terraform init -backend-config="bucket=tf-state-${{ secrets.PROJECT_ID }}" | |
env: | |
GOOGLE_CREDENTIALS: ${{ secrets.GCP_CREDENTIALS }} | |
- name: Terraform Workspace | |
id: terraform-workspace | |
run: terraform workspace select ${GITHUB_REF##*/} || terraform workspace new ${GITHUB_REF##*/} | |
env: | |
GOOGLE_CREDENTIALS: ${{ secrets.GCP_CREDENTIALS }} | |
- name: Terraform Apply or Destroy | |
id: terraform-apply-destroy | |
run: | | |
if [ "${{ env.OPERATION }}" = "destroy" ]; then | |
terraform destroy -auto-approve -var="project_id=${{ secrets.PROJECT_ID }}" -var="branch=${GITHUB_REF##*/}" | |
else | |
terraform apply -auto-approve -var="project_id=${{ secrets.PROJECT_ID }}" -var="branch=${GITHUB_REF##*/}" | |
env: | |
GOOGLE_CREDENTIALS: ${{ secrets.GCP_CREDENTIALS }} |