Update main.yml #10
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: Docker Build and Deploy | |
on: | |
push: | |
branches: | |
- main | |
workflow_dispatch: | |
jobs: | |
build_and_deploy: | |
name: Build and Deploy | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v2 | |
- name: SSH into Server, Pull Changes, and Build Docker Image | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.SSH_HOST }} | |
username: ${{ secrets.SSH_USER }} | |
key: ${{ secrets.SSH_PRIVATE_KEY }} | |
script: | | |
cd ${{ secrets.WORK_DIR }}/kemistry | |
git pull origin main || { echo "Failed to pull changes"; exit 1; } | |
docker build -t kemistry . | |
docker tag kemistry kevinkoech357/kemistry | |
- name: Docker Login | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
- name: Push Docker Image | |
run: docker push kevinkoech357/kemistry || { echo "Failed to push Docker image"; exit 1; } | |
- name: SSH into Server and Deploy | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.SSH_HOST }} | |
username: ${{ secrets.SSH_USER }} | |
key: ${{ secrets.SSH_PRIVATE_KEY }} | |
script: | | |
docker pull kevinkoech357/kemistry || { echo "Failed to pull Docker image"; exit 1; } | |
docker stop kemistry_container || true | |
docker rm kemistry_container || true | |
docker run -d -p 8007:8007 --name kemistry_container kevinkoech357/kemistry || { echo "Failed to run Docker container"; exit 1; } | |
- name: Restart Nginx | |
run: | | |
ssh ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} "sudo service nginx restart" || { echo "Failed to restart Nginx"; exit 1; } | |
- name: Cleanup | |
run: rm -f ~/.ssh/id_rsa ~/.ssh/known_hosts || { echo "Failed to cleanup SSH keys"; exit 1; } |