Merge pull request #230 from Kernel360/develop #43
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: Build and Deploy | |
on: | |
push: | |
branches: [ "main" ] | |
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: 'temurin' | |
- name: Set environment values | |
run: | | |
cd ./src/main/resources | |
touch ./.env | |
echo "${{ secrets.ENV }}" > ./.env | |
shell: bash | |
- name: Build with Gradle | |
run: ./gradlew assemble | |
- 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: Deploy to server | |
uses: appleboy/ssh-action@master | |
with: | |
host: 35.216.60.77 | |
username: km25352535 | |
key: ${{ secrets.SSH_PRIVATE_KEY }} | |
port: 22 | |
script: | | |
# Log in to Docker Hub | |
echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login -u ${{ secrets.DOCKERHUB_USERNAME }} --password-stdin | |
# Pull the latest Docker image | |
sudo docker pull ${{ secrets.DOCKERHUB_CONTAINERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY }}:latest | |
# Stop and remove the existing container if it exists | |
sudo docker ps -q -f name=speech2 | xargs -r sudo docker stop | |
sudo docker images --filter "dangling=true" -q | xargs sudo docker rmi | |
sudo docker ps -a -q -f name=speech2 | xargs -r sudo docker rm | |
# Run the new container with environment variables | |
sudo docker run -d \ | |
-p 8080:8080 \ | |
--name speech2 \ | |
${{ secrets.DOCKERHUB_CONTAINERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY }}:latest | |
# Clean up old Docker images on the server | |
sudo docker image prune -f |