Skip to content

Running

Running #58

Workflow file for this run

name: CICD Test
run-name: Running
on:
push:
branches:
- dis
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'corretto'
- name: Set environment values
run: |
mkdir -p ./src/main/resources
echo "${{ secrets.ENV }}" > ./src/main/resources/.env
shell: bash
- name: Build with Gradle
run: gradle clean build bootjar -x test
- name: Build and push Docker image
run: |
# Log in to Docker Hub
echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login -u ${{ secrets.DOCKERHUB_USERNAME }} --password-stdin
# Build the Docker image with the 'latest' tag
docker build -t ${{ secrets.DOCKERHUB_CONTAINERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY }} .
# Push the Docker image to Docker Hub
docker push ${{ secrets.DOCKERHUB_CONTAINERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY }}:latest
- name: Clean up dangling Docker images locally
run: |
# Clean up any dangling images after the build
sudo docker image prune -f
- name: Test SSH connection
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.PUBLIC_DNS }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: 22
script: |
echo "Testing SSH connection"
uptime
- name: Deploy to EC2 Server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.PUBLIC_DNS }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: 22
script: |
echo "Docker Hub에 로그인"
echo "${{ secrets.DOCKERHUB_PASSWORD }}" | sudo docker login -u ${{ secrets.DOCKERHUB_USERNAME }} --password-stdin
echo "최신 Docker 이미지 Pull"
sudo docker pull ${{ secrets.DOCKERHUB_CONTAINERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY }}:latest
echo "기존 컨테이너가 있을 경우 중지 및 제거"
sudo docker ps -q -f name=${{ secrets.DOCKERHUB_CONTAINERNAME }} | xargs -r sudo docker stop
sudo docker ps -a -q -f name=${{ secrets.DOCKERHUB_CONTAINERNAME }} | xargs -r sudo docker rm
sudo docker run -d \
-p 8080:8080 \
--name ${{ secrets.DOCKERHUB_CONTAINERNAME }} \
${{ secrets.DOCKERHUB_CONTAINERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY }}:latest
echo "오래된 Docker 이미지 정리"
sudo docker image prune -f