|
1 | 1 | #!/bin/bash |
2 | 2 |
|
| 3 | +# Function to log into AWS ECR |
| 4 | +aws_ecr_login() { |
| 5 | + echo ">>> Logging into AWS ECR" |
| 6 | + aws ecr get-login-password --region "$AWS_REGION" | docker login --username AWS --password-stdin "$AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com" |
| 7 | +} |
| 8 | + |
| 9 | +# Function to build and push Docker image |
| 10 | +build_and_push_image() { |
| 11 | + local service_name="$1" |
| 12 | + local image_name="nestjs_$service_name:latest" |
| 13 | + local ecr_image_uri="$AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$service_name:latest" |
| 14 | + |
| 15 | + echo ">>> Building $image_name" |
| 16 | + docker build -t "$image_name" -f "$service_name/Dockerfile" "$service_name" |
| 17 | + |
| 18 | + if [ $? -eq 0 ]; then |
| 19 | + echo ">>> Tagging $image_name" |
| 20 | + docker tag "$image_name" "$ecr_image_uri" |
| 21 | + |
| 22 | + echo ">>> Pushing $image_name" |
| 23 | + docker push "$ecr_image_uri" |
| 24 | + |
| 25 | + if [ $? -eq 0 ]; then |
| 26 | + echo ">>> Deployment completed for $service_name" |
| 27 | + else |
| 28 | + echo ">>> Failed to push image $image_name" >&2 |
| 29 | + fi |
| 30 | + else |
| 31 | + echo ">>> Failed to build image $image_name" >&2 |
| 32 | + fi |
| 33 | +} |
| 34 | + |
| 35 | +# Main script execution |
3 | 36 | printf "Please select service:\n" |
4 | 37 | select d in */; do |
5 | 38 | test -n "$d" && break |
|
9 | 42 | service_name="${d%/}" |
10 | 43 |
|
11 | 44 | if [ "$service_name" == 'fluent-bit' ]; then |
| 45 | + echo ">>> Service fluent-bit is not allowed for deployment" |
12 | 46 | exit 1 |
13 | 47 | fi |
14 | 48 |
|
15 | | -service_dir="./$service_name" |
16 | | - |
17 | | -if [ ! -e "$service_dir/Dockerfile" ]; then |
| 49 | +if [ ! -e "$service_name/Dockerfile" ]; then |
18 | 50 | echo ">>> Dockerfile not found for service $service_name" |
19 | 51 | exit 1 |
20 | 52 | fi |
21 | 53 |
|
22 | 54 | echo ">>> Deploying service $service_name..." |
23 | | -cd "$service_dir" || exit |
24 | | - |
25 | | -aws_ecr_login() { |
26 | | - echo ">>> Logging into AWS ECR" |
27 | | - aws ecr get-login-password --region "$AWS_REGION" | docker login --username AWS --password-stdin "$AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/nestjs-microservices-$service_name" |
28 | | -} |
29 | | - |
30 | | -build_and_push_image() { |
31 | | - local image_name="nestjs_$service_name:latest" |
32 | | - echo ">>> Building $image_name" |
33 | | - docker build -t "$image_name" -f Dockerfile . |
34 | | - echo ">>> Tagging $image_name" |
35 | | - docker tag "$image_name" "$AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$service_name:latest" |
36 | | - echo ">>> Pushing $image_name" |
37 | | - docker push "$AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$service_name:latest" |
38 | | -} |
39 | | - |
40 | | -aws_ecr_login && build_and_push_image |
41 | | - |
42 | | -echo ">>> Deployment completed for $service_name" |
43 | | -cd .. |
| 55 | +aws_ecr_login && build_and_push_image "$service_name" |
0 commit comments