|
| 1 | +name: Deploy to Amazon ECS |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - dev |
| 7 | + workflow_dispatch: |
| 8 | + |
| 9 | +env: |
| 10 | + AWS_REGION: ${{ secrets.AWS_REGION }} |
| 11 | + ECR_REPOSITORY: ${{ secrets.ECR_REPOSITORY }} |
| 12 | + ECS_CLUSTER: ${{ secrets.ECS_CLUSTER }} |
| 13 | + ECS_EXECUTION_ROLE_ARN: ${{ secrets.ECS_EXECUTION_ROLE_ARN }} |
| 14 | + APP_NAME: dataspace |
| 15 | + APP_PORT: 8000 |
| 16 | + DB_ENGINE: django.db.backends.postgresql |
| 17 | + DB_PORT: 5432 |
| 18 | + DEBUG_MODE: "False" |
| 19 | + TELEMETRY_URL: http://otel-collector:4317 |
| 20 | + CPU_UNITS: 256 |
| 21 | + MEMORY_UNITS: 512 |
| 22 | + SSM_PATH_PREFIX: /dataspace |
| 23 | + ENVIRONMENT: ${{ secrets.ENVIRONMENT || 'dev' }} |
| 24 | + |
| 25 | +jobs: |
| 26 | + deploy-infrastructure: |
| 27 | + name: Deploy Infrastructure |
| 28 | + runs-on: ubuntu-latest |
| 29 | + environment: development |
| 30 | + if: github.event_name == 'workflow_dispatch' || contains(github.event.head_commit.modified, 'aws/cloudformation') |
| 31 | + |
| 32 | + steps: |
| 33 | + - name: Checkout |
| 34 | + uses: actions/checkout@v3 |
| 35 | + |
| 36 | + - name: Configure AWS credentials |
| 37 | + uses: aws-actions/configure-aws-credentials@v1 |
| 38 | + with: |
| 39 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 40 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 41 | + aws-region: ${{ env.AWS_REGION }} |
| 42 | + |
| 43 | + - name: Deploy CloudFormation stack |
| 44 | + run: | |
| 45 | + aws cloudformation deploy \ |
| 46 | + --template-file aws/cloudformation/dataspace-infrastructure.yml \ |
| 47 | + --stack-name dataspace-${{ env.ENVIRONMENT }}-infrastructure \ |
| 48 | + --parameter-overrides \ |
| 49 | + Environment=${{ env.ENVIRONMENT }} \ |
| 50 | + VpcId=${{ secrets.VPC_ID }} \ |
| 51 | + SubnetIds=${{ secrets.SUBNET_IDS }} \ |
| 52 | + DBUsername=${{ secrets.DB_USERNAME }} \ |
| 53 | + DBPassword=${{ secrets.DB_PASSWORD }} \ |
| 54 | + DBName=${{ secrets.DB_NAME }} \ |
| 55 | + ElasticsearchPassword=${{ secrets.ELASTICSEARCH_PASSWORD }} \ |
| 56 | + DjangoSecretKey=${{ secrets.DJANGO_SECRET_KEY }} \ |
| 57 | + --capabilities CAPABILITY_IAM \ |
| 58 | + --no-fail-on-empty-changeset |
| 59 | +
|
| 60 | + deploy-app: |
| 61 | + name: Deploy Application |
| 62 | + runs-on: ubuntu-latest |
| 63 | + environment: development |
| 64 | + needs: deploy-infrastructure |
| 65 | + if: always() # Run even if infrastructure deployment is skipped |
| 66 | + |
| 67 | + steps: |
| 68 | + - name: Checkout |
| 69 | + uses: actions/checkout@v3 |
| 70 | + |
| 71 | + - name: Configure AWS credentials |
| 72 | + uses: aws-actions/configure-aws-credentials@v1 |
| 73 | + with: |
| 74 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 75 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 76 | + aws-region: ${{ env.AWS_REGION }} |
| 77 | + |
| 78 | + - name: Login to Amazon ECR |
| 79 | + id: login-ecr |
| 80 | + uses: aws-actions/amazon-ecr-login@v1 |
| 81 | + |
| 82 | + - name: Build, tag, and push image to Amazon ECR |
| 83 | + id: build-image |
| 84 | + env: |
| 85 | + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} |
| 86 | + IMAGE_TAG: ${{ github.sha }} |
| 87 | + run: | |
| 88 | + docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . |
| 89 | + docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG |
| 90 | + echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT |
| 91 | +
|
| 92 | + - name: Download task definition and get EFS ID |
| 93 | + run: | |
| 94 | + aws ecs describe-task-definition --task-definition dataspace --query taskDefinition > aws/current-task-definition.json |
| 95 | + aws ecs describe-task-definition --task-definition dataspace-otel-collector --query taskDefinition > aws/current-otel-task-definition.json |
| 96 | + # Get the EFS ID from CloudFormation export |
| 97 | + EFS_ID=$(aws cloudformation list-exports --query "Exports[?Name=='dataspace-${{ env.ENVIRONMENT }}-MigrationsFileSystemId'].Value" --output text) |
| 98 | + echo "EFS_ID=$EFS_ID" >> $GITHUB_ENV |
| 99 | +
|
| 100 | + - name: Update container image only |
| 101 | + id: task-def-app |
| 102 | + uses: aws-actions/amazon-ecs-render-task-definition@v1 |
| 103 | + with: |
| 104 | + task-definition: aws/current-task-definition.json |
| 105 | + container-name: dataspace |
| 106 | + image: ${{ steps.build-image.outputs.image }} |
| 107 | + |
| 108 | + - name: Deploy main application ECS task definition |
| 109 | + uses: aws-actions/amazon-ecs-deploy-task-definition@v1 |
| 110 | + with: |
| 111 | + task-definition: ${{ steps.task-def-app.outputs.task-definition }} |
| 112 | + service: ${{ secrets.ECS_SERVICE }} |
| 113 | + cluster: ${{ env.ECS_CLUSTER }} |
| 114 | + wait-for-service-stability: true |
| 115 | + |
| 116 | + deploy-otel: |
| 117 | + name: Deploy OpenTelemetry Collector |
| 118 | + runs-on: ubuntu-latest |
| 119 | + environment: development |
| 120 | + needs: deploy-app |
| 121 | + |
| 122 | + steps: |
| 123 | + - name: Checkout |
| 124 | + uses: actions/checkout@v3 |
| 125 | + |
| 126 | + - name: Configure AWS credentials |
| 127 | + uses: aws-actions/configure-aws-credentials@v1 |
| 128 | + with: |
| 129 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 130 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 131 | + aws-region: ${{ env.AWS_REGION }} |
| 132 | + |
| 133 | + - name: Download current OpenTelemetry task definition |
| 134 | + id: download-otel-taskdef |
| 135 | + run: | |
| 136 | + aws ecs describe-task-definition \ |
| 137 | + --task-definition dataspace-otel-collector \ |
| 138 | + --query taskDefinition > aws/current-otel-task-definition.json |
| 139 | + cat aws/current-otel-task-definition.json |
| 140 | +
|
| 141 | + - name: Deploy OpenTelemetry ECS task definition |
| 142 | + uses: aws-actions/amazon-ecs-deploy-task-definition@v1 |
| 143 | + with: |
| 144 | + task-definition: aws/current-otel-task-definition.json |
| 145 | + service: ${{ secrets.ECS_OTEL_SERVICE }} |
| 146 | + cluster: ${{ env.ECS_CLUSTER }} |
| 147 | + wait-for-service-stability: true |
0 commit comments