|
| 1 | +name: Kite / _Deploy environment |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + agent_image: |
| 7 | + required: true |
| 8 | + type: string |
| 9 | + environment: |
| 10 | + required: true |
| 11 | + type: string |
| 12 | + github_environment: |
| 13 | + required: true |
| 14 | + type: string |
| 15 | + runtime_image: |
| 16 | + required: true |
| 17 | + type: string |
| 18 | + |
| 19 | +permissions: |
| 20 | + contents: read |
| 21 | + id-token: write |
| 22 | + |
| 23 | +concurrency: |
| 24 | + group: kite-deploy-${{ inputs.environment }} |
| 25 | + cancel-in-progress: ${{ inputs.environment == 'staging' }} |
| 26 | + |
| 27 | +jobs: |
| 28 | + deploy: |
| 29 | + runs-on: ubuntu-latest |
| 30 | + timeout-minutes: 30 |
| 31 | + environment: ${{ inputs.github_environment }} |
| 32 | + steps: |
| 33 | + - name: Configure AWS credentials |
| 34 | + uses: aws-actions/configure-aws-credentials@e6de054238d6b7531b4efff3b6587d9aade6a06c # v6.2.3 |
| 35 | + with: |
| 36 | + aws-region: ${{ vars.AWS_REGION }} |
| 37 | + role-to-assume: ${{ vars.AWS_ROLE_ARN }} |
| 38 | + role-session-name: kite-${{ inputs.environment }}-${{ github.run_id }} |
| 39 | + - name: Register images and update ECS service |
| 40 | + env: |
| 41 | + AGENT_IMAGE: ${{ inputs.agent_image }} |
| 42 | + DEPLOY_ENVIRONMENT: ${{ inputs.environment }} |
| 43 | + RUNTIME_IMAGE: ${{ inputs.runtime_image }} |
| 44 | + run: | |
| 45 | + set -euo pipefail |
| 46 | + [[ "$AGENT_IMAGE" =~ ^ghcr\.io/copilotkit/opentag-agent@sha256:[0-9a-f]{64}$ ]] |
| 47 | + [[ "$RUNTIME_IMAGE" =~ ^ghcr\.io/copilotkit/opentag-runtime@sha256:[0-9a-f]{64}$ ]] |
| 48 | + [[ "$DEPLOY_ENVIRONMENT" =~ ^(staging|prod|community)$ ]] |
| 49 | + service="kite-$DEPLOY_ENVIRONMENT" |
| 50 | + current_task_definition=$(aws ecs describe-services \ |
| 51 | + --cluster kite --services "$service" \ |
| 52 | + --query 'services[0].taskDefinition' --output text) |
| 53 | + test "$current_task_definition" != None |
| 54 | +
|
| 55 | + current_response="$RUNNER_TEMP/current-task-definition-response.json" |
| 56 | + current_json="$RUNNER_TEMP/current-task-definition.json" |
| 57 | + next_json="$RUNNER_TEMP/next-task-definition.json" |
| 58 | + aws ecs describe-task-definition \ |
| 59 | + --task-definition "$current_task_definition" \ |
| 60 | + --include TAGS > "$current_response" |
| 61 | + jq '.taskDefinition' "$current_response" > "$current_json" |
| 62 | + current_agent=$(jq -r '.containerDefinitions[] | select(.name == "agent") | .image' "$current_json") |
| 63 | + current_runtime=$(jq -r '.containerDefinitions[] | select(.name == "runtime") | .image' "$current_json") |
| 64 | +
|
| 65 | + task_definition="$current_task_definition" |
| 66 | + if [ "$current_agent" != "$AGENT_IMAGE" ] || [ "$current_runtime" != "$RUNTIME_IMAGE" ]; then |
| 67 | + jq --arg agent "$AGENT_IMAGE" --arg runtime "$RUNTIME_IMAGE" ' |
| 68 | + .containerDefinitions |= map( |
| 69 | + if .name == "agent" then .image = $agent |
| 70 | + elif .name == "runtime" then .image = $runtime |
| 71 | + else . end |
| 72 | + ) |
| 73 | + | del( |
| 74 | + .taskDefinitionArn, |
| 75 | + .revision, |
| 76 | + .status, |
| 77 | + .requiresAttributes, |
| 78 | + .compatibilities, |
| 79 | + .registeredAt, |
| 80 | + .registeredBy |
| 81 | + ) |
| 82 | + ' "$current_json" > "$next_json" |
| 83 | + task_definition=$(aws ecs register-task-definition \ |
| 84 | + --cli-input-json "file://$next_json" \ |
| 85 | + --tags "$(jq -c '.tags' "$current_response")" \ |
| 86 | + --query 'taskDefinition.taskDefinitionArn' --output text) |
| 87 | + aws ecs update-service \ |
| 88 | + --cluster kite \ |
| 89 | + --service "$service" \ |
| 90 | + --task-definition "$task_definition" \ |
| 91 | + --force-new-deployment >/dev/null |
| 92 | + fi |
| 93 | +
|
| 94 | + aws ecs wait services-stable --cluster kite --services "$service" |
| 95 | + deployed_task_definition=$(aws ecs describe-services \ |
| 96 | + --cluster kite --services "$service" \ |
| 97 | + --query 'services[0].taskDefinition' --output text) |
| 98 | + test "$deployed_task_definition" = "$task_definition" |
| 99 | + agent=$(aws ecs describe-task-definition --task-definition "$deployed_task_definition" \ |
| 100 | + --query "taskDefinition.containerDefinitions[?name=='agent'].image | [0]" --output text) |
| 101 | + runtime=$(aws ecs describe-task-definition --task-definition "$deployed_task_definition" \ |
| 102 | + --query "taskDefinition.containerDefinitions[?name=='runtime'].image | [0]" --output text) |
| 103 | + test "$agent" = "$AGENT_IMAGE" |
| 104 | + test "$runtime" = "$RUNTIME_IMAGE" |
| 105 | + running=$(aws ecs describe-services --cluster kite --services "$service" \ |
| 106 | + --query 'services[0].runningCount' --output text) |
| 107 | + test "$running" = "1" |
0 commit comments