Skip to content

♻️ 스케줄러 서버로 검색 로그 전송 (#254) (#278) #26

♻️ 스케줄러 서버로 검색 로그 전송 (#254) (#278)

♻️ 스케줄러 서버로 검색 로그 전송 (#254) (#278) #26

name: CI/CD Pipeline for Dev Batch
on:
push:
branches:
- develop
paths-ignore:
- 'application/**'
- '.github/**'
jobs:
build:
runs-on: ubuntu-latest
env:
MODULE_NAME: scheduler
steps:
- name: Parse combined secrets
id: parse_secrets
run: |
echo "Extracting secrets..."
echo '${{ secrets.DEV_BATCH_META_DATA }}' | jq -r 'to_entries | .[] | "echo \(.key)=\(.value) >> $GITHUB_ENV"' | bash
- name: Checkout source code
uses: actions/checkout@v3
with:
token: ${{ env.TOKEN_GITHUB }}
submodules: true
- name: Set up JDK 17
uses: actions/setup-java@v1
with:
java-version: 17
- name: Grant execute permission for gradlew
run: chmod +x gradlew
shell: bash
- name: Build with Gradle
run: SPRING_PROFILES_ACTIVE=test ./gradlew :${{ env.MODULE_NAME }}:clean :${{ env.MODULE_NAME }}:build
shell: bash
- name: Upload build artifact (JAR and Dockerfile)
uses: actions/upload-artifact@v3
with:
name: build-artifacts
path: |
./${{ env.MODULE_NAME }}/build/libs/*.jar
./${{ env.MODULE_NAME }}/Dockerfile
upload-docker-image:
runs-on: ubuntu-latest
needs: build
steps:
- name: Parse combined secrets
id: parse_secrets
run: |
echo "Extracting secrets..."
echo '${{ secrets.DEV_BATCH_META_DATA }}' | jq -r 'to_entries | .[] | "echo \(.key)=\(.value) >> $GITHUB_ENV"' | bash
- name: Download build artifact (JAR and Dockerfile)
uses: actions/download-artifact@v3
with:
name: build-artifacts
- name: Log in to Amazon ECR Public
run: |
aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin ${{ env.ECR_REPOSITORY_URI }}
- name: Build Docker image
run: docker build --build-arg PROFILE=${{ env.ENVIRONMENT }} -t ${{ env.ECR_REPOSITORY_URI }}:latest .
- name: Push Docker image to Amazon ECR
run: docker push ${{ env.ECR_REPOSITORY_URI }}:latest
deploy:
runs-on: ubuntu-latest
needs: upload-docker-image
steps:
- name: Parse combined secrets
id: parse_secrets
run: |
echo "Extracting secrets..."
echo '${{ secrets.DEV_BATCH_META_DATA }}' | jq -r 'to_entries | .[] | "echo \(.key)=\(.value) >> $GITHUB_ENV"' | bash
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ env.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ env.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- name: Get EC2 instance ID
id: get-instance-id
run: |
INSTANCE_ID=$(aws ec2 describe-instances \
--filters "Name=tag:Type,Values=${{ env.EC2_TAG_NAME }}" "Name=instance-state-name,Values=running" \
--query "Reservations[*].Instances[*].InstanceId" \
--output text \
--region ${{ env.AWS_REGION }})
if [ -z "$INSTANCE_ID" ]; then
echo "No running instance found with tag Type=batch"
exit 1
fi
echo "Instance ID: $INSTANCE_ID"
echo "::set-output name=instance_id::$INSTANCE_ID"
- name: Deploy to EC2 using SSM
id: deploy-ssm
run: |
COMMAND_ID=$(aws ssm send-command \
--document-name "AWS-RunShellScript" \
--targets "Key=InstanceIds,Values=${{ steps.get-instance-id.outputs.instance_id }}" \
--parameters 'commands=["${{ env.DEPLOY_COMMAND }}"]' \
--comment "Deploy new Docker container" \
--query "Command.CommandId" \
--output text \
--region ${{ env.AWS_REGION }})
if [ -z "$COMMAND_ID" ]; then
echo "Failed to send command"
exit 1
fi
echo "SSM Command ID: $COMMAND_ID"
echo "::set-output name=command_id::$COMMAND_ID"
- name: Monitor SSM Command
run: |
STATUS="InProgress"
while [ "$STATUS" = "InProgress" ] || [ "$STATUS" = "Pending" ]; do
STATUS=$(aws ssm list-command-invocations \
--command-id ${{ steps.deploy-ssm.outputs.command_id }} \
--details \
--query "CommandInvocations[0].Status" \
--output text \
--region ${{ env.AWS_REGION }})
echo "Current status: $STATUS"
sleep 5
done
if [ "$STATUS" = "Success" ]; then
echo "Deployment succeeded!"
exit 0
else
echo "Deployment failed with status: $STATUS"
exit 1
fi