|
| 1 | +name: "Setup K8s and Terraform" |
| 2 | +description: "Common setup for Kubernetes cluster access and Terraform initialization" |
| 3 | + |
| 4 | +inputs: |
| 5 | + cluster: |
| 6 | + description: "The cluster to deploy to (e.g., aztec-gke-private or kind)" |
| 7 | + required: true |
| 8 | + namespace: |
| 9 | + description: "The namespace to deploy to" |
| 10 | + required: true |
| 11 | + ref: |
| 12 | + description: "The branch name to deploy from" |
| 13 | + required: false |
| 14 | + default: "next" |
| 15 | + region: |
| 16 | + description: "GCP region" |
| 17 | + required: false |
| 18 | + default: "us-west1-a" |
| 19 | + gcp_sa_key: |
| 20 | + description: "GCP service account JSON key" |
| 21 | + required: true |
| 22 | + kubeconfig_b64: |
| 23 | + description: "Base64 encoded kubeconfig for kind clusters" |
| 24 | + required: false |
| 25 | + terraform_dir: |
| 26 | + description: "Terraform working directory" |
| 27 | + required: true |
| 28 | + tf_state_bucket: |
| 29 | + description: "Terraform state bucket for GCS backend" |
| 30 | + required: false |
| 31 | + default: "aztec-terraform" |
| 32 | + tf_state_prefix: |
| 33 | + description: "Terraform state prefix for GCS backend" |
| 34 | + required: true |
| 35 | + additional_state_path: |
| 36 | + description: "Additional path component for state (e.g., salt value)" |
| 37 | + required: false |
| 38 | + default: "" |
| 39 | + run_terraform_destroy: |
| 40 | + description: "Whether to run terraform destroy" |
| 41 | + required: false |
| 42 | + default: "false" |
| 43 | + |
| 44 | +outputs: |
| 45 | + kubectl_context: |
| 46 | + description: "The current kubectl context" |
| 47 | + value: ${{ steps.setup_vars.outputs.kubectl_context }} |
| 48 | + |
| 49 | +runs: |
| 50 | + using: "composite" |
| 51 | + steps: |
| 52 | + - name: Check if directory exists |
| 53 | + id: check_dir |
| 54 | + shell: bash |
| 55 | + run: | |
| 56 | + if [ -d ".git" ]; then |
| 57 | + echo "exists=true" >> $GITHUB_OUTPUT |
| 58 | + else |
| 59 | + echo "exists=false" >> $GITHUB_OUTPUT |
| 60 | + fi |
| 61 | +
|
| 62 | + - name: Checkout code |
| 63 | + if: ${{ steps.check_dir.outputs.exists != 'true' }} |
| 64 | + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 |
| 65 | + with: |
| 66 | + ref: ${{ inputs.ref }} |
| 67 | + |
| 68 | + - name: Authenticate to Google Cloud |
| 69 | + uses: google-github-actions/auth@6fc4af4b145ae7821d527454aa9bd537d1f2dc5f |
| 70 | + with: |
| 71 | + credentials_json: ${{ inputs.gcp_sa_key }} |
| 72 | + |
| 73 | + - name: Set up Cloud SDK |
| 74 | + uses: google-github-actions/setup-gcloud@6189d56e4096ee891640bb02ac264be376592d6a |
| 75 | + |
| 76 | + - name: Install GKE Auth Plugin |
| 77 | + shell: bash |
| 78 | + run: | |
| 79 | + gcloud components install gke-gcloud-auth-plugin --quiet |
| 80 | +
|
| 81 | + - name: Configure kubectl with GKE cluster |
| 82 | + if: ${{ inputs.cluster != 'kind' }} |
| 83 | + shell: bash |
| 84 | + run: | |
| 85 | + gcloud container clusters get-credentials ${{ inputs.cluster }} --region ${{ inputs.region }} |
| 86 | +
|
| 87 | + - name: Configure kubectl with kind cluster |
| 88 | + if: ${{ inputs.cluster == 'kind' }} |
| 89 | + shell: bash |
| 90 | + run: | |
| 91 | + if [ -z "${{ inputs.kubeconfig_b64 }}" ]; then |
| 92 | + echo "KUBECONFIG_B64 is not set" |
| 93 | + exit 1 |
| 94 | + fi |
| 95 | + mkdir -p $HOME/.kube |
| 96 | + echo "${{ inputs.kubeconfig_b64 }}" | base64 -d > $HOME/.kube/config |
| 97 | + kubectl config use-context kind-kind |
| 98 | +
|
| 99 | + - name: Set up kubectl context |
| 100 | + id: setup_vars |
| 101 | + shell: bash |
| 102 | + run: | |
| 103 | + CLUSTER_CONTEXT=$(kubectl config current-context) |
| 104 | + echo "kubectl_context=${CLUSTER_CONTEXT}" >> $GITHUB_OUTPUT |
| 105 | + echo "TF_VAR_K8S_CLUSTER_CONTEXT=${CLUSTER_CONTEXT}" >> $GITHUB_ENV |
| 106 | +
|
| 107 | + - name: Setup Terraform |
| 108 | + uses: hashicorp/setup-terraform@v3 |
| 109 | + with: |
| 110 | + terraform_version: "1.5.0" |
| 111 | + |
| 112 | + - name: Terraform Init |
| 113 | + shell: bash |
| 114 | + working-directory: ${{ inputs.terraform_dir }} |
| 115 | + run: | |
| 116 | + # Clean up any previous backend overrides |
| 117 | + rm -f backend_override.tf |
| 118 | +
|
| 119 | + # Build the state path |
| 120 | + STATE_PATH="${{ inputs.cluster }}/${{ inputs.namespace }}" |
| 121 | + if [ -n "${{ inputs.additional_state_path }}" ]; then |
| 122 | + STATE_PATH="${STATE_PATH}/${{ inputs.additional_state_path }}" |
| 123 | + fi |
| 124 | +
|
| 125 | + if [ "${{ inputs.cluster }}" == "kind" ]; then |
| 126 | + # For kind, use local backend |
| 127 | + cat > backend_override.tf << EOF |
| 128 | + terraform { |
| 129 | + backend "local" { |
| 130 | + path = "state/${STATE_PATH}/terraform.tfstate" |
| 131 | + } |
| 132 | + } |
| 133 | + EOF |
| 134 | + else |
| 135 | + # For GKE, use GCS backend |
| 136 | + cat > backend_override.tf << EOF |
| 137 | + terraform { |
| 138 | + backend "gcs" { |
| 139 | + bucket = "${{ inputs.tf_state_bucket }}" |
| 140 | + prefix = "${{ inputs.tf_state_prefix }}/${{ inputs.region }}/${STATE_PATH}/terraform.tfstate" |
| 141 | + } |
| 142 | + } |
| 143 | + EOF |
| 144 | + fi |
| 145 | +
|
| 146 | + terraform init -reconfigure |
| 147 | +
|
| 148 | + - name: Terraform Destroy |
| 149 | + if: ${{ inputs.run_terraform_destroy == 'true' }} |
| 150 | + shell: bash |
| 151 | + working-directory: ${{ inputs.terraform_dir }} |
| 152 | + continue-on-error: true |
| 153 | + run: | |
| 154 | + terraform destroy -auto-approve |
0 commit comments