Skip to content

Deploy Dezswap API

Deploy Dezswap API #32

Workflow file for this run

name: Deploy Dezswap API
on:
workflow_run:
workflows: ["CI"]
branches: [main]
types:
- completed
env:
APP_TYPE: api
AWS_REGION: ${{ secrets.AWS_REGION }}
ECR_REPOSITORY: dezswap-api
ECS_CLUSTER: dezswap-api
DIMENSION_ECS_SERVICE: dimension-dezswap-api
DIMENSION_TASK_DEFINITION: dimension-dezswap-api
DIMENSION_CONTAINER_NAME: dimension-dezswap-api
CUBE_ECS_SERVICE: cube-dezswap-api
CUBE_TASK_DEFINITION: cube-dezswap-api
CUBE_CONTAINER_NAME: cube-dezswap-api
jobs:
check_paths:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
outputs:
run_next_job: ${{ steps.check_paths.outputs.run_next_job }}
steps:
- name: Checkout repository
uses: actions/checkout@v2
with:
fetch-depth: 2
- name: Check changed file paths
id: check_paths
run: |
CHANGED_FILES=$(git diff HEAD~1 --name-only)
# files except api related
MATCHING_FILES=$(echo "$CHANGED_FILES" | grep -v '^indexer/')
if [[ -z "$MATCHING_FILES" ]]; then
echo "no mathing file"
echo "run_next_job=false" >> $GITHUB_OUTPUT
else
echo "mathing file $MATCHING_FILES"
echo "run_next_job=true" >> $GITHUB_OUTPUT
fi
build:
name: build dezswap-api api image
needs: check_paths
if: ${{ needs.check_paths.outputs.run_next_job == 'true' }}
runs-on: ubuntu-latest
environment: production
outputs:
dimension-tag: ${{ steps.build-image.outputs.dimension-tag }}
cube-tag: ${{ steps.build-image.outputs.cube-tag }}
steps:
- name: checkout
uses: actions/checkout@v3
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1.7.0
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1.5.1
- name: Test, build, tag, and push image to Amazon ECR
id: build-image
working-directory: .
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
DIMENSION_CONFIG: ${{ secrets.DIMENSION_CONFIG }}
CUBE_CONFIG: ${{ secrets.CUBE_CONFIG }}
APP_TYPE: ${{ env.APP_TYPE }}
run: |
make test
IMAGE_TAG=`git rev-parse --short HEAD`
touch config.yml
docker build --build-arg APP_TYPE=$APP_TYPE -t $ECR_REGISTRY/$ECR_REPOSITORY:latest -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
echo "FROM $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
COPY config.yml /app/config.yml" > Dockerfile.final
configs=("$DIMENSION_CONFIG" "$CUBE_CONFIG")
networks=("dimension" "cube")
for i in "${!configs[@]}"; do
echo "${configs[i]}" > config.yml
imgTag=${networks[i]}-$APP_TYPE-$IMAGE_TAG
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$imgTag -f Dockerfile.final .
echo "${networks[i]}-tag=$imgTag" >> $GITHUB_OUTPUT
done
docker image push -a $ECR_REGISTRY/$ECR_REPOSITORY
deploy-dimension:
name: Deploy dimension
runs-on: ubuntu-latest
needs: build
environment: production
steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1.7.0
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1.5.1
- name: Download Task Definition
id: download-task-definition
working-directory: .
run: |
aws ecs describe-task-definition --task-definition ${{ env.DIMENSION_TASK_DEFINITION }} | jq '.taskDefinition' > ${{ env.DIMENSION_TASK_DEFINITION }}.json
- name: Fill in the new image ID in the Amazon ECS task definition
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1.1.3
with:
task-definition: ./${{ env.DIMENSION_TASK_DEFINITION }}.json
container-name: ${{ env.DIMENSION_CONTAINER_NAME }}
image: ${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY}}:${{ needs.build.outputs.dimension-tag }}
- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1.4.11
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
service: ${{ env.DIMENSION_ECS_SERVICE }}
cluster: ${{ env.ECS_CLUSTER }}
wait-for-service-stability: true
deploy-cube:
name: Deploy cube
runs-on: ubuntu-latest
needs: build
environment: production
steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1.7.0
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1.5.1
- name: Download Task Definition
id: download-task-definition
working-directory: .
run: |
aws ecs describe-task-definition --task-definition ${{ env.CUBE_TASK_DEFINITION }} | jq '.taskDefinition' > ${{ env.CUBE_TASK_DEFINITION }}.json
- name: Fill in the new image ID in the Amazon ECS task definition
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1.1.1
with:
task-definition: ./${{ env.CUBE_TASK_DEFINITION }}.json
container-name: ${{ env.CUBE_CONTAINER_NAME }}
image: ${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY}}:${{ needs.build.outputs.cube-tag }}
- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1.4.11
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
service: ${{ env.CUBE_ECS_SERVICE }}
cluster: ${{ env.ECS_CLUSTER }}
wait-for-service-stability: true