diff --git a/.github/workflows/template-only-ci-infra.yml b/.github/workflows/template-only-ci-infra.yml index d47e2dea..15a34829 100644 --- a/.github/workflows/template-only-ci-infra.yml +++ b/.github/workflows/template-only-ci-infra.yml @@ -58,7 +58,17 @@ jobs: - name: Install infra template run: | + # Note: projectName can't be too long since S3 bucket names have a 63 + # character max length, $RANDOM has a max value of 32767 + unique_id=$(printf '%.5d' $((RANDOM))) + project_name=plt-tst-act-${unique_id} + echo "PROJECT_NAME=${project_name}" >> "$GITHUB_ENV" + nava-platform infra install --template-uri ./template-infra \ + --data project_name="${project_name}" \ + --data owner=navapbc \ + --data code_repository_url=${{ github.repositoryUrl }} \ + --data default_region=us-east-1 \ --data app_name=app \ --data local_port=3000 \ --data is_dev_env_setup=true \ @@ -75,7 +85,7 @@ jobs: # Use access key credentials for the template infra test workflow # instead of using GitHub OIDC because only one GitHub OIDC provider - # can be added to the Platform accoutn, and we want to create a + # can be added to the Platform account, and we want to create a # GitHub OIDC provider as part of the template test. # (see https://docs.aws.amazon.com/cli/latest/reference/iam/create-open-id-connect-provider.html) # @@ -90,3 +100,4 @@ jobs: # GitHub token needed for GitHub CLI which is used during tests to check GitHub Actions auth GH_TOKEN: ${{ secrets.PLATFORM_TESTER_GITHUB_TOKEN }} IMAGE_TAG: ${{ github.sha }} + PROJECT_NAME: ${{ env.PROJECT_NAME }} diff --git a/copier.yml b/copier.yml index d76502d8..54bf857b 100644 --- a/copier.yml +++ b/copier.yml @@ -1,3 +1,59 @@ +# +# Base vars +# +project_name: + type: str + help: The slugified name of the project (lower case, dashes, and underscores) + +owner: + type: str + help: Project owner slug (used for tagging infra resources) + +code_repository_url: + type: str + help: URL of project source code repository + +default_region: + type: str + help: Default AWS region for project + default: us-east-2 + choices: + - us-east-1 + - us-east-2 + - us-west-1 + - us-west-2 + - af-south-1 + - ap-east-1 + - ap-south-2 + - ap-southeast-3 + - ap-southeast-5 + - ap-southeast-4 + - ap-south-1 + - ap-northeast-3 + - ap-northeast-2 + - ap-southeast-1 + - ap-southeast-2 + - ap-northeast-1 + - ca-central-1 + - ca-west-1 + - cn-north-1 + - cn-northwest-1 + - eu-central-1 + - eu-west-1 + - eu-west-2 + - eu-south-1 + - eu-west-3 + - eu-south-2 + - eu-north-1 + - eu-central-2 + - il-central-1 + - me-south-1 + - me-central-1 + - sa-east-1 + +# +# App vars +# app_name: type: str help: The name of the app diff --git a/infra/project-config/main.tf b/infra/project-config/main.tf.jinja similarity index 78% rename from infra/project-config/main.tf rename to infra/project-config/main.tf.jinja index e3ecf2df..419c43ec 100644 --- a/infra/project-config/main.tf +++ b/infra/project-config/main.tf.jinja @@ -1,18 +1,18 @@ locals { # Machine readable project name (lower case letters, dashes, and underscores) # This will be used in names of AWS resources - project_name = "" + project_name = "{{ project_name }}" # Project owner (e.g. navapbc). Used for tagging infra resources. - owner = "" + owner = "{{ owner }}" # URL of project source code repository - code_repository_url = "" + code_repository_url = "{{ code_repository_url }}" # Default AWS region for project (e.g. us-east-1, us-east-2, us-west-1). # This is dependent on where your project is located (if regional) # otherwise us-east-1 is a good default - default_region = "" + default_region = "{{ default_region }}" github_actions_role_name = "${local.project_name}-github-actions" diff --git a/template-only-bin/set-up-project b/template-only-bin/set-up-project index 2432dd9a..74b2ba5e 100755 --- a/template-only-bin/set-up-project +++ b/template-only-bin/set-up-project @@ -1,45 +1,6 @@ #!/bin/bash set -euo pipefail -project_name="$1" -owner="$2" -default_region="$3" -repo_url=$(git remote get-url origin) - -echo "Account configuration" -echo "=====================" -echo "PROJECT_NAME=${project_name}" -echo "REPO_URL=${repo_url}" -echo - -cd infra/project-config - -echo "-------------------------------------" -echo "Replace placeholder values in main.tf" -echo "-------------------------------------" - -# First replace the placeholder value for in main.tf -# The project name is used to define unique names for the infrastructure -# resources that are created in subsequent infra setup steps. -sed -i.bak "s//${project_name}/" main.tf - -# Then replace the placeholder value for in main.tf -# The repository is needed to set up the GitHub OpenID Connect provider -# in AWS which allows GitHub Actions to authenticate with our AWS account -# when called from our repository only. -# Use '|' as the regex delimiter for sed instead of '/' since -# REPO_URL will have a '/' in it -sed -i.bak "s||${repo_url}|" main.tf - -# Replace remaining placeholder values -sed -i.bak "s//${owner}/" main.tf -sed -i.bak "s//${default_region}/" main.tf - -# Remove the backup file created by sed -rm main.tf.bak - -cd - - # Set has_database to false for template only CI since database setup takes too long sed -i.bak "s/has_database = true/has_database = false/" infra/app/app-config/main.tf rm infra/app/app-config/main.tf.bak diff --git a/template-only-test/template_infra_test.go b/template-only-test/template_infra_test.go index f0bc5fdd..b1722241 100644 --- a/template-only-test/template_infra_test.go +++ b/template-only-test/template_infra_test.go @@ -3,22 +3,17 @@ package test import ( "fmt" "os" - "strings" "testing" "time" "github.com/gruntwork-io/terratest/modules/aws" http_helper "github.com/gruntwork-io/terratest/modules/http-helper" - "github.com/gruntwork-io/terratest/modules/random" "github.com/gruntwork-io/terratest/modules/shell" "github.com/gruntwork-io/terratest/modules/terraform" "github.com/stretchr/testify/assert" ) -// Note: projectName can't be too long since S3 bucket names have a 63 character max length -var uniqueId = strings.ToLower(random.UniqueId()) -var projectName = fmt.Sprintf("plt-tst-act-%s", uniqueId) - +var projectName = os.Getenv("PROJECT_NAME") var imageTag = os.Getenv("IMAGE_TAG") func TestEndToEnd(t *testing.T) { @@ -60,7 +55,7 @@ func SetUpProject(t *testing.T, projectName string) { fmt.Println("::group::Configuring project") shell.RunCommand(t, shell.Command{ Command: "make", - Args: []string{"-f", "template-only.mak", "set-up-project", fmt.Sprintf("PROJECT_NAME=%s", projectName)}, + Args: []string{"-f", "template-only.mak", "set-up-project"}, WorkingDir: "../", }) fmt.Println("::endgroup::")