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

Configure terraform api #139

Merged
merged 41 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
46531f5
ci: config docker build step to collect image name
italopessoa Sep 11, 2024
68e2583
use DOCKER_METADATA_OUTPUT_TAGS
italopessoa Sep 11, 2024
0d4edcc
add api_docker_image to vars file
italopessoa Sep 11, 2024
f6a99ed
please, just work
italopessoa Sep 11, 2024
d2e0a89
lets see if this works
italopessoa Sep 11, 2024
325bd7c
go
italopessoa Sep 12, 2024
7bdf970
read from output
italopessoa Sep 12, 2024
e856923
create api.auto.tfvars
italopessoa Sep 12, 2024
253e910
this is the right one
italopessoa Sep 12, 2024
752ca21
ci: add terraform on the pipeline
italopessoa Sep 12, 2024
b22f36c
update tf variables
italopessoa Sep 12, 2024
07df968
fix terraform plan
italopessoa Sep 12, 2024
7737004
add secrets to env
italopessoa Sep 12, 2024
7a14289
TEST
italopessoa Sep 12, 2024
a4f33c4
extract secrets
italopessoa Sep 12, 2024
8e03a43
just echo
italopessoa Sep 12, 2024
1807cff
such a boring meeting
italopessoa Sep 12, 2024
559e3eb
gogogo
italopessoa Sep 12, 2024
b3705d2
maybe this works
italopessoa Sep 12, 2024
c86837a
carai, nao vi o echo
italopessoa Sep 12, 2024
341e9b2
I wanna see it
italopessoa Sep 12, 2024
aaf62c9
boraaaa
italopessoa Sep 12, 2024
9e99e2b
small mistakes
italopessoa Sep 12, 2024
a17ac75
extract content on tfvars
italopessoa Sep 12, 2024
211b6f7
test
italopessoa Sep 12, 2024
acafdb7
vars
italopessoa Sep 12, 2024
84dd57d
show me the file
italopessoa Sep 12, 2024
f25eb05
a new approach
italopessoa Sep 12, 2024
f6d6882
nao e possivel jesus
italopessoa Sep 12, 2024
d85212c
vamo ve se nao tem agora
italopessoa Sep 12, 2024
9f38d5a
anything should work
italopessoa Sep 12, 2024
20e2d17
tudo empty
italopessoa Sep 12, 2024
a96fe9f
mysql
italopessoa Sep 12, 2024
497d5c5
i don't understand
italopessoa Sep 12, 2024
16bb4ae
x
italopessoa Sep 12, 2024
838f165
rds not x
italopessoa Sep 12, 2024
232462d
puta que pariu, nao acredito que era so isso
italopessoa Sep 12, 2024
83d1558
fix tf variables
italopessoa Sep 12, 2024
b554868
remove rds variable
italopessoa Sep 12, 2024
57b0677
fix issuer
italopessoa Sep 12, 2024
a92657f
fix docker build step
italopessoa Sep 12, 2024
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
60 changes: 55 additions & 5 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ env:
REGISTRY: ghcr.io
# github.repository as <account>/<repo>
IMAGE_NAME: ${{ github.repository }}/api
TF_CLOUD_ORGANIZATION: "${{ vars.BMB_TF_ORGANIZATION }}"
TF_API_TOKEN: "${{ secrets.TF_API_TOKEN }}"
TF_WORKSPACE: "${{ vars.TF_WORKSPACE }}"
CONFIG_DIRECTORY: "./tf"

jobs:
build:
Expand Down Expand Up @@ -71,15 +75,17 @@ jobs:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

build-docker-image:
needs: [sonarcloud]
if: github.ref == 'refs/heads/main'
needs: [build]
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
# This is used to complete the identity challenge
# with sigstore/fulcio when running outside of PRs.
id-token: write
outputs:
API_IMAGE_TAG: ${{ fromJson(steps.meta.outputs.json).tags[0] }}

steps:
- name: Checkout repository
Expand Down Expand Up @@ -131,11 +137,55 @@ jobs:
cache-from: type=gha
cache-to: type=gha,mode=

create-infrastructure:
create-app:
if: github.ref == 'refs/heads/main'
needs: [build-docker-image]
name: "Terraform Apply"
runs-on: ubuntu-latest
environment: dev
permissions:
contents: read
steps:
- uses: actions/checkout@v4
- name: Terraform goes here
run: echo WRAU!
- name: Checkout
uses: actions/checkout@v4

