Skip to content

Build and deploy

Build and deploy #51

Workflow file for this run

name: CI/CD Docker
env:
RESOURCE_PATH: ./src/main/resources/application.properties
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 17
uses: actions/setup-java@v2
with:
distribution: 'adopt'
java-version: '17'
- name: Fill up properties from github
uses: microsoft/variable-substitution@v1
with:
files: ${{ env.RESOURCE_PATH }}
env:
spring.jwt.secret: ${{ secrets.JWT_SECRET_KEY }}
spring.datasource.url: ${{ secrets.MYSQL_URL }}
spring.datasource.username: ${{ secrets.MYSQL_USERNAME }}
spring.datasource.password: ${{ secrets.MYSQL_PASSWORD }}
- name: Build with Gradle
run: ./gradlew build
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Log in to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and push Docker image with Buildx
run: docker buildx build --push --platform linux/amd64 -t ${{ 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