Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add tf-account to support multiple customers #1

Merged
merged 127 commits into from
Sep 14, 2023
Merged
Changes from all commits
Commits
Show all changes
127 commits
Select commit Hold shift + click to select a range
5241158
feat: add tf-account to support multiple customers
obs-gh-nikhildua Aug 21, 2023
f401ef7
add debug variable
obs-gh-nikhildua Aug 22, 2023
963b52d
Add -or create
obs-gh-nikhildua Aug 22, 2023
a3cf88b
Update to inline debug + update tf args
obs-gh-nikhildua Aug 22, 2023
1947f33
add workspace
obs-gh-nikhildua Aug 22, 2023
07fe0b8
update workspace step
obs-gh-nikhildua Aug 22, 2023
b114906
update workspace step
obs-gh-nikhildua Aug 22, 2023
782ff92
remove debug
obs-gh-nikhildua Aug 22, 2023
e702ce2
remove debug success flag
obs-gh-nikhildua Aug 22, 2023
d616960
try out addnl customers
obs-gh-nikhildua Aug 23, 2023
932cd69
Revert strategy
obs-gh-nikhildua Aug 23, 2023
6392055
add if
obs-gh-nikhildua Aug 23, 2023
0994f47
remove if
obs-gh-nikhildua Aug 23, 2023
071c255
add output for customers
obs-gh-nikhildua Aug 23, 2023
c7bd5b6
update version
obs-gh-nikhildua Aug 23, 2023
6a5b981
try out other action
obs-gh-nikhildua Aug 23, 2023
cb27b11
update versi
obs-gh-nikhildua Aug 23, 2023
afd6dc2
Revert
obs-gh-nikhildua Aug 23, 2023
cd4684c
add more echo
obs-gh-nikhildua Aug 23, 2023
77446d7
more echo
obs-gh-nikhildua Aug 23, 2023
8395b0b
more echo
obs-gh-nikhildua Aug 23, 2023
fa37720
covner to json
obs-gh-nikhildua Aug 23, 2023
9e25b31
to json
obs-gh-nikhildua Aug 23, 2023
d770757
fix typo
obs-gh-nikhildua Aug 23, 2023
288808a
Debug
obs-gh-nikhildua Aug 23, 2023
d9b366f
string conv
obs-gh-nikhildua Aug 23, 2023
2b04747
Try matrix
obs-gh-nikhildua Aug 23, 2023
cdc35ba
Try matrix
obs-gh-nikhildua Aug 23, 2023
3d68810
update path
obs-gh-nikhildua Aug 23, 2023
5743f83
update
obs-gh-nikhildua Aug 23, 2023
02f2845
fromjson
obs-gh-nikhildua Aug 23, 2023
6481c80
more echo
obs-gh-nikhildua Aug 23, 2023
9294305
build matrix
obs-gh-nikhildua Aug 23, 2023
31352f1
build matrix
obs-gh-nikhildua Aug 23, 2023
bdb67ea
Add json
obs-gh-nikhildua Aug 23, 2023
d641d9b
update matrix
obs-gh-nikhildua Aug 23, 2023
f586e0e
update matrix
obs-gh-nikhildua Aug 23, 2023
1b1bab9
update matrix
obs-gh-nikhildua Aug 23, 2023
cafeb23
update matrix
obs-gh-nikhildua Aug 23, 2023
ac28022
update matrix
obs-gh-nikhildua Aug 23, 2023
50c4df9
update matrix
obs-gh-nikhildua Aug 23, 2023
0caf1c2
update matrix
obs-gh-nikhildua Aug 23, 2023
a9b870d
update matrix
obs-gh-nikhildua Aug 23, 2023
653a9ca
update matrix
obs-gh-nikhildua Aug 23, 2023
ef7a2f8
update matrix
obs-gh-nikhildua Aug 23, 2023
f9b24ce
update matrix
obs-gh-nikhildua Aug 23, 2023
9d3769f
update matrix
obs-gh-nikhildua Aug 23, 2023
cfe63df
update matrix
obs-gh-nikhildua Aug 23, 2023
303f038
Add working matrix flow with default
obs-gh-nikhildua Aug 23, 2023
1987412
update tf-account to support secrets correctly
obs-gh-nikhildua Aug 24, 2023
4907319
update for secret name
obs-gh-nikhildua Aug 24, 2023
b32b562
handle default
obs-gh-nikhildua Aug 24, 2023
012a913
update default case
obs-gh-nikhildua Aug 24, 2023
a7abe15
prints
obs-gh-nikhildua Aug 24, 2023
40d4049
prints
obs-gh-nikhildua Aug 24, 2023
d6fd85b
update writing
obs-gh-nikhildua Aug 24, 2023
05d07b0
remove default customer mention
obs-gh-nikhildua Aug 24, 2023
7537bff
Try out git apply
obs-gh-nikhildua Aug 28, 2023
3fc8943
add overide conditional
obs-gh-nikhildua Aug 29, 2023
b0269a0
fix formatting
obs-gh-nikhildua Aug 29, 2023
5d48b02
update fmti
obs-gh-nikhildua Aug 29, 2023
08930fa
add diff
obs-gh-nikhildua Aug 29, 2023
29f7b12
remove tf diff
obs-gh-nikhildua Aug 29, 2023
bb4cb5b
remove comments
obs-gh-nikhildua Aug 29, 2023
7801db6
fix shebang
obs-gh-nikhildua Aug 29, 2023
93ca76d
update shell
obs-gh-nikhildua Aug 29, 2023
ef62157
Add new overrride
obs-gh-nikhildua Aug 30, 2023
e7ff5d1
temporarily add apply
obs-gh-nikhildua Aug 30, 2023
a80605f
fix workspace name in PR comment
obs-gh-nikhildua Aug 30, 2023
82abed5
add conditional for apply
obs-gh-nikhildua Sep 1, 2023
90ba52b
Remove continue on fail
obs-gh-nikhildua Sep 6, 2023
95fbff4
run plan wo conditional
obs-gh-nikhildua Sep 6, 2023
329f18b
add error disablement for file
obs-gh-nikhildua Sep 6, 2023
4e87888
update to support txt
obs-gh-nikhildua Sep 6, 2023
391e30d
if block to check for plan file
obs-gh-nikhildua Sep 6, 2023
fe02031
try stdout instead of plan
obs-gh-nikhildua Sep 6, 2023
902485b
Formatting
obs-gh-nikhildua Sep 6, 2023
38e323a
fix comment
obs-gh-nikhildua Sep 6, 2023
4ede9a8
Fix echo
obs-gh-nikhildua Sep 6, 2023
203261f
fix formatting
obs-gh-nikhildua Sep 6, 2023
e23164d
formatting fix again
obs-gh-nikhildua Sep 6, 2023
67687da
update var
obs-gh-nikhildua Sep 6, 2023
ce1a562
Remove comment
obs-gh-nikhildua Sep 6, 2023
9ed2818
fix string append
obs-gh-nikhildua Sep 6, 2023
38b278d
fix string formatting
obs-gh-nikhildua Sep 6, 2023
762f0a0
Add var
obs-gh-nikhildua Sep 6, 2023
66d7848
comment out
obs-gh-nikhildua Sep 6, 2023
3239fde
revert back to normal
obs-gh-nikhildua Sep 6, 2023
3440056
add new step
obs-gh-nikhildua Sep 6, 2023
fe9758b
Fix cat
obs-gh-nikhildua Sep 6, 2023
dad42d6
Add testing
obs-gh-nikhildua Sep 6, 2023
a5a2ca1
fix test issues
obs-gh-nikhildua Sep 6, 2023
4b1cc93
fix echo
obs-gh-nikhildua Sep 6, 2023
de90303
add strings
obs-gh-nikhildua Sep 6, 2023
a3f9ff2
echo plan
obs-gh-nikhildua Sep 6, 2023
423a216
echo plan
obs-gh-nikhildua Sep 6, 2023
bc92830
remove #
obs-gh-nikhildua Sep 6, 2023
2c85d14
add in tf show
obs-gh-nikhildua Sep 6, 2023
d0e1c70
Add back show:
obs-gh-nikhildua Sep 6, 2023
4574496
Add back show
obs-gh-nikhildua Sep 6, 2023
b2fed60
Add back show
obs-gh-nikhildua Sep 6, 2023
1112458
Remove extra lines
obs-gh-nikhildua Sep 6, 2023
90264fe
Remove old echo
obs-gh-nikhildua Sep 6, 2023
ce83733
add ;
obs-gh-nikhildua Sep 6, 2023
17fd70b
init empty file
obs-gh-nikhildua Sep 6, 2023
fc26738
always set output regardless
obs-gh-nikhildua Sep 7, 2023
07ed6b4
Test out apply
obs-gh-nikhildua Sep 12, 2023
9229c2b
remvoe apply
obs-gh-nikhildua Sep 12, 2023
c2e59c6
Test destiry
obs-gh-nikhildua Sep 12, 2023
f138089
reset
obs-gh-nikhildua Sep 12, 2023
005231f
feat: update post feedback
obs-gh-nikhildua Sep 12, 2023
f87cbe7
fix: typo in command
obs-gh-nikhildua Sep 12, 2023
07ea046
fix: typo in command
obs-gh-nikhildua Sep 12, 2023
9faf4c5
split workspace line
obs-gh-nikhildua Sep 12, 2023
8f13f96
split workspace line
obs-gh-nikhildua Sep 12, 2023
fff09d7
split workspace line
obs-gh-nikhildua Sep 12, 2023
cd589db
split workspace line
obs-gh-nikhildua Sep 12, 2023
71f96ca
echo override
obs-gh-nikhildua Sep 12, 2023
c6d9946
update workspace
obs-gh-nikhildua Sep 12, 2023
d03d589
Add condition for vars
obs-gh-nikhildua Sep 13, 2023
45afacf
Add condition for vars
obs-gh-nikhildua Sep 13, 2023
1317a9a
fix string
obs-gh-nikhildua Sep 13, 2023
ccce8e3
rmv echos
obs-gh-nikhildua Sep 13, 2023
18b2379
add array for input args
obs-gh-nikhildua Sep 13, 2023
beedf93
add workspaces folder
obs-gh-nikhildua Sep 14, 2023
2bb5761
change to tenants
obs-gh-nikhildua Sep 14, 2023
031df28
update customer folder
obs-gh-nikhildua Sep 14, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
218 changes: 218 additions & 0 deletions .github/workflows/tf-account.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
name: Run terraform on account repo
on:
workflow_call:
secrets:
AWS_ROLE_TO_ASSUME:
required: true
AWS_REGION:
required: true
TF_BACKEND_S3_BUCKET:
required: true
TF_BACKEND_S3_KEY:
required: true
TF_BACKEND_S3_REGION:
required: true
TS_OAUTH_CLIENT_ID:
required: false
TS_OAUTH_SECRET:
required: false

