Skip to content

Deploy Coder to GCP #49

Deploy Coder to GCP

Deploy Coder to GCP #49

name: Deploy Coder to GCP
on:
workflow_dispatch:
env:
TF_DIR: coder/deploy
TF_VAR_project: ${{ vars.GCP_PROJECT_ID }}
TF_VAR_script_path: startup.sh
TF_VAR_region: ${{ vars.GCP_REGION }}
TF_VAR_zone: ${{ vars.GCP_ZONE }}
TF_VAR_machine_type: ${{ vars.GCP_MACHINE_TYPE }}
TF_VAR_service_account_email: ${{ vars.GCP_SERVICE_ACCOUNT }}
TF_VAR_vm_name: coder-entrypoint
jobs:
terraform-plan:
runs-on: ubuntu-latest
outputs:
plan-artifact: tf-artifacts
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Authenticate with GCP
uses: google-github-actions/auth@v2
with:
credentials_json: ${{ secrets.GCP_SA_KEY }}
- name: Set up gcloud CLI
uses: google-github-actions/setup-gcloud@v2
with:
project_id: ${{ vars.GCP_PROJECT_ID }}
- name: Install Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: 1.12.2
- name: Terraform Init
working-directory: ${{ env.TF_DIR }}
run: terraform init -backend-config="bucket=${{ vars.TF_STATE_BUCKET }}"
- name: Replace env variables in startup script
working-directory: ${{ env.TF_DIR }}
run: |
sed -i \
-e "s|<GH_APP_ID>|${{ vars.GH_APP_ID }}|g" \
-e "s|<GH_APP_CLIENT_ID>|${{ secrets.GH_APP_CLIENT_ID }}|g" \
-e "s|<GH_APP_CLIENT_SECRET>|${{ secrets.GH_APP_CLIENT_SECRET }}|g" \
-e "s|<GH_ALLOWED_ORGS>|${{ vars.GH_ALLOWED_ORGS }}|g" \
-e "s|<GH_OAUTH_CLIENT_ID>|${{ secrets.GH_OAUTH_CLIENT_ID }}|g" \
-e "s|<GH_OAUTH_CLIENT_SECRET>|${{ secrets.GH_OAUTH_CLIENT_SECRET }}|g" \
${{ env.TF_VAR_script_path }}
- name: Terraform Plan
working-directory: ${{ env.TF_DIR }}
run: terraform plan -input=false -out=tfplan
- name: Upload Plan Artifacts
uses: actions/upload-artifact@v4
with:
name: tf-artifacts
include-hidden-files: true
path: |
${{ env.TF_DIR }}/tfplan
${{ env.TF_DIR }}/.terraform/
${{ env.TF_DIR }}/.terraform.lock.hcl
terraform-apply:
runs-on: ubuntu-latest
needs: terraform-plan
environment: coder-deploy
steps:
- uses: actions/checkout@v4
- name: Download Terraform Artifacts
uses: actions/download-artifact@v4
with:
name: tf-artifacts
path: ${{ env.TF_DIR }}
- name: Grant execute permissions to Terraform folder
working-directory: ${{ env.TF_DIR }}
run: chmod -R +x .terraform/
- name: Authenticate with GCP
uses: google-github-actions/auth@v2
with:
credentials_json: ${{ secrets.GCP_SA_KEY }}
- name: Set up gcloud CLI
uses: google-github-actions/setup-gcloud@v2
with:
project_id: ${{ vars.GCP_PROJECT_ID }}
- name: Install Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: 1.12.2
- name: Replace env variables in startup script
working-directory: ${{ env.TF_DIR }}
run: |
sed -i \
-e "s|<GH_APP_ID>|${{ vars.GH_APP_ID }}|g" \
-e "s|<GH_APP_CLIENT_ID>|${{ secrets.GH_APP_CLIENT_ID }}|g" \
-e "s|<GH_APP_CLIENT_SECRET>|${{ secrets.GH_APP_CLIENT_SECRET }}|g" \
-e "s|<GH_ALLOWED_ORGS>|${{ vars.GH_ALLOWED_ORGS }}|g" \
-e "s|<GH_OAUTH_CLIENT_ID>|${{ secrets.GH_OAUTH_CLIENT_ID }}|g" \
-e "s|<GH_OAUTH_CLIENT_SECRET>|${{ secrets.GH_OAUTH_CLIENT_SECRET }}|g" \
${{ env.TF_VAR_script_path }}
- name: Terraform Apply
working-directory: ${{ env.TF_DIR }}
run: terraform apply -auto-approve tfplan