Skip to content

Merge pull request #23 from dongkyun0713/feature/docker #5

Merge pull request #23 from dongkyun0713/feature/docker

Merge pull request #23 from dongkyun0713/feature/docker #5

Workflow file for this run

name: Deploy to AWS EC2 using Docker
on:
push:
branches:
- develop
env:
DOCKER_IMAGE_NAME: dongkyun0713/kkeujeok_repository
EC2_HOST: ${{ secrets.EC2_HOST }}
EC2_SSH_USER: ec2-user
PRIVATE_KEY: ${{ secrets.EC2_SSH_PRIVATE_KEY }}
jobs:
build-and-push-docker:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
- name: Set up application.yml
run: echo "${{ secrets.APPLICATION }}" > ./src/main/resources/application.yml
- name: Build with Gradle
run: ./gradlew build -x test
- name: Build the Docker image
run: docker build . --file Dockerfile --tag ${{ env.DOCKER_IMAGE_NAME }}:latest
- name: Login to Docker Hub using Access Token
run: echo "${{ secrets.DOCKER_HUB_TOKEN }}" | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
- name: Push the Docker image
run: docker push ${{ env.DOCKER_IMAGE_NAME }}:latest
deploy-to-ec2:
needs: build-and-push-docker
runs-on: ubuntu-latest
steps:
- name: Deploy to EC2
uses: appleboy/ssh-action@master
with:
host: ${{ env.EC2_HOST }}
username: ${{ env.EC2_SSH_USER }}
key: ${{ env.PRIVATE_KEY }}
script: |
CONTAINER_ID=$(sudo docker ps -q --filter "publish=80-8080")
if [ ! -z "$CONTAINER_ID" ]; then
sudo docker stop $CONTAINER_ID
sudo docker rm $CONTAINER_ID
fi
sudo docker pull ${{ env.DOCKER_HUB }}
sudo docker run --name ${{ env.CONTAINER_NAME }} -d -p 80:8080 -e TZ=Asia/Seoul ${{ env.DOCKER_HUB }}