fix: 도커 포트충돌 오류 해결 #12
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: Carrot CICD | |
on: | |
push: | |
branches: | |
- main #main 브랜치 push시 실행 | |
pull_request: | |
branches: | |
- main | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
#gradle caching | |
- name: Gradle Caching | |
uses: actions/cache@v3 | |
with: | |
path: | | |
~/.gradle/caches | |
~/.gradle/wrapper | |
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*gradle*','**/gradle-wrapper.properties') }} | |
restore-keys: | | |
${{ runner.os }}-gradle- | |
#gradle build | |
- name: Build with Gradle Wrapper | |
run: ./gradlew build | |
#docker login | |
- name: Docker Hub Login | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKER_USER_NAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
#docker build & push to dev | |
- name: Build Docker image | |
run: docker build -t "${{ secrets.DOCKER_USER_NAME }}/carrot:latest" . | |
- name: Push Docker image | |
run: docker push "${{ secrets.DOCKER_USER_NAME }}/carrot:latest" | |
#deploy to develop | |
- name: Deploy to Server | |
uses: appleboy/ssh-action@master | |
with: | |
host: "${{ secrets.EC2_SECRET_HOST }}" | |
username: ubuntu | |
key: "${{ secrets.EC2_SECRET_PEM }}" | |
port: 22 | |
script: | | |
EXISTING_CONTAINER_ID=$(sudo docker ps -q -f "publish=8080" -f "status=running") | |
if [ ! -z "$EXISTING_CONTAINER_ID" ]; then | |
sudo docker stop $EXISTING_CONTAINER_ID | |
sudo docker rm $EXISTING_CONTAINER_ID | |
fi | |
EXISTING_CONTAINER_ID=$(sudo docker ps -q -f "status=exited") | |
if [ ! -z "$EXISTING_CONTAINER_ID" ]; then | |
sudo docker rm $EXISTING_CONTAINER_ID | |
fi | |
sudo docker pull ${{ secrets.DOCKER_USER_NAME }}/carrot:latest | |
sudo docker run --name spring -d -p 8080:8080 -e TZ=Asia/Seoul ${{ secrets.DOCKER_USER_NAME }}/carrot:latest | |
sudo docker image prune -a -f |