Skip to content

Merge pull request #166 from Media-XI/hotfix #93

Merge pull request #166 from Media-XI/hotfix

Merge pull request #166 from Media-XI/hotfix #93

Workflow file for this run

name: Docker Prod CI
on:
push:
branches: [ "main" ]
env:
# Use docker.io for Docker Hub if empty
REGISTRY: docker.io
# github.repository as <account>/<repo>
IMAGE_NAME: shonn/art-be
DOCKER_REGISTRY_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKER_REGISTRY_PASSWORD: ${{ secrets.DOCKERHUB_PASSWORD }}
JASYPT_ENCRYPTOR_PASSWORD: ${{ secrets.JASYPT_ENCRYPTOR_PASSWORD }}
AWS_S3_BUCKET_NAME: ${{ secrets.AWS_S3_BUCKET_NAME }}
AWS_CODE_DEPLOY_NAME: artscope-codedeploy
AWS_CODE_DEPLOY_GROUP: artscope-codedeploy-group
jobs:
build-dockerized-production:
name: Build and push Docker image
runs-on: ubuntu-latest
services:
redis:
image: redis
ports:
- 16379:6379
steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
- name: Cache Gradle packages
uses: actions/cache@v2
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: Grant execute permission for gradlew
run: chmod +x ./gradlew
- name: Build with Gradle
uses: gradle/gradle-build-action@v2.4.2
with:
arguments: |
build
- name: Setup Docker buildx
uses: docker/setup-buildx-action@79abd3f86f79a9d68a23c75a09a9a85889262adf
- name: Set Docker Image Name (Build Info)
run: echo "DOCKER_IMAGE_NAME=${{ env.IMAGE_NAME }}:${{ secrets.MAJOR }}.${{ secrets.MINOR }}" >> $GITHUB_ENV
- name: Build the Docker image
run: docker build --tag ${{ env.DOCKER_IMAGE_NAME }} --build-arg BUILD_INFO=${{ env.DOCKER_IMAGE_NAME }} -f Dockerfile.prod .
- name: Login to registry ${{ env.REGISTRY }}
run: echo "$DOCKER_REGISTRY_PASSWORD" | docker login "$REGISTRY" -u "$DOCKER_REGISTRY_USERNAME" --password-stdin
- name: Push to ${{ env.REGISTRY }}
run: docker push ${{ env.DOCKER_IMAGE_NAME }}
# Docker 이미지 이름을 image.txt 파일에 쓰기
- name: Write Docker image name to file
run: echo "${{ env.DOCKER_IMAGE_NAME }}" > image.txt
- name: Create zip file for AWS CodeDeploy
run: mkdir ${{ env.AWS_CODE_DEPLOY_NAME }} && cp -r appspec.yml image.txt scripts ${{ env.AWS_CODE_DEPLOY_NAME }}
# AWS 설정
- name: AWS Configure
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_KEY }}
aws-region: ap-northeast-2
# AWS S3로 배포 파일 업로드
- name: Upload to AWS S3
run: |
aws deploy push \
--application-name ${{ env.AWS_CODE_DEPLOY_NAME }} \
--s3-location s3://${{ env.AWS_S3_BUCKET_NAME }}/codedeploy/$GITHUB_SHA.zip \
--ignore-hidden-files \
--source ${{ env.AWS_CODE_DEPLOY_NAME }}
# AWS EC2 CodeDeploy 배포 요청
- name: Delpoy to AWS EC2
run: |
aws deploy create-deployment \
--application-name ${{ env.AWS_CODE_DEPLOY_NAME }} \
--deployment-config-name CodeDeployDefault.OneAtATime \
--deployment-group-name ${{ env.AWS_CODE_DEPLOY_GROUP }} \
--description "Deploy artscope" \
--s3-location bucket=$AWS_S3_BUCKET_NAME,key=codedeploy/$GITHUB_SHA.zip,bundleType=zip
# 다음 버전으로 업데이트
- name: Autoincrement a new minor version
run: |
echo "NEW_MINOR_VERSION=$((${{ secrets.MINOR }}+1))" >> $GITHUB_ENV
- name: Update Minor version
uses: hmanzur/actions-set-secret@v2.0.0
with:
name: 'MINOR'
value: ${{ env.NEW_MINOR_VERSION }}
repository: ${{ secrets.REPO }}
token: ${{ secrets.REPO_ACCESS_TOKEN }}