generated from divnix/digga
-
Notifications
You must be signed in to change notification settings - Fork 2
222 lines (217 loc) · 7.73 KB
/
terraform.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
name: "Terraform"
on:
push:
branches:
- "main"
pull_request:
branches:
- "main"
workflow_dispatch:
concurrency: push-to-main
env:
SOPS_AGE_KEY: ${{ secrets.SOPS_AGE_KEY }}
DOTFILES_DIR: dotfiles
SECRETS_DIR: infrastructure-secrets
jobs:
plan:
runs-on: ubuntu-latest
outputs:
diff: ${{ steps.plan.outputs.diff }}
steps:
- name: Checkout
uses: actions/checkout@main
with:
path: dotfiles
- name: Checkout infrastructure-secrets
uses: actions/checkout@main
with:
repository: linyinfeng/infrastructure-secrets
ref: main
path: infrastructure-secrets
token: "${{ secrets.PAT_FOR_AUTOMATED_UPDATE }}"
- name: Install nix
uses: cachix/install-nix-action@master
with:
nix_path: nixpkgs=channel:nixos-unstable
extra_nix_config: |
experimental-features = nix-command flakes
access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}
- name: Setup cachix
uses: cachix/cachix-action@master
with:
name: linyinfeng
signingKey: "${{ secrets.CACHIX_SIGNING_KEY }}"
- name: Cache terraform
uses: actions/cache@main
with:
path: dotfiles/terraform/.terraform/providers
key: ${{ runner.os }}-terraform-providers-${{ hashFiles('terraform/.terraform.lock.hcl') }}
- name: Terraform init
run: |
nix develop ./dotfiles --command terraform-init
- name: Terraform plan
id: plan
run: |
mkdir logs
set +e
nix develop ./dotfiles --command terraform-wrapper plan -out="$PWD/terraform.plan" -detailed-exitcode \
> >(sed 's/\x1B\[[0-9;]\{1,\}[A-Za-z]//g' | tee logs/terraform-plan.stdout.log) \
2> >(sed 's/\x1B\[[0-9;]\{1,\}[A-Za-z]//g' | tee logs/terraform-plan.stderr.log >&2)
exit_code="${PIPESTATUS[0]}"
set -e
if [ "$exit_code" -eq 0 ]; then
# success, empty diff
echo "diff=false" >>"$GITHUB_OUTPUT"
elif [ "$exit_code" -eq 2 ]; then
# success, non-empty diff
echo "diff=true" >>"$GITHUB_OUTPUT"
else
exit "$exit_code"
fi
- name: Import GPG key
if: steps.plan.outputs.diff == 'true'
run: |
gpg --import dotfiles/nixos/profiles/users/yinfeng/_pgp/pub.asc
- name: Encrypt plan
if: steps.plan.outputs.diff == 'true'
run: |
mkdir encrypted_plan
nix develop ./dotfiles --command bash -c "sops --encrypt terraform.plan > encrypted_plan/terraform.plan"
- name: Upload plan
if: steps.plan.outputs.diff == 'true'
uses: actions/upload-artifact@main
with:
name: plan
path: encrypted_plan
- name: Upload logs
uses: actions/upload-artifact@main
with:
name: logs-plan
path: logs
- name: Notify
if: steps.plan.outputs.diff == 'true'
run: |
set -e
nix develop ./dotfiles --command sops exec-file infrastructure-secrets/terraform-outputs.yaml 'yq ".ntfy_sh_topic_secret.value" "{}" > ntfy-topic-secret'
stdout_log_url=$(curl -F "c=@logs/terraform-plan.stdout.log" https://pb.li7g.com)
stderr_log_url=$(curl -F "c=@logs/terraform-plan.stderr.log" https://pb.li7g.com)
curl \
--header "Title: Terraform" \
--header "Tags: terraform" \
--data "Terraform planned
stdout: $stdout_log_url
stderr: $stderr_log_url" \
"https://ntfy.sh/main-$(cat ntfy-topic-secret)"
apply:
runs-on: ubuntu-latest
environment: infrastructure
needs: [plan]
if: |
needs.plan.outputs.diff == 'true' &&
github.ref == 'refs/heads/main'
steps:
- name: Checkout
uses: actions/checkout@main
with:
path: dotfiles
token: "${{ secrets.PAT_FOR_AUTOMATED_UPDATE }}"
- name: Checkout infrastructure-secrets
uses: actions/checkout@main
with:
repository: linyinfeng/infrastructure-secrets
ref: main
path: infrastructure-secrets
token: "${{ secrets.PAT_FOR_AUTOMATED_UPDATE }}"
- name: Install nix
uses: cachix/install-nix-action@master
with:
nix_path: nixpkgs=channel:nixos-unstable
extra_nix_config: |
experimental-features = nix-command flakes
access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}
- name: Setup cachix
uses: cachix/cachix-action@master
with:
name: linyinfeng
signingKey: "${{ secrets.CACHIX_SIGNING_KEY }}"
- name: Cache terraform
uses: actions/cache@main
with:
path: dotfiles/terraform/.terraform/providers
key: ${{ runner.os }}-terraform-providers-${{ hashFiles('terraform/.terraform.lock.hcl') }}
- name: Terraform init
run: |
nix develop ./dotfiles --command terraform-init
- name: Download plan
uses: actions/download-artifact@main
with:
name: plan
path: encrypted_plan
- name: Download logs
uses: actions/download-artifact@main
with:
name: logs-plan
path: logs
- name: Decrypt plan
run: |
nix develop ./dotfiles --command sops --decrypt encrypted_plan/terraform.plan > terraform.plan
- name: Terraform apply
run: |
nix develop ./dotfiles --command terraform-wrapper apply "$(realpath terraform.plan)" \
> >(sed 's/\x1B\[[0-9;]\{1,\}[A-Za-z]//g' | tee logs/terraform-apply.stdout.log) \
2> >(sed 's/\x1B\[[0-9;]\{1,\}[A-Za-z]//g' | tee logs/terraform-apply.stderr.log >&2)
- name: Terraform update outputs
if: always()
run: |
nix develop ./dotfiles --command terraform-update-outputs
- name: Terraform extract data
if: always()
run: |
nix develop ./dotfiles --command terraform-outputs-extract-data
- name: Terraform extract secrets
if: always()
run: |
nix develop ./dotfiles --command terraform-outputs-extract-secrets
- name: Format
if: always()
run: |
cd dotfiles
nix fmt
- name: Commit and push
if: always()
run: |
set -e
root=$(pwd)
for repo in "dotfiles" "infrastructure-secrets"; do
echo "commit and push '$repo'..."
cd "$root/$repo"
if [ -z "$(git status --porcelain)" ]; then
echo "repository is clean, skip commit and push"
else
git config --global user.email "[email protected]"
git config --global user.name "Nano"
git add --all
git commit --message "Terraform apply"
git push
fi
done
- name: Upload logs
if: always()
uses: actions/upload-artifact@main
with:
name: logs-apply
path: logs
- name: Notify
if: always()
run: |
set -e
nix develop ./dotfiles --command sops exec-file infrastructure-secrets/terraform-outputs.yaml 'yq ".ntfy_sh_topic_secret.value" "{}" > ntfy-topic-secret'
stdout_log_url=$(curl -F "c=@logs/terraform-apply.stdout.log" https://pb.li7g.com)
stderr_log_url=$(curl -F "c=@logs/terraform-apply.stderr.log" https://pb.li7g.com)
curl \
--header "Title: Terraform" \
--header "Tags: terraform" \
--data "Terraform applied
stdout: $stdout_log_url
stderr: $stderr_log_url" \
"https://ntfy.sh/main-$(cat ntfy-topic-secret)"