Update deploy-image.yml #25
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: Checkout code | |
uses: actions/checkout@v2 | |
- name: Create Environment File | |
run: | | |
echo "DB_HOST=${{ secrets.RDS_DB_HOST }}" > video.env | |
echo "DB_USER=${{ secrets.RDS_DB_USER }}" >> video.env | |
echo "DB_PASSWORD=${{ secrets.RDS_DB_PASSWORD }}" >> video.env | |
echo "DB_PORT=${{ secrets.RDS_DB_NAME }}" >> video.env | |
- name: Display Environment File | |
run: | | |
mkdir -p /tmp/context | |
cp video.env /tmp/context | |
- name: Create tag | |
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: clean images | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.AWS_HOST }} | |
username: ${{ secrets.USERNAME }} | |
key: ${{ secrets.SSH_PRIVATE_KEY }} | |
script: docker system prune -a -f | |
- name: Pull image | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.AWS_HOST }} | |
username: ${{ secrets.USERNAME }} | |
key: ${{ secrets.SSH_PRIVATE_KEY }} | |
script: | | |
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) | |
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 --env-file /tmp/context/video.env -p 80:3000 olhadanylevska/video-server:${{ steps.create_tag.outputs.tag }} |