Skip to content

rm env:

rm env: #10

Workflow file for this run

name: Setup
on:
push:
branches:
- 'blue-green-deploy'
workflow_dispatch:
permissions:
id-token: write
contents: read
jobs:
init:
uses: ./.github/workflows/build.yml
with:
aws_account_id: ${{ vars.AWS_ACCOUNT_ID }}
aws_region: ${{ vars.AWS_REGION }}
aws_role: ${{ vars.AWS_ROLE }}
repository_name: ${{ github.repository }}
service:
needs:
- init
runs-on: ubuntu-latest
env:
TF_VAR_task_definition_arn: ${{ needs.init.outputs.task_definition_arn }}
outputs:
cluster_name: ${{ steps.set-envs.outputs.CLUSTER_NAME }}
service_name: ${{ steps.set-envs.outputs.SERVICE_NAME }}
lb_listener_arn: ${{ steps.set-envs.outputs.LB_LISTENER_ARN }}
steps:
- uses: actions/checkout@v4
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: ${{ vars.aws_region }}
role-to-assume: arn:aws:iam::${{ vars.aws_account_id }}:role/${{ vars.aws_role }}
role-session-name: GitHubActions
- name: Init
shell: bash
run: |
cd tf/service
terraform init
- name: Deploy
shell: bash
id: deploy
run: |
cd tf/service
terraform apply -auto-approve -var-file=${{ github.workspace }}/tf/variables.tfvars
- name: Set env vars
id: set-envs
shell: bash
run: |
cd tf/service
CLUSTER_NAME=$(terraform output -raw cluster_name)
echo "CLUSTER_NAME=$CLUSTER_NAME" >> $GITHUB_OUTPUT
SERVICE_NAME=$(terraform output -raw service_name)
echo "SERVICE_NAME=$SERVICE_NAME" >> $GITHUB_OUTPUT
LB_LISTENER_ARN=$(terraform output -raw lb_listener_arn)
echo "LB_LISTENER_ARN=$LB_LISTENER_ARN" >> $GITHUB_OUTPUT
network:
needs: service
runs-on: ubuntu-latest
env:
TF_VAR_load_balancer_listener_arn: ${{ needs.service.outputs.lb_listener_arn }}
outputs:
api_invoke_url: ${{ steps.set-envs.outputs.API_INVOKE_URL }}
steps:
- uses: actions/checkout@v4
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: ${{ vars.aws_region }}
role-to-assume: arn:aws:iam::${{ vars.aws_account_id }}:role/${{ vars.aws_role }}
role-session-name: GitHubActions
- name: Init
shell: bash
run: |
cd tf/network
terraform init
- name: Deploy
shell: bash
id: deploy
run: |
cd tf/network
terraform apply -auto-approve -var-file=${{ github.workspace }}/tf/variables.tfvars
- name: Set env vars
id: set-envs
shell: bash
run: |
cd tf/network
API_INVOKE_URL=$(terraform output -raw api_invoke_url)
echo "API_INVOKE_URL=$API_INVOKE_URL" >> $GITHUB_OUTPUT
check_image:
needs:
- init
- service
runs-on: ubuntu-latest
steps:
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: ${{ vars.aws_region }}
role-to-assume: arn:aws:iam::${{ vars.aws_account_id }}:role/${{ vars.aws_role }}
role-session-name: GitHubActions
- name: Get current task definition
id: task-def
shell: bash
run: |
TASK_DEF_ARN=$(aws ecs describe-services \
--cluster ${{ needs.service.outputs.cluster_name }} \
--services ${{ needs.service.outputs.service_name }} \
--region ${{ vars.aws_region }} \
--query 'services[0].taskDefinition' \
--output text)
echo "TASK_DEF_ARN=$TASK_DEF_ARN" >> $GITHUB_ENV
- name: Get image from task definition
id: check-image
shell: bash
run: |
CURRENT_IMAGE=$(aws ecs describe-task-definition \
--task-definition ${{ env.TASK_DEF_ARN }} \
--region ${{ vars.aws_region }} \
--query 'taskDefinition.containerDefinitions[*].image' \
--output text)
echo "CURRENT_IMAGE=$CURRENT_IMAGE"
echo "CURRENT_IMAGE=$CURRENT_IMAGE" >> $GITHUB_ENV
- name: Compare with Terraform image
id: compare
run: |
if [ "${{ env.CURRENT_IMAGE }}" != "${{ needs.init.outputs.image_uri }}" ]; then
echo "ERROR: Image mismatch! Current image: ${{ env.CURRENT_IMAGE }}, Expected: ${{ needs.task.outputs.image_uri }}"
exit 1
else
echo "Image matched: ${{ env.CURRENT_IMAGE }}"
fi
check_response:
needs:
- network
- service
runs-on: ubuntu-latest
steps:
- name: Make API request
id: curl_request
run: |
RESPONSE=$(curl -s ${{ needs.network.outputs.api_invoke_url}}/host)
echo "Response: $RESPONSE"
echo "$RESPONSE" | jq
- name: Assert API response contains expected fields
run: |
RESPONSE=$(curl -s ${{ needs.network.outputs.api_invoke_url}}/host)
echo "$RESPONSE" | jq
# Extract values from response
MESSAGE=$(echo "$RESPONSE" | jq -r '.message')
IMAGE_URI=$(echo "$RESPONSE" | jq -r '.imageUri')
HOSTNAME=$(echo "$RESPONSE" | jq -r '.hostname')
# Check if expected fields exist
if [[ "$MESSAGE" == "null" || "$IMAGE_URI" == "null" || "$HOSTNAME" == "null" ]]; then
echo "Error: Response does not contain expected fields."
exit 1
else
echo "API response contains all expected fields."
fi