inputs:
terraform-version:
description: 'Terraform version'
required: false
type: string
default: '1.5.4'
tailscale-tags:
required: false
type: string
https-proxy:
required: false
type: string
no-proxy:
required: false
type: string
customers:
required: true
type: string
description: YAML file of customers to apply TF-Account Config to

jobs:
build_matrix:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.yaml2json.outputs.output }}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Convert customer yaml to json
uses: fabasoad/data-format-converter-action@main
id: yaml2json
with:
input: ${{ github.workspace }}/${{ inputs.customers }}
from: "yaml"
to: "json"

terraform:
name: 'Run terraform - ${{ matrix.customers.name }}'
runs-on: ubuntu-latest
# Needed to interact with Github's OIDC token
permissions:
id-token: write
contents: write
pull-requests: write
strategy:
fail-fast: false
matrix: ${{ fromJson(needs.build_matrix.outputs.matrix) }}
needs:
- build_matrix
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup Terraform
uses: hashicorp/setup-terraform@v2
with:
terraform_version: ${{ inputs.terraform-version }}

- name: Tailscale
uses: tailscale/github-action@v2
# a tailscale oauth client requires tags
if: inputs.tailscale-tags != ''
with:
oauth-client-id: ${{ secrets.TS_OAUTH_CLIENT_ID }}
oauth-secret: ${{ secrets.TS_OAUTH_SECRET }}
tags: ${{ inputs.tailscale-tags }}

