3455 #50
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: Deploy to VM | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
# - name: Set AWS credentials | |
# uses: aws-actions/configure-aws-credentials@v1 | |
# with: | |
# aws-access-key-id: ${{ secrets.AWS_EC2_ACCESS_KEY_ID }} | |
# aws-secret-access-key: ${{ secrets.AWS_EC2_SECRET_ACCESS_KEY }} | |
# aws-region: eu-west-2 | |
# - name: Check SSH Connection | |
# run: | | |
# ssh -i ${{secrets.SSH_PRIVATE_KEY}} -o StrictHostKeyChecking=no -o BatchMode=yes -o ConnectTimeout=10 ${{ secrets.USERNAME }}@${{ secrets.AWS_HOST }} exit | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Create tag for image | |
id: create_tag | |
run: | | |
TAG=$(date +'%Y%m%d%H%M%S') | |
echo "::set-output name=tag::$TAG" | |
- name: Build Docker image | |
run: | | |
cd server | |
docker build -t olhadanylevska/video-server:${{ steps.create_tag.outputs.tag }} . | |
cd .. | |
- name: Log in to Docker Hub | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
- name: Push Docker image to Docker Hub | |
run: | | |
docker push olhadanylevska/video-server:${{ steps.create_tag.outputs.tag }} | |
- name: Clear images and pull new image | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.AWS_HOST }} | |
username: ${{ secrets.USERNAME }} | |
key: ${{ secrets.SSH_PRIVATE_KEY }} | |
script: | | |
sudo apt-get update | |
sudo install docker -y | |
sudo systemctl start docker | |
sudo systemctl enable docker | |
docker system prune -a -f | |
docker pull olhadanylevska/video-server:${{ steps.create_tag.outputs.tag }} | |
- name: Identify and stop previous container | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.AWS_HOST }} | |
username: ${{ secrets.USERNAME }} | |
key: ${{ secrets.SSH_PRIVATE_KEY }} | |
script: | | |
CONTAINER_ID=$(docker ps -q --filter "ancestor=olhadanylevska/video-server:${{ steps.create_tag.outputs.tag }}") | |
if [ ! -z "$CONTAINER_ID" ]; then | |
docker stop $CONTAINER_ID | |
docker rm $CONTAINER_ID | |
fi | |
- name: Run container on VM server | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.AWS_HOST }} | |
username: ${{ secrets.USERNAME }} | |
key: ${{ secrets.SSH_PRIVATE_KEY }} | |
script: | | |
docker run -d \ | |
-e DB_USER=${{ secrets.RDS_DB_USER }} \ | |
-e DB_HOST=${{ secrets.RDS_DB_HOST }} \ | |
-e DB_PASSWORD=${{ secrets.RDS_DB_PASSWORD }} \ | |
-e DB_PORT=${{ secrets.RDS_DB_PORT }} \ | |
-p 80:3000 \ | |
olhadanylevska/video-server:${{ steps.create_tag.outputs.tag }} |