Skip to content

readme-for-app-demo-gif #44

readme-for-app-demo-gif

readme-for-app-demo-gif #44

Workflow file for this run

name: CI/CD Docker
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up JDK 17
uses: actions/setup-java@v2
with:
distribution: 'adopt'
java-version: '17'
- name: Build with Gradle
run: ./gradlew build
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Build and push Docker image
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: ${{ secrets.DOCKER_IMAGE }}:latest
- name: Deploy to Instance
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.AWS_HOST }}
username: ${{ secrets.AWS_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
sudo docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
sudo docker pull ${{ secrets.DOCKER_IMAGE }}:latest
EXISTING_CONTAINER=$(sudo docker ps --filter "publish=8080" -q)
if [ ! -z "$EXISTING_CONTAINER" ]; then
echo "Stopping and removing existing container on port 8080"
sudo docker stop $EXISTING_CONTAINER
sudo docker rm $EXISTING_CONTAINER
fi
sudo docker rm -f ${{ secrets.CONTAINER_NAME }} || true
sudo docker run -d -p 8080:8080 --name ${{ secrets.CONTAINER_NAME }} \
-e MYSQL_URL=${{ secrets.MYSQL_URL }} \
-e MYSQL_USERNAME=${{ secrets.MYSQL_USERNAME }} \
-e MYSQL_PASSWORD=${{ secrets.MYSQL_PASSWORD }} \
${{ secrets.DOCKER_IMAGE }}:latest
sudo docker system prune -f