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

Reusable GHA for Terraform deploy #323

Merged
merged 74 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from 68 commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
414cf6c
saving reusable actions
Sep 30, 2024
3b42fba
tf deploy
Oct 1, 2024
819ee60
started docker reusablity
Oct 3, 2024
8cbbcd4
Merge branch 'main' into reusable-actions-combine-frontend-api
Oct 8, 2024
31d06bc
init
Oct 8, 2024
0af8c27
token
Oct 8, 2024
48d9557
registry
Oct 8, 2024
2b5c20d
version
Oct 8, 2024
e819775
try outcomes
Oct 8, 2024
f4a0e5d
try outcomes
Oct 8, 2024
fc7655c
try outcomes
Oct 8, 2024
aa24451
try outcomes
Oct 8, 2024
3c1f03a
try outcomes
Oct 8, 2024
6feb648
rm inspect
Oct 8, 2024
9a4f9c7
put auth prior
Oct 8, 2024
e3e1ac4
use cli
Oct 8, 2024
0cca9ed
s
Oct 8, 2024
0cba12e
with permissions
Oct 8, 2024
384fb60
with permissions
Oct 8, 2024
140e284
with permissions
Oct 8, 2024
8828fc5
move to frontend
Oct 9, 2024
4d3ce26
move to frontend
Oct 9, 2024
f19caca
use dist dir
Oct 9, 2024
e26081f
wip
Oct 9, 2024
ea2beaf
load vars
Oct 9, 2024
9bd6f38
build front and back together
Oct 10, 2024
0d10bdf
clean-up
Oct 10, 2024
d0df97b
Merge branch 'main' into reusable-actions-combine-frontend-api
Oct 10, 2024
d2cde09
clean-up
Oct 10, 2024
8163469
test clean-up
Oct 10, 2024
6cc1cdb
worked
Oct 10, 2024
64aea5a
clean more
Oct 10, 2024
5615032
Merge branch 'main' into reusable-gha-tf-deploy
Oct 10, 2024
1fdc4db
wip
Oct 11, 2024
19951a8
Terraform variable clean-up and creation
Oct 11, 2024
9b4f06f
wip
Oct 11, 2024
177b380
-
Oct 11, 2024
57f567d
add different env configs for state
Oct 11, 2024
cb0f281
Merge branch 'add-var-to-tf' into reusable-gha-tf-deploy
Oct 11, 2024
45a1c15
try az app service action without publish secret
Oct 15, 2024
5d45bea
permissions
Oct 15, 2024
06861a2
change app name
Oct 15, 2024
fa7a96a
try without login
Oct 15, 2024
02774d5
try building dev2 from action
Oct 15, 2024
f57a2df
try global env
Oct 15, 2024
bf48dc7
inherit
Oct 15, 2024
40ae68e
use login at a higher level
Oct 15, 2024
4374feb
wip
Oct 15, 2024
f4e39be
moved everything to base
Oct 15, 2024
324427b
use environment...
Oct 15, 2024
c09540f
wip
Oct 15, 2024
1ac85b8
oidc true
Oct 15, 2024
5ef2bc5
oidc true
Oct 15, 2024
6d88c34
wip
Oct 15, 2024
4be6671
rm unused vars
Oct 15, 2024
953c5ca
rm unused vars
Oct 15, 2024
6970e7b
make cidr different for each env
Oct 15, 2024
31a2e52
make cidr different for each env
Oct 15, 2024
60df27e
make cidr different for each env
Oct 15, 2024
02c9993
remove path thing
Oct 15, 2024
5e3f960
add back api to path
Oct 15, 2024
f584c90
revert network vars
Oct 16, 2024
bc05ad5
wipe
Oct 16, 2024
cfde56a
wip
Oct 16, 2024
d4494db
clean-up for merge
Oct 16, 2024
ecea674
Merge branch 'main' into reusable-gha-tf-deploy
Oct 16, 2024
61d0ffc
tab state config files to the left
Oct 16, 2024
fcdfeeb
ft fmt
Oct 16, 2024
f6dfb74
add back var in old workflow
Oct 16, 2024
ab6e0ea
gitignore tf lock
Oct 16, 2024
14a75d3
gitignore tf lock
Oct 16, 2024
c2f603f
gitignore tf lock
Oct 16, 2024
0c9042e
gitignore tf lock
Oct 16, 2024
9a6cbcc
gitignore tf lock
Oct 16, 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
5 changes: 4 additions & 1 deletion .github/actions/build-frontend/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ inputs:
frontend-build-path:
description: The temporary path where build files are storaged
required: true
api-endpoint:
description: The endpoint to connect the frontend to an api
required: true

