-
Notifications
You must be signed in to change notification settings - Fork 0
100 lines (97 loc) · 3.53 KB
/
deploy.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
name: CICD with Terraform
on:
push:
branches:
- main
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
PRIVATE_SSH_KEY: ${{ secrets.AWS_SSH_KEY_PRIVATE }}
PUBLIC_SSH_KEY: ${{ secrets.AWS_SSH_KEY_PUBLIC }}
TF_STATE_BUCKET_NAME: ${{ secrets.TF_STATE_BUCKET_NAME }}
jobs:
deploy-infra:
runs-on: ubuntu-latest
outputs:
EC2_PUBLIC_IP: ${{ steps.set-ip.outputs.web_server_public_ip }}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: setup Terraform
uses: hashicorp/setup-terraform@v2
with:
terraform_wrapper: false
- name: Terraform Format
id: fmt
run: terraform fmt -check
- name: Terraform init
id: init
run: terraform init #-backend-config="bucket=$TF_STATE_BUCKET_NAME" -backend-config="region=us-east-1"
working-directory: ./terraform
- name: Terraform Validate
id: validate
run: terraform validate -no-color
- name: Terraform Plan
id: plan
run: |-
terraform plan -destroy \
-var="public_key=$PUBLIC_SSH_KEY" \
-var="private_key=$PRIVATE_SSH_KEY" \
-var="key_name=terraformawskey" \
-out=PLAN
working-directory: ./terraform
- name: Terraform Apply
id: apply
run: terraform apply PLAN
working-directory: ./terraform
- name: Set IP for EC2 instance
id: set-ip
run: echo "web_server_public_ip=$(terraform output web_server_public_ip)" >> $GITHUB_OUTPUT
working-directory: ./terraform
deploy-app:
runs-on: ubuntu-latest
needs: deploy-infra
steps:
- name: Get the EC2 IP from job deploy-infra
run: |-
echo ${{needs.deploy-infra.outputs.EC2_PUBLIC_IP}}
echo "EC2_PUBLIC_IP=${{needs.deploy-infra.outputs.EC2_PUBLIC_IP}}" >> $GITHUB_ENV
- name: Checkout
uses: actions/checkout@v3
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-region: us-east-1
- name: Login to AWS ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Build and Push docker image
env:
REGISTRY: ${{ steps.login-ecr.outputs.registry }}
REPOSITORY: example-node-app
IMAGE_TAG: ${{ github.sha }}
run: |-
docker build -t $REGISTRY/$REPOSITORY:$IMAGE_TAG .
docker push $REGISTRY/$REPOSITORY:$IMAGE_TAG
working-directory: ./nodeapp
- name: Deploy Docker Image to EC2
env:
REGISTRY: ${{ steps.login-ecr.outputs.registry }}
REPOSITORY: example-node-app
IMAGE_TAG: ${{ github.sha }}
AWS_DEFAULT_REGION: us-east-1
uses: appleboy/ssh-action@master
with:
host: ${{ env.EC2_PUBLIC_IP }}
username: ubuntu
key: ${{ env.PRIVATE_SSH_KEY }}
envs: PRIVATE_SSH_KEY,REGISTRY,REPOSITORY,IMAGE_TAG,AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY,AWS_DEFAULT_REGION,AWS_REGION
script: |-
sudo apt update
sudo apt install docker.io -y
sudo apt install awscli -y
sudo $(aws ecr get-login --no-include-email --region us-east-1);
sudo docker stop myappcontainer || true
sudo docker rm myappcontainer || true
sudo docker pull $REGISTRY/$REPOSITORY:$IMAGE_TAG
sudo docker run -d --name myappcontainer -p 80:3000 $REGISTRY/$REPOSITORY:$IMAGE_TAG