- name: Config Proxy Environment Variables
if: inputs.https-proxy != ''
# intermediate variables to avoid injection attack
# https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#using-an-intermediate-environment-variable
env:
HTTPS_PROXY: ${{ inputs.https-proxy }}
NO_PROXY: ${{ inputs.no-proxy }}
run: |
printf "HTTPS_PROXY=%s\n" "$HTTPS_PROXY" >> "$GITHUB_ENV"
printf "NO_PROXY=%s\n" "$NO_PROXY" >> "$GITHUB_ENV"

- name: Create Override file (non-default customer)
if: matrix.customers.name != 'default'
shell: bash
run: |
echo "::debug::${{ toJson(matrix) }}"
sudo apt-get install --quiet=2 --assume-yes jo
if [ -n "${{ matrix.customers.user_email }}" ] && [ -n "${{matrix.customers.user_password_actions_secret_name}}" ]; then
jo -p provider=$(jo \
observe[]=$(jo \
customer=${{ matrix.customers.customer }} \
domain=${{ matrix.customers.domain }} \
user_email=${{ matrix.customers.user_email }} \
user_password=${{secrets[matrix.customers.user_password_actions_secret_name]}}))
else
jo -p provider=$(jo \
observe[]=$(jo \
customer=${{ matrix.customers.customer }} \
domain=${{ matrix.customers.domain }}))
fi > override.tf.json

