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
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Deploy to VM | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.AWS_HOST }} | |
username: ${{ secrets.USERNAME }} | |
key: ${{ secrets.SSH_PRIVATE_KEY }} | |
script: | | |
sudo apt-get update | |
sudo apt-get install -y docker.io | |
- 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: | | |
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 }} |