forked from credebl/studio
-
Notifications
You must be signed in to change notification settings - Fork 0
120 lines (97 loc) · 5.17 KB
/
dev.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
name: Build and Deploy App to ECS
on:
push:
branches:
- pipeline-implementation
env:
ECR_IMAGE_TAG: "FRONTEND_V_${{ github.run_number }}"
ECR_REPOSITORY: "dev-services"
AWS_REGION: "ap-southeast-1"
CLUSTER: "DEV-NGOTAG-CLUSTER"
jobs:
build:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
steps:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v3
with:
role-to-assume: ${{ secrets.IAM_ROLE }}
aws-region: ${{ env.AWS_REGION }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Create .env file
run: |
echo "PUBLIC_BASE_URL=${{ secrets.PUBLIC_BASE_URL }}" > .env
echo "PUBLIC_CRYPTO_PRIVATE_KEY=${{ secrets.PUBLIC_CRYPTO_PRIVATE_KEY }}" >> .env
echo "PUBLIC_PLATFORM_NAME=${{ secrets.PUBLIC_PLATFORM_NAME }}" >> .env
echo "PUBLIC_PLATFORM_LOGO=${{ secrets.PUBLIC_PLATFORM_LOGO }}" >> .env
echo "PUBLIC_POWERED_BY=${{ secrets.PUBLIC_POWERED_BY }}" >> .env
echo "PUBLIC_PLATFORM_WEB_URL=${{ secrets.PUBLIC_PLATFORM_WEB_URL }}" >> .env
echo "PUBLIC_POWERED_BY_URL=${{ secrets.PUBLIC_POWERED_BY_URL }}" >> .env
echo "PUBLIC_PLATFORM_SUPPORT_EMAIL=${{ secrets.PUBLIC_PLATFORM_SUPPORT_EMAIL }}" >> .env
echo "PUBLIC_SHOW_NAME_AS_LOGO=${{ secrets.PUBLIC_SHOW_NAME_AS_LOGO }}" >> .env
echo "PUBLIC_ALLOW_DOMAIN=${{ secrets.PUBLIC_ALLOW_DOMAIN }}" >> .env
- name: Build, tag, and push image to Amazon ECR
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: ${{ env.ECR_REPOSITORY }}
IMAGE_TAG: ${{ env.ECR_IMAGE_TAG }}
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
docker image list
- name: Set environment variables
run: |
echo "ECR_REGISTRY=${{ steps.login-ecr.outputs.registry }}" >> $GITHUB_ENV
echo "ECR_REPOSITORY=${{ env.ECR_REPOSITORY }}" >> $GITHUB_ENV
echo "IMAGE_TAG=${{ env.ECR_IMAGE_TAG }}" >> $GITHUB_ENV
- name: Print environment variables
run: |
echo "ECR_REGISTRY: $ECR_REGISTRY"
echo "ECR_REPOSITORY: $ECR_REPOSITORY"
echo "IMAGE_TAG: $IMAGE_TAG"
- name: Retrieve Repository URI
run: |
REPOSITORY_URI=$(aws ecr describe-repositories --repository-names ${{ env.ECR_REPOSITORY }} --region ${{ env.AWS_REGION }} | jq -r '.repositories[].repositoryUri')
echo "REPOSITORY_URI=${REPOSITORY_URI}" >> $GITHUB_ENV
- name: Replace executionRoleArn in task definition
run: |
sed -i "s#\"executionRoleArn\": \"arn:aws:iam::.*:role/ecsTaskExecutionRole\"#\"executionRoleArn\": \"arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/ecsTaskExecutionRole\"#" taskdef/frontend-taskdef.json
- name: Update Task Definition and Service
run: |
FAMILY=$(sed -n 's/.*"family": "\(.*\)",/\1/p' taskdef/frontend-taskdef.json)
SERVICE_NAME="frontend-ui"
echo "SERVICE_NAME: $SERVICE_NAME"
# Replace placeholders in the JSON file
sed -e "s;%BUILD_NUMBER%;${{ github.run_number }};g" -e "s;%REPOSITORY_URI%;${REPOSITORY_URI};g" taskdef/frontend-taskdef.json > ${GITHUB_WORKSPACE}/${NAME}-v_${{ github.run_number }}.json
# Debug: Print the content of the modified JSON file
cat ${GITHUB_WORKSPACE}/${NAME}-v_${{ github.run_number }}.json
# Register the task definition using the modified JSON file
aws ecs register-task-definition --family ${FAMILY} --cli-input-json file://${GITHUB_WORKSPACE}/${NAME}-v_${{ github.run_number }}.json --region ${{ env.AWS_REGION }}
SERVICE_INFO=$(aws ecs describe-services --services ${SERVICE_NAME} --cluster ${{ env.CLUSTER }} --region ${{ env.AWS_REGION }})
# Check if the service exists
if [ "$(echo "$SERVICE_INFO" | jq -r '.services | length')" -eq 0 ]; then
echo "Service does not exist, creating new service..."
# Your logic to create a new service goes here
else
echo "Entered existing service"
# Extract desired count from the stored service info
DESIRED_COUNT=$(echo "$SERVICE_INFO" | jq -r '.services[].desiredCount')
echo "DESIRED_COUNT: $DESIRED_COUNT"
if [ "$DESIRED_COUNT" = "0" ]; then
DESIRED_COUNT="1"
fi
# Get the latest task definition revision
REVISION=$(aws ecs describe-task-definition --task-definition ${FAMILY} --region ${{ env.AWS_REGION }} | jq -r '.taskDefinition.revision')
echo "REVISION: $REVISION"
# Update the existing service
aws ecs update-service --cluster ${{ env.CLUSTER }} --region ${{ env.AWS_REGION }} --service ${SERVICE_NAME} --task-definition ${FAMILY}:${REVISION} --desired-count ${DESIRED_COUNT}
fi