Skip to content

Commit f32a17f

Browse files
authored
Merge pull request #29 from sivarkpocrk/test-auto
Test auto
2 parents 443f3f5 + dc03bf5 commit f32a17f

File tree

2 files changed

+75
-11
lines changed

2 files changed

+75
-11
lines changed

.github/workflows/terraform.yml

Lines changed: 74 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Terraform Setup and Destroy
1+
name: Terraform Setup, Deploy, and Destroy
22

33
on:
44
workflow_dispatch:
@@ -19,25 +19,52 @@ on:
1919
- destroy
2020

2121
jobs:
22-
terraform:
23-
name: Terraform Setup or Destroy
22+
test-lint:
23+
uses: ./.github/workflows/test-and-lint.yml
24+
name: Test and Lint
25+
secrets:
26+
DOCKERHUB_USER: ${{ vars.DOCKERHUB_USER }}
27+
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
28+
29+
setup:
30+
name: Terraform Setup
2431
runs-on: ubuntu-22.04
32+
needs: [test-lint]
2533

2634
steps:
2735
- name: Checkout Code
2836
uses: actions/checkout@v4
2937

30-
- name: Set Up Terraform
38+
- name: Set Vars
39+
run: |
40+
if [[ $GITHUB_REF == 'refs/heads/prod' ]]; then
41+
echo "prod" > .workspace
42+
else
43+
echo "staging" > .workspace
44+
fi
45+
46+
- name: Install Terraform
3147
run: |
32-
sudo apt-get update -y
33-
sudo apt-get install -y terraform
48+
if terraform -version &>/dev/null; then
49+
echo "Terraform is already installed."
50+
terraform -version
51+
else
52+
echo "Terraform not found. Installing..."
53+
sudo apt-get update -y
54+
sudo apt-get install -y gnupg software-properties-common curl
55+
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
56+
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
57+
sudo apt-get update -y
58+
sudo apt-get install -y terraform
59+
terraform -version
60+
fi
3461
3562
- name: Configure AWS Credentials
3663
uses: aws-actions/configure-aws-credentials@v4
3764
with:
38-
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
39-
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
40-
aws-region: us-east-1
65+
aws-access-key-id: ${{ vars.AWS_ACCESS_KEY_ID }}
66+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
67+
aws-region: us-east-1
4168

4269
- name: Terraform Init for Setup
4370
run: |
@@ -56,17 +83,52 @@ jobs:
5683
terraform destroy -auto-approve
5784
fi
5885
86+
deploy:
87+
name: Deploy
88+
runs-on: ubuntu-22.04
89+
needs: [setup]
90+
91+
steps:
92+
- name: Checkout Code
93+
uses: actions/checkout@v4
94+
95+
- name: Set Vars
96+
run: |
97+
if [[ $GITHUB_REF == 'refs/heads/prod' ]]; then
98+
echo "prod" > .workspace
99+
else
100+
echo "staging" > .workspace
101+
fi
102+
103+
- name: Push to ECR
104+
env:
105+
AWS_ACCESS_KEY_ID: ${{ vars.AWS_ACCESS_KEY_ID }}
106+
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
107+
run: |
108+
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin ${{ vars.AWS_ACCOUNT_ID }}.dkr.ecr.us-east-1.amazonaws.com
109+
docker build --compress -t ${{ vars.ECR_REPO_APP }}:$GITHUB_SHA .
110+
docker push ${{ vars.ECR_REPO_APP }}:$GITHUB_SHA
111+
docker build --compress -t ${{ vars.ECR_REPO_PROXY }}:$GITHUB_SHA proxy/
112+
docker push ${{ vars.ECR_REPO_PROXY }}:$GITHUB_SHA
113+
59114
- name: Terraform Init for Deploy
60115
run: |
61116
cd infra/deploy
62117
terraform init
63118
64119
- name: Terraform Apply/Destroy for Deploy
65120
env:
121+
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
122+
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
123+
TF_VAR_db_password: ${{ secrets.TF_VAR_DB_PASSWORD }}
124+
TF_VAR_django_secret_key: ${{ secrets.TF_VAR_DJANGO_SECRET_KEY }}
66125
TF_WORKSPACE: ${{ github.event.inputs.environment }}
67126
run: |
127+
export TF_VAR_ecr_app_image="${{ vars.ECR_REPO_APP }}:$GITHUB_SHA"
128+
export TF_VAR_ecr_proxy_image="${{ vars.ECR_REPO_PROXY }}:$GITHUB_SHA"
129+
workspace=$(cat .workspace)
68130
cd infra/deploy
69-
terraform workspace select $TF_WORKSPACE || terraform workspace new $TF_WORKSPACE
131+
terraform workspace select $workspace || terraform workspace new $workspace
70132
if [ "${{ github.event.inputs.action }}" == "apply" ]; then
71133
terraform apply -auto-approve
72134
else
@@ -75,6 +137,8 @@ jobs:
75137
76138
- name: Cleanup Workspace (if destroyed)
77139
if: ${{ github.event.inputs.action == 'destroy' }}
140+
env:
141+
TF_WORKSPACE: ${{ github.event.inputs.environment }}
78142
run: |
79143
cd infra/deploy
80144
terraform workspace select default

infra/setup/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ terraform {
1111
key = "tf-state-setup"
1212
region = "us-east-1"
1313
encrypt = true
14-
dynamodb_table = "devops-siva-recp-app-tf-lock"
14+
dynamodb_table = "devops-sivas-recp-app-tf-lock"
1515
}
1616
}
1717

0 commit comments

Comments
 (0)