diff --git a/.github/workflows/build-infra.yml b/.github/workflows/release-infra.yml similarity index 86% rename from .github/workflows/build-infra.yml rename to .github/workflows/release-infra.yml index 1e901c91..d76351dc 100644 --- a/.github/workflows/build-infra.yml +++ b/.github/workflows/release-infra.yml @@ -2,6 +2,10 @@ name: build-infrastructure on: push: + tags: + - release-dev-** + - release-preprod-** + branches: - main @@ -33,6 +37,18 @@ jobs: echo "environment=${ENVIRONMENT}" >> $GITHUB_OUTPUT echo "::add-mask::${AWS_REGION}" + - id: env-var + run: | + # prod env from main branch + if [ ${{ github.ref == 'refs/heads/main' }} ]; then + export ENVIRONMENT=prod + # Otherwise get the environment from the tag + else + export ENVIRONMENT=$(echo ${{ github.ref_name }} | awk -F- '{ print $2 }') + fi + echo "environment=${ENVIRONMENT}" >> $GITHUB_OUTPUT + + start-runner: uses: i-dot-ai/i-dot-ai-core-github-actions/.github/workflows/start-runner.yml@main needs: set-vars diff --git a/Makefile b/Makefile index d185fb1c..a878f46f 100644 --- a/Makefile +++ b/Makefile @@ -112,4 +112,10 @@ tf_apply: ## Apply terraform .PHONY: tf_destroy tf_destroy: ## Destroy terraform make tf_set_workspace && \ - terraform -chdir=./infrastructure destroy -var-file=$(CONFIG_DIR)/${env}-input-params.tfvars ${tf_build_args} \ No newline at end of file + terraform -chdir=./infrastructure destroy -var-file=$(CONFIG_DIR)/${env}-input-params.tfvars ${tf_build_args} + + +# Release commands to deploy your app to AWS +.PHONY: release +release: ## Deploy app + chmod +x ./infrastructure/scripts/release.sh && ./infrastructure/scripts/release.sh $(env) \ No newline at end of file diff --git a/infrastructure/ecs.tf b/infrastructure/ecs.tf index 740d9e7f..ccd5619a 100644 --- a/infrastructure/ecs.tf +++ b/infrastructure/ecs.tf @@ -1,9 +1,9 @@ module "ecs" { source = "../../i-ai-core-infrastructure//modules/ecs" project_name = var.project_name - image_tag = "5586478ada171e6b4fc81c3c4142f2d125079bf3" + image_tag = var.image_tag prefix = "i-dot-ai" - ecr_repository_uri = module.ecr.ecr_repository_url + ecr_repository_uri = module.ecr_front_end.ecr_repository_url ecs_cluster_id = data.terraform_remote_state.platform.outputs.ecs_cluster_id health_check = { healthy_threshold = 3 diff --git a/infrastructure/scripts/release.sh b/infrastructure/scripts/release.sh new file mode 100755 index 00000000..ecfd6c58 --- /dev/null +++ b/infrastructure/scripts/release.sh @@ -0,0 +1,33 @@ +#!/bin/bash +# Usage: +## Just pass the name of the env we want to tag and deploy. +## This will create a tag locally with a format of $$ENV-$BRANCH-$CURRENT_USER-$TIMESTAMP +## Then push it to the remote git. +ENV=$1 +BRANCH=$(git rev-parse --abbrev-ref HEAD) +CURRENT_USER=$(whoami) +TIMESTAMP=$(date +%d-%m-%y--%H%M%S) +TAG_NAME="release-$ENV-$BRANCH-$CURRENT_USER-$TIMESTAMP" + +echo "Current branch name is" "$BRANCH" +echo "Current environment name is" "$ENV" +echo "Timestamp assigned will be $TIMESTAMP" +echo "New tag name will be " "$TAG_NAME" + +if [ $ENV == 'prod' ]; then + echo '' + if [ $BRANCH != 'main' ]; then + echo -e "\033[0;31mYou can only deploy to prod through a PR into main\033[0m" + exit 0 + fi +fi + +## +echo "Removing Local tags" +git tag -d $(git tag -l) + +# Command to run +echo "Applying local tag" && \ +git tag "$TAG_NAME" && \ +echo "Pushing tag" && \ +git push origin $TAG_NAME \ No newline at end of file