- name: Terraform Format
id: fmt
run: terraform fmt -check

- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v2
with:
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
role-session-name: ${{ github.sha }}
aws-region: ${{ secrets.AWS_REGION }}

- name: Terraform Init
id: init
run: |
terraform init -no-color \
-backend-config="bucket=${{ secrets.TF_BACKEND_S3_BUCKET }}" \
-backend-config="key=${{ secrets.TF_BACKEND_S3_KEY }}" \
-backend-config="region=${{ secrets.TF_BACKEND_S3_REGION }}" \
-backend-config="workspace_key_prefix=${{ github.event.repository.name }}"

- name: Terraform Validate
id: validate
run: terraform validate -no-color

- name: Terraform Workspace Configure
id: workspace
obs-gh-nikhildua marked this conversation as resolved.
Show resolved Hide resolved
run: |
terraform workspace select -or-create=true ${{ matrix.customers.name }}
echo "workspace=${{ matrix.customers.name }}" >> $GITHUB_OUTPUT

- name: Terraform Plan
id: plan
if: github.event_name == 'pull_request'
run: |
path='plan'
txt_path='plan.txt'
var_file=customers/${{ steps.workspace.outputs.workspace}}.tfvars
touch $txt_path && echo "path=${txt_path}" >> $GITHUB_OUTPUT
plan_args=()
if [ -e "$var_file" ]; then
plan_args+=("-var-file=$var_file")
fi
terraform plan "${plan_args[@]}" -no-color -out="$path"
terraform show -no-color "$path" > "$txt_path"
echo "path=${txt_path}" >> $GITHUB_OUTPUT
continue-on-error: true

- uses: actions/upload-artifact@v3
if: steps.plan.outcome == 'success'
with:
name: plan
path: ${{ steps.plan.outputs.path }}

- name: Add Plan to Job Summary
if: steps.plan.outcome == 'success'
run: |
{
echo '# Terraform Plan'
echo '```'
cat "${{ steps.plan.outputs.path }}"
echo '```'
} >> "$GITHUB_STEP_SUMMARY"

- name: Create Pull Request Comment
uses: actions/github-script@v6
if: github.event_name == 'pull_request'
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs')
const MAX_PLAN_LENGTH = 60000 // Max comment length is 65536
const plan = '```\n' + fs.readFileSync('${{ steps.plan.outputs.path }}', 'utf8') + '\n```'

const workflowSummaryURL = '${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}'

const output = `#### Workspace \`${{ steps.workspace.outputs.workspace}}\`
#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\`
#### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\`
#### Terraform Validation 🤖\`${{ steps.validate.outcome }}\`
#### Terraform Plan 📖\`${{ steps.plan.outcome }}\`
<details><summary>Show <a href="${workflowSummaryURL}">Plan</a></summary>

${plan.length <= MAX_PLAN_LENGTH ? plan : `_The plan is too large to include in a comment, open the [workflow summary](${workflowSummaryURL}) to view it._`}
</details>

**Pusher**: @${{ github.actor }}, **Action**: \`${{ github.event_name }}\``;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})

- name: Terraform Plan Status
if: steps.plan.outcome == 'failure'
run: exit 1

- name: Terraform Apply
if: github.ref == format('refs/heads/{0}', github.event.repository.default_branch)
run: terraform apply -auto-approve