Skip to content

Commit

Permalink
Pull infra/project-config/main.tf into template
Browse files Browse the repository at this point in the history
  • Loading branch information
doshitan committed Nov 6, 2024
1 parent 46a4114 commit 25904e1
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 51 deletions.
13 changes: 12 additions & 1 deletion .github/workflows/template-only-ci-infra.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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)
#
Expand All @@ -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 }}
56 changes: 56 additions & 0 deletions copier.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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_name }}"

# Project owner (e.g. navapbc). Used for tagging infra resources.
owner = "<OWNER>"
owner = "{{ owner }}"

# URL of project source code repository
code_repository_url = "<REPO_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 = "{{ default_region }}"

github_actions_role_name = "${local.project_name}-github-actions"

Expand Down
39 changes: 0 additions & 39 deletions template-only-bin/set-up-project
Original file line number Diff line number Diff line change
@@ -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 <PROJECT_NAME> 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>/${project_name}/" main.tf

# Then replace the placeholder value for <REPO_URL> 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>|${repo_url}|" main.tf

# Replace remaining placeholder values
sed -i.bak "s/<OWNER>/${owner}/" main.tf
sed -i.bak "s/<DEFAULT_REGION>/${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
9 changes: 2 additions & 7 deletions template-only-test/template_infra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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::")
Expand Down

0 comments on commit 25904e1

Please sign in to comment.