Init #61
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Init | |
on: | |
workflow_dispatch: | |
permissions: | |
id-token: write | |
contents: read | |
jobs: | |
ecs-check: | |
runs-on: ubuntu-latest | |
outputs: | |
service_exists: ${{ steps.set-output.outputs.service_exists }} | |
task_definition_arn: ${{ steps.set-output.outputs.task_definition_arn }} | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Check ECS Service | |
id: check-ecs-service | |
uses: ./.github/actions/check_svc | |
with: | |
aws_region: ${{ vars.aws_region }} | |
aws_role: ${{ vars.aws_account_id }}:role/${{ vars.aws_role }} | |
tfvars_file: "tf/variables.tfvars" | |
- name: Set output for service existence | |
id: set-output | |
shell: bash | |
run: | | |
echo "service_exists=${{ env.SERVICE_EXISTS }}" >> $GITHUB_OUTPUT | |
echo "task_definition_arn=${{ env.TASK_DEFINITION_ARN }}" >> $GITHUB_OUTPUT | |
ecr: | |
runs-on: ubuntu-latest | |
env: | |
TF_VAR_ecr_repository_name: ${{ github.event.repository.name }} | |
steps: | |
- uses: actions/checkout@v4 | |
- uses: hashicorp/setup-terraform@v3 | |
- 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/ecr | |
terraform init | |
- name: Deploy | |
shell: bash | |
id: deploy | |
run: | | |
cd tf/ecr | |
terraform apply -auto-approve -var-file=${{ github.workspace }}/tf/variables.tfvars | |
build: | |
needs: | |
- ecs-check | |
- ecr | |
if: ${{ needs.ecs-check.outputs.service_exists == 'false' }} | |
uses: ./.github/workflows/build.yml | |
with: | |
aws_account_id: ${{ vars.AWS_ACCOUNT_ID }} | |
aws_region: ${{ vars.AWS_REGION }} | |
aws_role: ${{ vars.AWS_ROLE }} | |
setup: | |
needs: | |
- ecs-check | |
- build | |
if: always() | |
uses: ./.github/workflows/setup.yml | |
with: | |
aws_account_id: ${{ vars.AWS_ACCOUNT_ID }} | |
aws_region: ${{ vars.AWS_REGION }} | |
aws_role: ${{ vars.AWS_ROLE }} | |
# Conditionally use task definition ARN from check if service exists, or from build if not | |
task_definition_arn: ${{ needs.ecs-check.outputs.service_exists == 'true' && needs.ecs-check.outputs.task_definition_arn || needs.build.outputs.task_definition_arn }} | |
test: | |
needs: setup | |
runs-on: ubuntu-latest | |
steps: | |
- name: echo url | |
run: echo url is ${{ needs.setup.outputs.api_invoke_url }} | |
- name: delay | |
run: sleep 300 | |
- name: Check if API returns 200 | |
run: | | |
STATUS_CODE=$(curl -o /dev/null -s -w "%{http_code}\n" ${{ needs.setup.outputs.api_invoke_url }}/host) | |
if [ "$STATUS_CODE" -eq 200 ]; then | |
echo "API is working correctly. Status code: $STATUS_CODE" | |
else | |
echo "API test failed. Status code: $STATUS_CODE" | |
exit 1 | |
fi |