Skip to content

Commit 2e3891b

Browse files
authored
[Feature] Add github action workflow (sidpalas#1)
1 parent 0374577 commit 2e3891b

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

.github/workflows/terraform.yml

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ on:
44
push:
55
branches:
66
- main
7+
tags:
8+
- v\d+\.\d+\.\d+$
79
pull_request:
810

911
jobs:
@@ -36,7 +38,7 @@ jobs:
3638
- name: Terraform Plan
3739
id: plan
3840
if: github.event_name == 'pull_request'
39-
run: terraform plan -no-color
41+
run: terraform plan -var db_pass=${{secrets.DB_PASS }} -no-color
4042
continue-on-error: true
4143

4244
- uses: actions/[email protected]
@@ -70,6 +72,20 @@ jobs:
7072
if: steps.plan.outcome == 'failure'
7173
run: exit 1
7274

73-
# - name: Terraform Apply
74-
# if: github.ref == 'refs/heads/main' && github.event_name == 'push'
75-
# run: terraform apply -auto-approve
75+
- name: Check tag
76+
id: check-tag
77+
run: |
78+
if [[ ${{ github.ref }} =~ ^refs/tags/vd+\.d+\.d+$ ]]; then echo ::set-output name=environment::production
79+
elif [[ github.ref == 'refs/heads/main' ]]; then echo ::set-output name=environment::staging
80+
else echo ::set-output name=environment::unknown
81+
fi
82+
83+
- name: Terraform Apply Staging
84+
if: steps.check-tag.outputs.environment == 'production' && github.event_name == 'push'
85+
working-directory: 07-managing-multiple-environments/file-structure/staging
86+
run: terraform apply -var db_pass=${{secrets.DB_PASS }} -auto-approve
87+
88+
- name: Terraform Apply Production
89+
if: steps.check-tag.outputs.environment == 'staging' && github.event_name == 'push'
90+
working-directory: 07-managing-multiple-environments/file-structure/production
91+
run: terraform apply -var db_pass=${{secrets.DB_PASS }} -auto-approve
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
terraform {
2+
# Assumes s3 bucket and dynamo DB table already set up
3+
# See /code/03-basics/aws-backend
4+
backend "s3" {
5+
bucket = "devops-directive-tf-state"
6+
key = "07-managing-multiple-environments/global/terraform.tfstate"
7+
region = "us-east-1"
8+
dynamodb_table = "terraform-state-locking"
9+
encrypt = true
10+
}
11+
12+
required_providers {
13+
aws = {
14+
source = "hashicorp/aws"
15+
version = "~> 3.0"
16+
}
17+
}
18+
}
19+
20+
provider "aws" {
21+
region = "us-east-1"
22+
}
23+
24+
# Route53 zone is shared across staging and production
25+
resource "aws_route53_zone" "primary" {
26+
name = "mysuperawesomesite.com"
27+
}

07-managing-multiple-environments/file-structure/production/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ module "web_app" {
3939
domain = "mysuperawesomesite.com"
4040
environment_name = local.environment_name
4141
instance_type = "t2.small"
42-
create_dns_zone = true
42+
create_dns_zone = false
4343
db_name = "${local.environment_name}mydb"
4444
db_user = "foo"
4545
db_pass = var.db_pass

0 commit comments

Comments
 (0)