1+ name : Backend CI/CD
2+
3+ on :
4+ push :
5+ branches :
6+ - dev
7+ paths :
8+ - ' backend/**'
9+
10+ permissions :
11+ contents : write
12+ id-token : write
13+
14+ jobs :
15+ detect-changes :
16+ runs-on : ubuntu-latest
17+ outputs :
18+ services : ${{ steps.detect.outputs.services }}
19+ commit_hash : ${{ steps.hash.outputs.commit_hash }}
20+ steps :
21+ - name : Checkout
22+ uses : actions/checkout@v4
23+ with :
24+ fetch-depth : 0
25+
26+ - name : Detect changed services
27+ id : detect
28+ run : |
29+ git fetch origin dev
30+ CHANGED=$(git diff --name-only ${{ github.event.before }} ${{ github.sha }} | grep '^backend/' | cut -d '/' -f2 | sort | uniq | jq -R -s -c 'split("\n") | map(select(. != ""))')
31+ echo "services=$CHANGED" >> $GITHUB_OUTPUT
32+
33+ - name : Get short git commit hash
34+ id : hash
35+ run : echo "commit_hash=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
36+
37+ build-and-push :
38+ needs : detect-changes
39+ runs-on : ubuntu-latest
40+ if : ${{ fromJson(needs.detect-changes.outputs.services) != '[]' }}
41+ strategy :
42+ matrix :
43+ service : ${{ fromJson(needs.detect-changes.outputs.services) }}
44+ fail-fast : false
45+ env :
46+ IMAGE_TAG : ${{ needs.detect-changes.outputs.commit_hash }}
47+ steps :
48+ - name : Checkout
49+ uses : actions/checkout@v4
50+
51+ - name : Set up JDK 17
52+ uses : actions/setup-java@v4
53+ with :
54+ java-version : ' 17'
55+ distribution : ' temurin'
56+ cache : maven
57+
58+ - name : Configure AWS credentials
59+ uses : aws-actions/configure-aws-credentials@v4
60+ with :
61+ role-to-assume : arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/mapzip-dev-GitHubActionsOIDCRole
62+ role-session-name : GitHub_to_AWS_via_FederatedOIDC
63+ aws-region : ap-northeast-2
64+
65+ - name : Login to Amazon ECR Private
66+ id : login-ecr-private
67+ uses : aws-actions/amazon-ecr-login@v2
68+
69+ - name : Build and push Docker image
70+ id : build
71+ env :
72+ REGISTRY : ${{ steps.login-ecr-private.outputs.registry }}
73+ run : |
74+ SERVICE="${{ matrix.service }}"
75+ echo "Building and pushing $SERVICE"
76+
77+ cd "./backend/$SERVICE" || exit 1
78+ mvn -B package -DskipTests --file pom.xml
79+ mv ./target/*.jar ./target/app.jar
80+ cd - || exit 1
81+
82+ docker build -t "$REGISTRY/mapzip-dev-ecr-$SERVICE:$IMAGE_TAG" "./backend/$SERVICE"
83+ docker push "$REGISTRY/mapzip-dev-ecr-$SERVICE:$IMAGE_TAG"
84+
85+ - name : Save successful service name
86+ if : success()
87+ run : |
88+ mkdir -p success
89+ echo "${{ matrix.service }}" > "success/${{ matrix.service }}.txt"
90+
91+ - name : Upload success list
92+ if : success()
93+ uses : actions/upload-artifact@v4
94+ with :
95+ name : successful-service-${{ matrix.service }}
96+ path : success/${{ matrix.service }}.txt
97+
98+ update-argocd-yaml :
99+ needs :
100+ - detect-changes
101+ - build-and-push
102+ runs-on : ubuntu-latest
103+ if : ${{ fromJson(needs.detect-changes.outputs.services) != '[]' }}
104+ steps :
105+ - name : Download all success artifacts
106+ uses : actions/download-artifact@v4
107+ with :
108+ pattern : successful-service-*
109+ path : ./services
110+ merge-multiple : true
111+
112+ - name : Read success list
113+ id : get-success
114+ run : |
115+ SUCCESS_SERVICES=""
116+ if [ -d "./services" ]; then
117+ for file in ./services/*.txt; do
118+ if [ -f "$file" ]; then
119+ SERVICE=$(basename "$file" .txt)
120+ SUCCESS_SERVICES="$SUCCESS_SERVICES $SERVICE"
121+ fi
122+ done
123+ fi
124+ echo "success_services=$(echo $SUCCESS_SERVICES | xargs)" >> $GITHUB_OUTPUT
125+
126+ - name : Checkout Infra repo
127+ uses : actions/checkout@v4
128+ with :
129+ repository : CLD3rd-Team4/Infra
130+ ref : dev
131+ token : ${{ secrets.INFRA_PAT }}
132+ path : infra
133+
134+ - name : Update YAMLs with new image tags
135+ env :
136+ IMAGE_TAG : ${{ needs.detect-changes.outputs.commit_hash }}
137+ run : |
138+ INFRA_PATH="argocd"
139+ git config --global user.name "github-actions"
140+ git config --global user.email "github-actions@github.com"
141+ cd infra
142+
143+ for SERVICE_NAME in ${{ steps.get-success.outputs.success_services }}; do
144+ echo "Updating image tag for service: $SERVICE_NAME"
145+
146+ if [[ "$SERVICE_NAME" == "auth" || "$SERVICE_NAME" == "gateway" || "$SERVICE_NAME" == "config" ]]; then
147+ SERVICE_DIR="$INFRA_PATH/platform"
148+ else
149+ SERVICE_DIR="$INFRA_PATH/service-$SERVICE_NAME"
150+ fi
151+
152+ FILE_NAME="${SERVICE_NAME#service-}.yaml"
153+ YAML_FILE="$SERVICE_DIR/$FILE_NAME"
154+
155+ if [ ! -f "$YAML_FILE" ]; then
156+ echo "Warning: YAML file not found: $YAML_FILE"
157+ continue
158+ fi
159+
160+ sed -i -E "/containers:/,/(^[[:space:]]*[^-[:space:]]|^$)/ s|(image:[[:space:]]*[^[:space:]]+:)[^[:space:]]+|\1$IMAGE_TAG|" "$YAML_FILE"
161+
162+ git add "$YAML_FILE"
163+ git commit -m "Update $SERVICE_NAME image tag to $IMAGE_TAG [ci skip]" || echo "No changes to commit for $SERVICE_NAME"
164+ done
165+
166+ git push origin dev
167+
0 commit comments