Skip to content

Update main.yml

Update main.yml #19

Workflow file for this run

name: Build Spring Application and Push to ECR
on:
push:
branches:
- main
jobs:
build: # jobs 섹션의 작업 이름으로 올바르게 설정
runs-on: ubuntu-latest
outputs:
image_tag: ${{ steps.get_version.outputs.version }}
steps:
- name: Checkout repository
uses: actions/checkout@v3
# 1. JDK 설치 후 Spring 애플리케이션 빌드
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
distribution: "temurin"
java-version: '17'
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build Spring Application with Secrets
env:
SPRING_DATASOURCE_URL: ${{ secrets.SPRING_DATASOURCE_URL }}
SPRING_DATASOURCE_USERNAME: ${{ secrets.SPRING_DATASOURCE_USERNAME }}
SPRING_DATASOURCE_PASSWORD: ${{ secrets.SPRING_DATASOURCE_PASSWORD }}
run: |
./gradlew clean bootJar
# 2. AWS CLI 로그인 및 ECR 리포지토리 생성
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.INFRA_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.INFRA_SECRET_ACCESS_KEY }}
aws-region: ap-northeast-2
- name: Log in to Amazon ECR
run: |
aws ecr get-login-password --region ap-northeast-2 | docker login --username AWS --password-stdin "${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.ap-northeast-2.amazonaws.com"
# 3. 이미지 태그 생성 (날짜-시간 형식)
- name: Generate image tag
id: get_version
run: echo "version=$(date +'%Y%m%d-%H%M%S')" >> $GITHUB_OUTPUT
- name: Build Docker Image with AWS CLI and Terraform
env:
ECR_REGISTRY: ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.ap-northeast-2.amazonaws.com
ECR_REPOSITORY: aiwa-project/member
IMAGE_TAG: ${{ steps.get_version.outputs.version }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
SPRING_DATASOURCE_URL: ${{ secrets.SPRING_DATASOURCE_URL }}
SPRING_DATASOURCE_USERNAME: ${{ secrets.SPRING_DATASOURCE_USERNAME }}
SPRING_DATASOURCE_PASSWORD: ${{ secrets.SPRING_DATASOURCE_PASSWORD }}
run: |
docker build \
--build-arg AWS_ACCESS_KEY_ID=${{ secrets.AWS_ACCESS_KEY_ID }} \
--build-arg AWS_SECRET_ACCESS_KEY=${{ secrets.AWS_SECRET_ACCESS_KEY }} \
--build-arg SPRING_DATASOURCE_URL=${{ secrets.SPRING_DATASOURCE_URL }} \
--build-arg SPRING_DATASOURCE_USERNAME=${{ secrets.SPRING_DATASOURCE_USERNAME }} \
--build-arg SPRING_DATASOURCE_PASSWORD=${{ secrets.SPRING_DATASOURCE_PASSWORD }} \
-t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG \
-t $ECR_REGISTRY/$ECR_REPOSITORY:latest \
.
# 5. ECR로 Docker 이미지 푸시
- name: Push Docker Image to Amazon ECR
env:
ECR_REGISTRY: ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.ap-northeast-2.amazonaws.com
ECR_REPOSITORY: aiwa-project/member
IMAGE_TAG: ${{ steps.get_version.outputs.version }}
run: |
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
docker push $ECR_REGISTRY/$ECR_REPOSITORY:latest
# 6. Helm 차트 저장소 체크아웃
- name: Checkout Helm chart repository
uses: actions/checkout@v4
with:
repository: AWS-Cloud-School-6/AIWA-MCP-Helm
path: AIWA-MCP-Helm
token: ${{ secrets.PAT_AIWA_MEMBER_TOKEN }}
# 7. Helm 차트 업데이트
- name: Update Helm chart
run: |
cd AIWA-MCP-Helm/aiwa-member
if [ ! -f values.yaml ]; then
echo "Error: values.yaml not found in $(pwd)"
exit 1
fi
sed -i 's|tag: .*|tag: "${{ steps.get_version.outputs.version }}"|' values.yaml
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add values.yaml
if git diff --staged --quiet; then
echo "No changes to commit in values.yaml"
else
git commit -m "Update image tag to ${{ steps.get_version.outputs.version }}"
git push || { echo "Failed to push changes"; exit 2; }
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}