|
| 1 | +name: Terraform CI/CD - LiveKit Infra |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + paths: |
| 6 | + - 'livekit/infra/**' |
| 7 | + push: |
| 8 | + branches: |
| 9 | + - main |
| 10 | + paths: |
| 11 | + - 'livekit/infra/**' |
| 12 | + |
| 13 | +permissions: |
| 14 | + contents: 'read' # needed by checkout |
| 15 | + id-token: 'write' # only if you use OIDC (not required here) |
| 16 | + |
| 17 | +jobs: |
| 18 | + terraform-plan: |
| 19 | + name: Terraform Plan (PRs & push) |
| 20 | + runs-on: ubuntu-latest |
| 21 | + if: github.event_name == 'pull_request' || github.event_name == 'push' |
| 22 | + steps: |
| 23 | + - name: Checkout repo |
| 24 | + uses: actions/checkout@v4 |
| 25 | + with: |
| 26 | + fetch-depth: 0 |
| 27 | + |
| 28 | + - name: Setup Terraform |
| 29 | + uses: hashicorp/setup-terraform@v2 |
| 30 | + with: |
| 31 | + terraform_version: 1.6.0 |
| 32 | + |
| 33 | + - name: Configure AWS credentials |
| 34 | + uses: aws-actions/configure-aws-credentials@v2 |
| 35 | + with: |
| 36 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 37 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 38 | + aws-region: ${{ secrets.AWS_REGION }} |
| 39 | + |
| 40 | + - name: Create terraform.tfvars from secrets |
| 41 | + working-directory: livekit/infra |
| 42 | + run: | |
| 43 | + # Create terraform.tfvars dynamically (values come from repo secrets) |
| 44 | + cat > terraform.tfvars <<'TFVARS' |
| 45 | + aws_region = "${{ secrets.AWS_REGION }}" |
| 46 | + tfstate_s3_bucket = "${{ secrets.TFSTATE_S3_BUCKET }}" |
| 47 | + key_name = "${{ secrets.TF_VAR_key_name }}" |
| 48 | + allowed_ssh_cidr = "${{ secrets.TF_VAR_allowed_ssh_cidr }}" |
| 49 | + instance_type = "${{ secrets.TF_VAR_instance_type }}" |
| 50 | + vpc_cidr = "${{ secrets.TF_VAR_vpc_cidr }}" |
| 51 | + public_subnet_cidr = "${{ secrets.TF_VAR_public_subnet_cidr }}" |
| 52 | + availability_zone = "${{ secrets.TF_VAR_availability_zone }}" |
| 53 | + ami_id = "${{ secrets.TF_VAR_ami_id }}" |
| 54 | +
|
| 55 | + livekit_api_key = "${{ secrets.TF_VAR_livekit_api_key }}" |
| 56 | + livekit_api_secret = "${{ secrets.TF_VAR_livekit_api_secret }}" |
| 57 | + TFVARS |
| 58 | +
|
| 59 | + - name: Terraform Init (reconfigure) |
| 60 | + working-directory: livekit/infra |
| 61 | + run: terraform init -input=false -reconfigure |
| 62 | + |
| 63 | + - name: Terraform Format Check |
| 64 | + working-directory: livekit/infra |
| 65 | + run: terraform fmt -check -recursive |
| 66 | + |
| 67 | + - name: Terraform Validate |
| 68 | + working-directory: livekit/infra |
| 69 | + run: terraform validate |
| 70 | + |
| 71 | + - name: Terraform Plan |
| 72 | + working-directory: livekit/infra |
| 73 | + run: | |
| 74 | + terraform plan -input=false -var-file="terraform.tfvars" -out=tfplan |
| 75 | + - name: Upload plan artifact (optional) |
| 76 | + if: github.event_name == 'pull_request' |
| 77 | + uses: actions/upload-artifact@v4 |
| 78 | + with: |
| 79 | + name: tfplan |
| 80 | + path: livekit/infra/tfplan |
| 81 | + |
| 82 | + terraform-apply: |
| 83 | + name: Terraform Apply (main) |
| 84 | + runs-on: ubuntu-latest |
| 85 | + needs: terraform-plan |
| 86 | + if: github.ref == 'refs/heads/main' |
| 87 | + steps: |
| 88 | + - name: Checkout repo |
| 89 | + uses: actions/checkout@v4 |
| 90 | + with: |
| 91 | + fetch-depth: 0 |
| 92 | + |
| 93 | + - name: Setup Terraform |
| 94 | + uses: hashicorp/setup-terraform@v2 |
| 95 | + with: |
| 96 | + terraform_version: 1.6.0 |
| 97 | + |
| 98 | + - name: Configure AWS credentials |
| 99 | + uses: aws-actions/configure-aws-credentials@v2 |
| 100 | + with: |
| 101 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 102 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 103 | + aws-region: ${{ secrets.AWS_REGION }} |
| 104 | + |
| 105 | + - name: Create terraform.tfvars from secrets |
| 106 | + working-directory: livekit/infra |
| 107 | + run: | |
| 108 | + cat > terraform.tfvars <<'TFVARS' |
| 109 | + aws_region = "${{ secrets.AWS_REGION }}" |
| 110 | + tfstate_s3_bucket = "${{ secrets.TFSTATE_S3_BUCKET }}" |
| 111 | + key_name = "${{ secrets.TF_VAR_key_name }}" |
| 112 | + allowed_ssh_cidr = "${{ secrets.TF_VAR_allowed_ssh_cidr }}" |
| 113 | + instance_type = "${{ secrets.TF_VAR_instance_type }}" |
| 114 | + vpc_cidr = "${{ secrets.TF_VAR_vpc_cidr }}" |
| 115 | + public_subnet_cidr = "${{ secrets.TF_VAR_public_subnet_cidr }}" |
| 116 | + availability_zone = "${{ secrets.TF_VAR_availability_zone }}" |
| 117 | + ami_id = "${{ secrets.TF_VAR_ami_id }}" |
| 118 | +
|
| 119 | + livekit_api_key = "${{ secrets.TF_VAR_livekit_api_key }}" |
| 120 | + livekit_api_secret = "${{ secrets.TF_VAR_livekit_api_secret }}" |
| 121 | + TFVARS |
| 122 | +
|
| 123 | + - name: Terraform Init (reconfigure) |
| 124 | + working-directory: livekit/infra |
| 125 | + run: terraform init -input=false -reconfigure |
| 126 | + |
| 127 | + - name: Terraform Apply |
| 128 | + working-directory: livekit/infra |
| 129 | + run: terraform apply -input=false -auto-approve -var-file="terraform.tfvars" |
| 130 | + |
| 131 | + - name: Show outputs |
| 132 | + working-directory: livekit/infra |
| 133 | + run: terraform output -json |
0 commit comments