runs:
using: composite
Expand All @@ -37,7 +40,7 @@ runs:
env:
DEPLOY_ENV: ${{ inputs.deploy-env }}
run: |
VITE_API_URL=${{ env.OCR_API_URL }} npm run build
VITE_API_URL=${{ inputs.api-endpoint }} npm run build
- name: Test frontend
shell: bash
working-directory: ${{ inputs.frontend-path }}
Expand Down
6 changes: 3 additions & 3 deletions .github/actions/build-publish-api/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ inputs:
docker-username:
description: Docker registry username
required: true
version:
description: API version
docker-tag:
description: Docker tag, typically an API version
required: true
dockerfile-path:
description: Dockerfile path
Expand Down Expand Up @@ -44,4 +44,4 @@ runs:
context: ${{ inputs.docker-context-path }}
file: ${{ inputs.dockerfile-path }}
push: true
tags: ${{ inputs.docker-registry }}/${{ env.REPO }}-${{ inputs.api-name }}:${{ inputs.version }}
tags: ${{ inputs.docker-registry }}/${{ env.REPO }}-${{ inputs.api-name }}:${{ inputs.docker-tag }}
56 changes: 56 additions & 0 deletions .github/actions/tf-setup/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Setup Environment with Terraform
description: This action sets up the given environment using Terraform.
inputs:
deploy-env:
description: The environment to deploy to.
required: true
azure-resource-group:
description: The Azure Resource Group for this environment.
required: true
azure-client-id:
description: The Azure client_id for this environment.
required: true
azure-tenant-id:
description: The Azure tenant_id for this environment.
required: true
azure-subscription-id:
description: The Azure subscription_id for this environment.
required: true
app-name:
description: The name of the application being deployed in Terraform.
required: true

runs:
using: composite
steps:
- name: Load input variables
working-directory: ./ops/terraform
shell: bash
env:
RESOURCE_GROUP_NAME: ${{ inputs.azure-resource-group }}
NAME: ${{ inputs.app-name }}
run: |
echo resource_group_name=\""$RESOURCE_GROUP_NAME"\" >> terraform.tfvars
echo name=\""$NAME"\" >> terraform.tfvars
az config set defaults.group=$RESOURCE_GROUP_NAME
- name: Set environment
shell: bash
id: set-environment
env:
DEPLOY_ENV: ${{ inputs.deploy-env }}
run: |-
echo "tf-env=$(
echo ${DEPLOY_ENV}
)" >> $GITHUB_OUTPUT
- name: Terraform deploy
working-directory: ./ops/terraform
env:
ARM_CLIENT_ID: ${{ inputs.azure-client-id }}
ARM_TENANT_ID: ${{ inputs.azure-tenant-id }}
ARM_SUBSCRIPTION_ID: ${{ inputs.azure-subscription-id }}
shell: bash
run: |
terraform init -backend-config=config/${{ inputs.deploy-env }}.config
terraform workspace select -or-create ${{ inputs.deploy-env }}
terraform plan -lock-timeout=30m
terraform apply -auto-approve -lock-timeout=30m
16 changes: 13 additions & 3 deletions .github/workflows/build-deploy-ocr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:

env:
REGISTRY: ghcr.io
VERSION: ${{ inputs.tag }}
VERSION: derek-dev-combine


