#!/usr/bin/env bash # Build and push the panoptiblog job image, point the ECS task definition at # that tag, and apply the infrastructure. With --run, start one task now; # otherwise the EventBridge rule starts one every day. # # The first run needs the site infrastructure applied first: infra/main.tf's # release_sha has no default, and scripts/deploy-site.sh is what writes it. set -euo pipefail repo_root="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/../.." && pwd)" region="us-east-1" run_now="" tofu_args=() for arg in "$@"; do case "${arg}" in --run) run_now=1 ;; *) tofu_args+=("${arg}") ;; esac done if ! identity="$(aws sts get-caller-identity --output text --query Arn 2>&1)"; then echo "deploy-panoptiblog: AWS credential check failed:" >&2 echo " ${identity}" >&2 exit 1 fi echo "deploy-panoptiblog: deploying as ${identity}" # The ECR repository is created by the first apply, so read its URL after one # exists rather than spelling an account id here. if ! repository="$(tofu -chdir="${repo_root}/infra" output -raw panoptiblog_repository_url 2>/dev/null)"; then echo "deploy-panoptiblog: applying once to create the ECR repository" tofu -chdir="${repo_root}/infra" apply "${tofu_args[@]}" repository="$(tofu -chdir="${repo_root}/infra" output -raw panoptiblog_repository_url)" fi registry="${repository%%/*}" # The tag is the commit, so the task definition names the code it runs rather # than a moving "latest" nobody can reproduce. tag="$(git -C "${repo_root}" rev-parse --short=12 HEAD)" aws ecr get-login-password --region "${region}" \ | docker login --username AWS --password-stdin "${registry}" >/dev/null docker build --platform linux/amd64 -t "${repository}:${tag}" \ -f "${repo_root}/scripts/panoptiblog/Dockerfile" "${repo_root}/scripts/panoptiblog" docker push "${repository}:${tag}" node "${repo_root}/scripts/patch-tfvars.mjs" \ "${repo_root}/infra/terraform.tfvars.json" panoptiblog_image_tag "${tag}" tofu -chdir="${repo_root}/infra" apply "${tofu_args[@]}" if [[ -n "${run_now}" ]]; then cluster="$(tofu -chdir="${repo_root}/infra" output -raw panoptiblog_cluster)" task_definition="$(tofu -chdir="${repo_root}/infra" output -raw panoptiblog_task_definition)" subnet="$(tofu -chdir="${repo_root}/infra" output -raw panoptiblog_subnet)" security_group="$(tofu -chdir="${repo_root}/infra" output -raw panoptiblog_security_group)" aws ecs run-task \ --cluster "${cluster}" \ --task-definition "${task_definition}" \ --launch-type FARGATE \ --region "${region}" \ --network-configuration "awsvpcConfiguration={subnets=[${subnet}],securityGroups=[${security_group}],assignPublicIp=ENABLED}" \ --query 'tasks[0].taskArn' --output text fi