Running #41
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: CICD Test | |
run-name: Running | |
on: | |
push: | |
branches: | |
- deploy | |
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: | | |
cd ./src/main/resources | |
touch ./.env | |
echo "${{ secrets.ENV }}" > ./.env.properties | |
shell: bash | |
- name: Load environment variables and run the application | |
run: | | |
export $(cat ./src/main/resources/.env | xargs) | |
- 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 | |
echo "환경 변수를 사용하여 새로운 컨테이너 실행" | |
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 |