-
Notifications
You must be signed in to change notification settings - Fork 0
107 lines (86 loc) · 3.49 KB
/
deploy.yml
File metadata and controls
107 lines (86 loc) · 3.49 KB
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
101
102
103
104
105
106
107
name: Deploy
# Trigger on CI workflow completion
on:
pull_request:
branches: main
env:
AWS_REGION: us-east-1 # set this to your preferred AWS region, e.g. us-west-1
ECR_REPOSITORY_LAMBDA: lambda-repo
permissions:
contents: read
jobs:
deploy-lambda:
name: Deploy Lambda Functions (Docker)
runs-on: ubuntu-latest
defaults:
run:
shell: bash
# Only run job if the current repository
# starts with the right prefix
if: startsWith(github.head_ref, 'deploy/')
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- name: Printout project name
run: echo $PROJECT_NAME >> $GITHUB_ENV
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Create ECR repository
run: |
AWS_ECR_REPOSITORY_NAME=${{ env.ECR_REPOSITORY_LAMBDA }}
if ! aws ecr describe-repositories --repository-names $AWS_ECR_REPOSITORY_NAME > /dev/null 2>&1; then
aws ecr create-repository --repository-name $AWS_ECR_REPOSITORY_NAME --image-scanning-configuration scanOnPush=true
fi
- name: Build, tag, and push image to Amazon ECR (Lambda Functions)
id: build-image
uses: docker/build-push-action@v3
with:
context: .
file: ./lambda_functions/docker/Dockerfile
push: true
tags: ${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY_LAMBDA}}:${{ github.sha }}
terraform:
name: 'Terraform (IaC)'
needs: deploy-lambda
runs-on: ubuntu-latest
environment: production
defaults:
run:
shell: bash
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- name: Printout project name env
run: echo "PROJECT_NAME=$(echo $PROJECT_NAME)" >> $GITHUB_ENV
- name: Setup Terraform
uses: hashicorp/setup-terraform@v1
with:
cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}
- name: Terraform Init
run: |
AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text) && \
cd terraform && terraform init \
-backend-config="region=$AWS_REGION" \
-backend-config='assume_role={"role_arn":"arn:aws:iam::'$AWS_ACCOUNT_ID':role/terraform_state_role"}'
- name: Terraform Format
run: make tf-fmt
- name: Terraform Plan
run: make tf-plan
# On push to "main", build or change infrastructure according to Terraform configuration files
# Note: It is recommended to set up a required "strict" status check in your repository for "Terraform Cloud". See the documentation on "strict" required status checks for more information: https://help.github.com/en/github/administering-a-repository/types-of-required-status-checks
- name: Terraform Apply
# if: github.ref == 'refs/heads/"main"' && github.event_name == 'push'
run: make tf-deploy