-
Notifications
You must be signed in to change notification settings - Fork 0
150 lines (126 loc) · 4.41 KB
/
ci.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
name: CI
on:
push:
branches:
- feature/**
env:
AWS_ROLE_ARN: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/${{ secrets.IAM_ROLE_NAME }}
AWS_DEFAULT_REGION: ap-northeast-1
AWS_ACCOUNT_ID: ${{ secrets.AWS_ACCOUNT_ID }}
permissions:
id-token: write
contents: write
pull-requests: write
jobs:
setup:
runs-on: ubuntu-latest
outputs:
modules_changed: ${{ steps.modules_changes.outputs.changes }}
envs_changed: ${{ steps.envs_changes.outputs.changes }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Get changed modules dirs
uses: dorny/paths-filter@v2
id: modules_changes
with:
filters: |
modules:
- 'modules/**'
- name: Get changed envs dirs
uses: dorny/paths-filter@v2
id: envs_changes
with:
filters: |
environments:
- 'environments/**'
terragrunt:
needs: setup
if: needs.setup.outputs.envs_changed != '[]'
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup aqua
uses: aquaproj/[email protected]
with:
aqua_version: v2.28.0
aqua_opts: ""
- name: Install tools via Aqua
run: aqua install
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v3
with:
role-to-assume: ${{ env.AWS_ROLE_ARN }}
aws-region: ${{ env.AWS_DEFAULT_REGION }}
- name: Initialize Terragrunt
working-directory: environments/prod
run: terragrunt run-all init --terragrunt-non-interactive
- name: Check Terragrunt format
working-directory: environments/prod
run: terragrunt run-all fmt -check
- name: Validate Terragrunt
working-directory: environments/prod
run: terragrunt run-all validate
- name: Run Terragrunt plan
working-directory: environments/prod
run: terragrunt run-all plan
terraform:
needs: setup
if: needs.setup.outputs.modules_changed != '[]'
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup aqua
uses: aquaproj/[email protected]
with:
aqua_version: v2.28.0
aqua_opts: ""
- name: Install tools via Aqua
run: aqua install
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v3
with:
role-to-assume: ${{ env.AWS_ROLE_ARN }}
aws-region: ${{ env.AWS_DEFAULT_REGION }}
- name: Run TFsec
env:
REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
tfsec --format=checkstyle | reviewdog -f=checkstyle -name="tfsec" -reporter=github-pr-review -filter-mode=nofilter -fail-on-error
- name: Run TFlint
env:
REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
tflint --init
tflint --format=checkstyle | reviewdog -f=checkstyle -name="tflint" -reporter=github-pr-review -filter-mode=nofilter -fail-on-error
release-pull-request:
needs: [terragrunt, terraform]
if: needs.setup.outputs.envs_changed != '[]' || needs.setup.outputs.modules_changed != '[]'
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set Tokyo Time
id: set_tokyo_time
run: echo "tokyo_time=$(TZ=Asia/Tokyo date '+%Y-%m-%d %H:%M:%S')" >> $GITHUB_ENV
- name: Determine if PR already exists
id: check_pr
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
PR_COUNT=$(gh pr list --head "${{ github.ref_name }}" --json number --jq '. | length')
echo "::set-output name=pr_count::$PR_COUNT"
- name: Create Pull Request
if: steps.check_pr.outputs.pr_count == '0'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh pr create \
-B main \
-H "${{ github.ref_name }}" \
-t "Automated Release - ${{ env.tokyo_time }}" \
-b "This automated PR merges the feature branch '${{ github.ref_name }}' into 'main' and includes the following updates:\n\n- Security checks\n- Format checks\n\nPlease review and approve the changes." \
-a "${{ github.actor }}"
shell: bash