diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 1cc70d1..6ea0281 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -26,12 +26,19 @@ jobs: CF_DEPLOY_DOMAIN: ${{ vars.CF_DEPLOY_DOMAIN }} # this is needed to get the lowercase version of the repository_owner name - # TODO: switch to some lowercase function in the future when Actions supports it - - name: Set lowercase repository_owner name + # and being able to override the suffix when mutliple instances are hosted + # by the same GitHub account. + - name: Set name suffix run: | - echo $GH_OWNER | awk '{ print "OWNER_LOWER=" tolower($0) }' >> ${GITHUB_ENV} + if [ -z "$OVERRIDE_NAME_SUFFIX" ] + then + echo $GH_OWNER | awk '{ print "NAME_SUFFIX=" tolower($0) }' >> ${GITHUB_ENV} + else + echo $OVERRIDE_NAME_SUFFIX | awk '{ print "NAME_SUFFIX=" tolower($0) }' >> ${GITHUB_ENV} + fi env: GH_OWNER: ${{ github.repository_owner }} + OVERRIDE_NAME_SUFFIX: ${{ vars.OVERRIDE_NAME_SUFFIX }} - uses: actions/checkout@v2 - uses: hashicorp/setup-terraform@v2 @@ -81,7 +88,7 @@ jobs: - name: Create D1 database uses: cloudflare/wrangler-action@2.0.0 with: - command: d1 create wildebeest-${{ env.OWNER_LOWER }} + command: d1 create wildebeest-${{ env.NAME_SUFFIX }} apiToken: ${{ secrets.CF_API_TOKEN }} continue-on-error: true env: @@ -90,7 +97,7 @@ jobs: - name: retrieve D1 database uses: cloudflare/wrangler-action@2.0.0 with: - command: d1 list | grep wildebeest-${{ env.OWNER_LOWER }} | awk '{print "d1_id="$2}' >> $GITHUB_ENV + command: d1 list | grep wildebeest-${{ env.NAME_SUFFIX }} | awk '{print "d1_id="$2}' >> $GITHUB_ENV apiToken: ${{ secrets.CF_API_TOKEN }} env: CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID }} @@ -98,11 +105,11 @@ jobs: - name: migrate D1 database uses: cloudflare/wrangler-action@2.0.0 with: - command: d1 migrations apply wildebeest-${{ env.OWNER_LOWER }} + command: d1 migrations apply wildebeest-${{ env.NAME_SUFFIX }} apiToken: ${{ secrets.CF_API_TOKEN }} preCommands: | echo "*** pre commands ***" - echo -e "[[d1_databases]]\nbinding=\"DATABASE\"\ndatabase_name=\"wildebeest-${{ env.OWNER_LOWER }}\"\ndatabase_id=\"${{ env.d1_id }}\"" >> wrangler.toml + echo -e "[[d1_databases]]\nbinding=\"DATABASE\"\ndatabase_name=\"wildebeest-${{ env.NAME_SUFFIX }}\"\ndatabase_id=\"${{ env.d1_id }}\"" >> wrangler.toml echo "******" env: CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID }} @@ -116,7 +123,7 @@ jobs: - name: retrieve Terraform state KV namespace uses: cloudflare/wrangler-action@2.0.0 with: - command: kv:namespace list | jq -r '.[] | select( .title == "wildebeest-terraform-${{ env.OWNER_LOWER }}-state" ) | .id' | awk '{print "tfstate_kv="$1}' >> $GITHUB_ENV + command: kv:namespace list | jq -r '.[] | select( .title == "wildebeest-terraform-${{ env.NAME_SUFFIX }}-state" ) | .id' | awk '{print "tfstate_kv="$1}' >> $GITHUB_ENV apiToken: ${{ secrets.CF_API_TOKEN }} preCommands: | echo "*** pre commands ***" @@ -188,7 +195,7 @@ jobs: TF_VAR_cloudflare_api_token: ${{ secrets.CF_API_TOKEN }} TF_VAR_cloudflare_zone_id: ${{ vars.CF_ZONE_ID }} TF_VAR_cloudflare_deploy_domain: ${{ vars.CF_DEPLOY_DOMAIN }} - TF_VAR_gh_username: ${{ env.OWNER_LOWER }} + TF_VAR_name_suffix: ${{ env.NAME_SUFFIX }} TF_VAR_d1_id: ${{ env.d1_id }} TF_VAR_do_cache_id: ${{ env.do_cache_id }} TF_VAR_access_auth_domain: ${{ env.auth_domain }} @@ -202,7 +209,7 @@ jobs: - name: retrieve Terraform state KV namespace uses: cloudflare/wrangler-action@2.0.0 with: - command: kv:namespace list | jq -r '.[] | select( .title == "wildebeest-terraform-${{ env.OWNER_LOWER }}-state" ) | .id' | awk '{print "tfstate_kv="$1}' >> $GITHUB_ENV + command: kv:namespace list | jq -r '.[] | select( .title == "wildebeest-terraform-${{ env.NAME_SUFFIX }}-state" ) | .id' | awk '{print "tfstate_kv="$1}' >> $GITHUB_ENV apiToken: ${{ secrets.CF_API_TOKEN }} preCommands: | echo "*** pre commands ***" @@ -243,7 +250,7 @@ jobs: apiToken: ${{ secrets.CF_API_TOKEN }} preCommands: | echo "*** pre commands ***" - echo -e "[[d1_databases]]\nbinding=\"DATABASE\"\ndatabase_name=\"wildebeest-${{ env.OWNER_LOWER }}\"\ndatabase_id=\"${{ env.d1_id }}\"\n" >> consumer/wrangler.toml + echo -e "[[d1_databases]]\nbinding=\"DATABASE\"\ndatabase_name=\"wildebeest-${{ env.NAME_SUFFIX }}\"\ndatabase_id=\"${{ env.d1_id }}\"\n" >> consumer/wrangler.toml echo -e "[durable_objects]\n" >> consumer/wrangler.toml echo -e "bindings=[" >> consumer/wrangler.toml @@ -260,7 +267,7 @@ jobs: - name: add Queue producer to Pages run: | - curl https://api.cloudflare.com/client/v4/accounts/${{ secrets.CF_ACCOUNT_ID }}/pages/projects/wildebeest-${{ env.OWNER_LOWER }} \ + curl https://api.cloudflare.com/client/v4/accounts/${{ secrets.CF_ACCOUNT_ID }}/pages/projects/wildebeest-${{ env.NAME_SUFFIX }} \ -XPATCH \ -H 'Authorization: Bearer ${{ secrets.CF_API_TOKEN }}' \ -d '{ @@ -287,7 +294,7 @@ jobs: # remove folder that aren't needed in Pages before we upload rm -rf ./tf ./scripts ./.github ./.npm ./consumer ./*.md echo "******" - command: pages publish --project-name=wildebeest-${{ env.OWNER_LOWER }} . + command: pages publish --project-name=wildebeest-${{ env.NAME_SUFFIX }} . env: CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID }} COMMIT_HASH: ${{ github.sha }} diff --git a/tf/main.tf b/tf/main.tf index 4f85846..f5384da 100644 --- a/tf/main.tf +++ b/tf/main.tf @@ -18,7 +18,7 @@ variable "cloudflare_api_token" { sensitive = true } -variable "gh_username" { +variable "name_suffix" { type = string } @@ -85,12 +85,12 @@ provider "cloudflare" { // to remove the binding from the Pages project, so leaving for now. resource "cloudflare_workers_kv_namespace" "wildebeest_cache" { account_id = var.cloudflare_account_id - title = "wildebeest-${lower(var.gh_username)}-cache" + title = "wildebeest-${lower(var.name_suffix)}-cache" } resource "cloudflare_workers_kv_namespace" "terraform_state" { account_id = var.cloudflare_account_id - title = "wildebeest-terraform-${lower(var.gh_username)}-state" + title = "wildebeest-terraform-${lower(var.name_suffix)}-state" } resource "random_password" "user_key" { @@ -100,7 +100,7 @@ resource "random_password" "user_key" { resource "cloudflare_pages_project" "wildebeest_pages_project" { account_id = var.cloudflare_account_id - name = "wildebeest-${lower(var.gh_username)}" + name = "wildebeest-${lower(var.name_suffix)}" production_branch = "main" deployment_configs { @@ -153,7 +153,7 @@ resource "cloudflare_record" "record" { resource "cloudflare_pages_domain" "domain" { account_id = var.cloudflare_account_id - project_name = "wildebeest-${lower(var.gh_username)}" + project_name = "wildebeest-${lower(var.name_suffix)}" domain = trimspace(var.cloudflare_deploy_domain) depends_on = [ @@ -164,7 +164,7 @@ resource "cloudflare_pages_domain" "domain" { resource "cloudflare_access_application" "wildebeest_access" { account_id = var.cloudflare_account_id - name = "wildebeest-${lower(var.gh_username)}" + name = "wildebeest-${lower(var.name_suffix)}" domain = "${trimspace(var.cloudflare_deploy_domain)}/oauth/authorize" type = "self_hosted" session_duration = "730h"