jobs:
Expand Down Expand Up @@ -47,17 +47,27 @@ jobs:

deploy:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
attestations: write
id-token: write
needs: build-and-push-image
environment: dev
steps:
- uses: azure/login@v2
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}

- name: Lowercase the repo name
run: echo "REPO=${GITHUB_REPOSITORY,,}" >>${GITHUB_ENV}

- name: Deploy to Azure Web App
id: deploy-to-webapp
uses: azure/webapps-deploy@v3
with:
app-name: reportvision-ocr-api-dev
publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
app-name: reportvision-ocr-dev
images: '${{ env.REGISTRY }}/${{ env.REPO}}-ocr-api:${{ env.VERSION }}'

42 changes: 30 additions & 12 deletions .github/workflows/deploy-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,18 @@ on:
required: true

permissions:
id-token: write
contents: read
packages: write
attestations: write
id-token: write

env:
NODE_VERSION: 20
OCR_API_URL: 'https://reportvision-ocr-api-dev.azurewebsites.net/'
OCR_API_URL: 'https://reportvision-ocr-dev.azurewebsites.net/'

jobs:
build_publish_ocr:
build-publish-ocr:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
attestations: write
id-token: write
steps:
- uses: actions/checkout@v4
- name: Build and Push backend
Expand All @@ -42,20 +39,41 @@ jobs:
docker-registry: ghcr.io
docker-pw: ${{ secrets.GITHUB_TOKEN }}
docker-username: ${{ github.actor }}
version: ${{ inputs.ocr-version }}
docker-tag: ${{ inputs.ocr-version }}
dockerfile-path: ./OCR/Dockerfile
docker-context-path: ./OCR/
api-name: ocr-api

build_frontend:
build-frontend:
runs-on: ubuntu-latest
environment: dev
environment: ${{ inputs.deploy-env }}
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/build-frontend
name: Build front-end application
with:
api-endpoint: ${{ env.OCR_API_URL }}
frontend-tarball: ./frontend.tgz
deploy-env: ${{ inputs.deploy-env }}
frontend-path: ./frontend
frontend-build-path: ./frontend/dist/
frontend-build-path: ./frontend/dist/

environment-setup:
runs-on: ubuntu-latest
environment: ${{ inputs.deploy-env }}
steps:
- uses: actions/checkout@v4
- uses: azure/login@v2
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- uses: ./.github/actions/tf-setup
name: Setup this environment with Terraform
with:
deploy-env: ${{ inputs.deploy-env }}
azure-resource-group: reportvision-rg-${{ inputs.deploy-env }}
azure-client-id: ${{ secrets.AZURE_CLIENT_ID }}
azure-tenant-id: ${{ secrets.AZURE_TENANT_ID }}
azure-subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
app-name: reportvision
8 changes: 4 additions & 4 deletions ops/terraform/config/dev.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
storage_account_name = "tfstaterv2024"
container_name = "rv-tfstate"
key = "dev.terraform.tfstate"
use_oidc = true
storage_account_name = "tfstaterv2024"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming these are for each of the different dev environments?

Copy link
Collaborator Author

@derekadombek derekadombek Oct 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thats correct! so with TF you can store the configuration state files either locally on our computers or a in a central location. these config files basically create and update these files in a global AZ Resource Group in the same account subscription. the benefit of separating them out into individual files like this is so we can use the -backend-config=config/${{ inputs.deploy-env }}.config flag in the terraform cli. We are no using that here in a reusable GHA:

terraform init -backend-config=config/${{ inputs.deploy-env }}.config

In the future however, I would like to research and find out if this is for sure the best and most appropriate way of doing this but for now, this allows for a bunch of flexibility.

