-
Notifications
You must be signed in to change notification settings - Fork 3
133 lines (116 loc) · 4.61 KB
/
deploy-opentofu.yml
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
name: Deploy OpenTofu
on:
push:
branches:
- '**'
- '!main'
paths:
- 'web/deploy/terraform/**'
- '.github/workflows/deploy-opentofu.yml'
workflow_dispatch:
inputs:
development-environment:
description: Development environment to deploy to.
required: true
default: staging
type: choice
options:
- staging
- production
pull_request:
branches:
- main
types:
- closed
paths:
- 'web/deploy/terraform/**'
- '.github/workflows/deploy-opentofu.yml'
env:
working_directory_parent: ./web/deploy/terraform
TF_VAR_AWS_ACCOUNT_ID: ${{ secrets.AWS_ACCOUNT_ID }}
permissions:
id-token: write
contents: read
jobs:
set-development-environment:
runs-on: ubuntu-latest
name: Set development environment
outputs:
development-environment: ${{ steps.set-development-environment.outputs.development-environment }}
steps:
- name: Set development environment
id: set-development-environment
run: |
if [[ "${{ github.event_name }}" == 'workflow_dispatch' && "${{ inputs.development-environment }}" == 'production' ]]; then
echo 'development-environment=production' >> "$GITHUB_OUTPUT"
else
echo 'development-environment=staging' >> "$GITHUB_OUTPUT"
fi
cat "$GITHUB_OUTPUT"
cat "$GITHUB_OUTPUT" | grep 'development-environment'
deploy-shared-resources:
needs: [set-development-environment]
if: ${{ always() && !cancelled() && needs.set-development-environment.result == 'success' && needs.set-development-environment.outputs.development-environment == 'staging' }}
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/[email protected]
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: ${{ secrets.AWS_REGION }}
role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/github-actions-role-shared
- name: Setup OpenTofu
uses: opentofu/setup-opentofu@v1
- name: Initialize shared resources
working-directory: ${{ env.working_directory_parent }}/shared
run: |
tofu init
- name: Plan resources
working-directory: ${{ env.working_directory_parent }}/shared
run: |
tofu plan -no-color -detailed-exitcode -out=tfplan
continue-on-error: true
- name: Deploy shared resources
if: ${{ github.event_name != 'push' }}
working-directory: ${{ env.working_directory_parent }}/shared
run: |
tofu apply -no-color -auto-approve tfplan
deploy-environments:
needs: [deploy-shared-resources, set-development-environment]
runs-on: ubuntu-latest
if: ${{ always() && !cancelled() && (needs.deploy-shared-resources.result == 'success' || (needs.deploy-shared-resources.result == 'skipped' && github.event_name == 'workflow_dispatch')) }}
env:
TF_VAR_PUBLIC_KEY: ${{ secrets.SSH_PUBLIC_KEY }}
steps:
- name: Checkout code
uses: actions/[email protected]
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: ${{ secrets.AWS_REGION }}
role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/github-actions-role-shared
- name: Setup OpenTofu
uses: opentofu/setup-opentofu@v1
- name: Initialize resources
working-directory: ${{ env.working_directory_parent }}/${{ needs.set-development-environment.outputs.development-environment }}
run: |
tofu init
- name: Plan resources
working-directory: ${{ env.working_directory_parent }}/${{ needs.set-development-environment.outputs.development-environment }}
run: |
tofu plan -no-color -detailed-exitcode -out=tfplan
continue-on-error: true
- name: Deploy resources
if: ${{ github.event_name != 'push' }}
working-directory: ${{ env.working_directory_parent }}/${{ needs.set-development-environment.outputs.development-environment }}
run: |
tofu apply -no-color -auto-approve tfplan
deploy-docker:
needs: [deploy-environments, set-development-environment]
if: ${{ always() && !cancelled() && needs.deploy-environments.result == 'success' && github.event_name != 'push' }}
name: Push and deploy Docker images to EC2
uses: ./.github/workflows/deploy-docker.yml
secrets: inherit
with:
development-environment: ${{ needs.set-development-environment.outputs.development-environment }}