- name: Create .auto.tfvars file
env:
API_IMAGE_TAG: ${{needs.build-docker-image.outputs.API_IMAGE_TAG}}
run: |
cat <<EOF > tf/api.auto.tfvars
eks_cluster_name = "${{ vars.BMB_EKS_CLUSTER_NAME }}"
apgw_name ="${{ vars.BMB_AUTH_API_NAME }}"
mercadopago_webhook_secret = "${{ secrets.BMB_MERCADO_PAGO_WH_SECRET }}"
mercadopago_accesstoken = "${{ secrets.BMB_MERCADO_PAGO_ACCESS_TOKEN }}"
jwt_signing_key = "${{ secrets.BMB_JWT_SECRET_KEY }}"
jwt_issuer = "${{ vars.BMB_JWT_ISSUER }}"
jwt_aud = "${{ vars.BMB_JWT_AUDIENCE }}"
api_docker_image = "${{ env.API_IMAGE_TAG }}"
internal_elb_name = "${{ vars.BMB_INTERNAL_LB_NAME }}"
db_user = "${{ secrets.BMB_MYSQL_USER }}"
db_pwd = "${{ secrets.BMB_MYSQL_PASSWORD }}"
rds_cluster_identifier = "${{ vars.BMB_MYSQL_CLUSTER }}"
EOF

- name: Upload Configuration
uses: hashicorp/tfc-workflows-github/actions/[email protected]
id: apply-upload
with:
workspace: ${{ env.TF_WORKSPACE }}
directory: ${{ env.CONFIG_DIRECTORY }}

- name: Create Apply Run
uses: hashicorp/tfc-workflows-github/actions/[email protected]
id: apply-run
with:
workspace: ${{ env.TF_WORKSPACE }}
configuration_version: ${{ steps.apply-upload.outputs.configuration_version_id }}
message: "Create Run from GitHub Actions CI ${{ github.sha }}"

- uses: hashicorp/tfc-workflows-github/actions/[email protected]
if: ${{ vars.TF_AUTO_APPROVE == 'true' }}
id: apply
with:
run: ${{ steps.apply-run.outputs.run_id }}
comment: "Confirmed from GitHub Actions CI ${{ github.sha }}"
97 changes: 97 additions & 0 deletions .github/workflows/terraform-plan.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
name: "Terraform Plan"

on:
pull_request:

env:
TF_CLOUD_ORGANIZATION: "${{ vars.BMB_TF_ORGANIZATION }}"
TF_API_TOKEN: "${{ secrets.TF_API_TOKEN }}"
TF_WORKSPACE: "${{ vars.TF_WORKSPACE }}"
CONFIG_DIRECTORY: "./tf"

jobs:

terraform:
environment: dev
name: "Terraform Plan"
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Create .auto.tfvars file
run: |
cat <<EOF > tf/api.auto.tfvars
eks_cluster_name = "${{ vars.BMB_EKS_CLUSTER_NAME }}"
apgw_name = "${{ vars.BMB_JWT_ISSUER }}"
mercadopago_webhook_secret = "${{ secrets.BMB_MERCADO_PAGO_WH_SECRET}}"
mercadopago_accesstoken = "${{ secrets.BMB_MERCADO_PAGO_ACCESS_TOKEN }}"
jwt_signing_key = "${{ secrets.BMB_JWT_SECRET_KEY }}"
jwt_issuer = "${{ vars.BMB_JWT_ISSUER }}"
jwt_aud = "${{ vars.BMB_JWT_AUDIENCE }}"
internal_elb_name = "${{ vars.BMB_INTERNAL_LB_NAME }}"
db_user = "${{ secrets.BMB_MYSQL_USER }}"
db_pwd = "${{ secrets.BMB_MYSQL_PASSWORD }}"
rds_cluster_identifier = "${{ vars.BMB_MYSQL_CLUSTER }}"
EOF

- name: Upload Configuration
uses: hashicorp/tfc-workflows-github/actions/[email protected]
id: plan-upload
with:
workspace: ${{ env.TF_WORKSPACE }}
directory: ${{ env.CONFIG_DIRECTORY }}
speculative: true

- name: Create Plan Run
uses: hashicorp/tfc-workflows-github/actions/[email protected]
id: plan-run
with:
workspace: ${{ env.TF_WORKSPACE }}
configuration_version: ${{ steps.plan-upload.outputs.configuration_version_id }}
plan_only: true

- name: Get Plan Output
uses: hashicorp/tfc-workflows-github/actions/[email protected]
id: plan-output
with:
plan: ${{ fromJSON(steps.plan-run.outputs.payload).data.relationships.plan.data.id }}

- name: Update PR
uses: actions/github-script@v7
id: plan-comment
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
// 1. Retrieve existing bot comments for the PR
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const botComment = comments.find(comment => {
return comment.user.type === 'Bot' && comment.body.includes('Terraform Cloud Plan Output')
});
const output = `#### Terraform Cloud Plan Output
\`\`\`
Plan: ${{ steps.plan-output.outputs.add }} to add, ${{ steps.plan-output.outputs.change }} to change, ${{ steps.plan-output.outputs.destroy }} to destroy.
\`\`\`
[Terraform Cloud Plan](${{ steps.plan-run.outputs.run_link }})
`;
// 3. Delete previous comment so PR timeline makes sense
if (botComment) {
github.rest.issues.deleteComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
});
}
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
});
Loading
Loading