Skip to content

CI / CD 내의 Docker 명령어를 수정한다. (#52) #16

CI / CD 내의 Docker 명령어를 수정한다. (#52)

CI / CD 내의 Docker 명령어를 수정한다. (#52) #16

Workflow file for this run

name: Deploy to AWS EC2
on:
push:
branches:
- prod
- dev
env:
AWS_REGION: ap-northeast-2
CONTAINER_NAME: gotchai-server
API_SPECIFICATION_PATH: ./api/src/main/resources/static/docs
jobs:
build:
name: Deploy API specification
runs-on: ubuntu-latest
environment: ${{ github.ref_name }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up JDK
uses: actions/setup-java@v4
with:
distribution: corretto
java-version: 21
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- name: Create docs directory
run: mkdir -p ${{ env.API_SPECIFICATION_PATH }}
- name: Run tests
run: ./gradlew test
- name: Upload API Specification to S3
run: aws s3 cp "${{ env.API_SPECIFICATION_PATH }}/api.yml" "s3://${{ secrets.API_SPECIFICATION_BUCKET }}/api.yml"
deploy-image:
name: Deploy image
runs-on: ubuntu-latest
environment: ${{ github.ref_name }}
needs: build
outputs:
image: ${{ steps.set-image.outputs.image }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up JDK
uses: actions/setup-java@v4
with:
distribution: corretto
java-version: 21
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- name: Set image
id: set-image
run: echo "image=${{ secrets.ECR_REGISTRY }}/${{ secrets.ECR_REPOSITORY }}:${{ github.sha }}" >> $GITHUB_OUTPUT
- name: Build and deploy image to AWS ECR
run: |
./gradlew :api:jib \
-Djib.to.image=${{ secrets.ECR_REGISTRY }}/${{ secrets.ECR_REPOSITORY }}:${{ github.sha }} \
-Djib.to.auth.username=AWS \
-Djib.to.auth.password=$(aws ecr get-login-password)
deploy-container:
name: Deploy container
runs-on: ubuntu-latest
needs: deploy-image
environment: ${{ github.ref_name }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Send necessary files to EC2
uses: appleboy/[email protected]
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.EC2_PRIVATE_KEY }}
source: "docker/docker-compose.yml"
target: "~"
- name: Build and deploy container to AWS EC2
uses: appleboy/[email protected]
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.EC2_PRIVATE_KEY }}
script: |
aws secretsmanager get-secret-value --secret-id ${{ github.ref_name }}-env --region ap-northeast-2 --query SecretString --output text | jq -r '. | to_entries | map("\(.key)=\(.value)") | .[]' > .env
aws ecr get-login-password | docker login --username AWS --password-stdin ${{ secrets.ECR_REGISTRY }}
export IMAGE_URI=${{ secrets.ECR_REGISTRY }}/${{ secrets.ECR_REPOSITORY }}:${{ github.sha }}
docker-compose --env-file .env up -d --build
notify-discord:
name: Notify Discord
runs-on: ubuntu-latest
needs: deploy-container
steps:
- name: Send Discord notification
run: |
curl -X POST -H "Content-Type: application/json" -d '{
"username": "GitHub Actions",
"avatar_url": "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png",
"embeds": [
{
"title": "**${{ github.ref_name == 'prod' && '프로덕션' || '개발' }} 환경 배포 성공**",
"description": "프로젝트가 성공적으로 배포되었습니다.",
"color": 3066993,
"fields": [
{
"name": "Repository",
"value": "[${{ github.repository }}](https://github.com/${{ github.repository }})",
"inline": true
},
{
"name": "Branch",
"value": "${{ github.ref_name }}",
"inline": true
},
{
"name": "Commit",
"value": "[$(git log -1 --pretty=%s)](https://github.com/${{ github.repository }}/commit/${{ github.sha }})",
"inline": true
},
{
"name": "Author",
"value": "[${{ github.actor }}](${{ github.event.sender.html_url }})",
"inline": true
}
]
}
]
}' ${{ secrets.DISCORD_WEBHOOK_URL }}