container_name = "rv-tfstate"
key = "dev.terraform.tfstate"
use_oidc = true
8 changes: 4 additions & 4 deletions ops/terraform/config/dev2.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
storage_account_name = "tfstaterv2024"
container_name = "rv-tfstate"
key = "dev2.terraform.tfstate"
use_oidc = true
storage_account_name = "tfstaterv2024"
container_name = "rv-tfstate"
key = "dev2.terraform.tfstate"
use_oidc = true
8 changes: 4 additions & 4 deletions ops/terraform/config/dev3.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
storage_account_name = "tfstaterv2024"
container_name = "rv-tfstate"
key = "dev3.terraform.tfstate"
use_oidc = true
storage_account_name = "tfstaterv2024"
container_name = "rv-tfstate"
key = "dev3.terraform.tfstate"
use_oidc = true
8 changes: 4 additions & 4 deletions ops/terraform/config/dev4.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
storage_account_name = "tfstaterv2024"
container_name = "rv-tfstate"
key = "dev4.terraform.tfstate"
use_oidc = true
storage_account_name = "tfstaterv2024"
container_name = "rv-tfstate"
key = "dev4.terraform.tfstate"
use_oidc = true
8 changes: 4 additions & 4 deletions ops/terraform/config/dev5.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
storage_account_name = "tfstaterv2024"
container_name = "rv-tfstate"
key = "dev5.terraform.tfstate"
use_oidc = true
storage_account_name = "tfstaterv2024"
container_name = "rv-tfstate"
key = "dev5.terraform.tfstate"
use_oidc = true
8 changes: 4 additions & 4 deletions ops/terraform/config/dev6.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
storage_account_name = "tfstaterv2024"
container_name = "rv-tfstate"
key = "dev6.terraform.tfstate"
use_oidc = true
storage_account_name = "tfstaterv2024"
container_name = "rv-tfstate"
key = "dev6.terraform.tfstate"
use_oidc = true
6 changes: 3 additions & 3 deletions ops/terraform/locals.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
locals {
environment = "${terraform.workspace}"
environment = terraform.workspace
init = {
environment = local.environment
location = "eastus2"
environment = local.environment
location = "eastus2"
}
dev = {
dev = {
Expand Down
24 changes: 12 additions & 12 deletions ops/terraform/main.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
locals {
workspaces = "${merge(local.dev, local.dev2, local.dev3, local.dev4, local.dev5, local.dev6)}"
workspace = "${local.workspaces[terraform.workspace]}"
workspaces = merge(local.dev, local.dev2, local.dev3, local.dev4, local.dev5, local.dev6)
workspace = local.workspaces[terraform.workspace]

management_tags = {
environment = local.environment
Expand All @@ -20,7 +20,7 @@ module "networking" {
websubnetcidr = local.workspace["websubnetcidr"]
lbsubnetcidr = local.workspace["lbsubnetcidr"]
# dbsubnetcidr = local.network.config.dbsubnetcidr
env = local.environment
env = local.environment
}

##########
Expand All @@ -34,8 +34,8 @@ module "securitygroup" {
resource_group = data.azurerm_resource_group.rg.name
web_subnet_id = module.networking.websubnet_id
# db_subnet_id = module.networking.dbsubnet_id
lb_subnet_id = module.networking.lbsubnet_id
env = local.environment
lb_subnet_id = module.networking.lbsubnet_id
env = local.environment
}

module "app_gateway" {
Expand Down Expand Up @@ -73,13 +73,13 @@ module "storage" {
##########

module "ocr_api" {
source = "./modules/app_service"
name = var.name
location = local.init.location
resource_group = data.azurerm_resource_group.rg.name
app_subnet_id = module.networking.lbsubnet_id
env = local.environment
vnet = module.networking.network_name
source = "./modules/app_service"
name = var.name
location = local.init.location
resource_group = data.azurerm_resource_group.rg.name
app_subnet_id = module.networking.lbsubnet_id
env = local.environment
vnet = module.networking.network_name
}

# module "compute" {
Expand Down
2 changes: 1 addition & 1 deletion ops/terraform/providers.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
terraform {
backend "azurerm" {
resource_group_name = "reportvision-rg-global"
resource_group_name = "reportvision-rg-global"
}
required_providers {
azurerm = {
Expand Down
Loading