Skip to content

Final check

Final check #2

name: PROD DEPLOYMENT WITH TAG
on:
push:
tags:
- 'v1.0.1'
env:
DEPLOY_TAG: ${{ github.ref_name }}
jobs:
PROD_deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code from tag
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Create .env and Dockerfile
run: |
echo "${{ secrets.DOCKER_FILE_PROD }}" > Dockerfile
- name: Build Docker image
run: |
docker build -t ${{ secrets.DOCKER_IMAGE_NAME_PROD }} .
- name: Log in to Docker Hub
env:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME_PROD }}
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD_PROD }}
run: |
echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
docker push ${{ secrets.DOCKER_IMAGE_NAME_PROD }}
- name: Deploy to Remote Server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST_NAME_PROD }}
username: ${{ secrets.USERNAME_PROD }}
key: ${{ secrets.SSH_KEY_PROD }}
port: ${{ secrets.PORT_PROD }}
script: |
TARGET_DIR="${{ secrets.TARGET_DIR_PROD }}"
ENVIRONMENT="$TARGET_DIR/.env"
if [ -f "$ENVIRONMENT" ]; then
rm "$ENVIRONMENT"
fi
mkdir -p "$TARGET_DIR"
cat << EOF > "$ENVIRONMENT"
${{ secrets.PROD_ENV }}
EOF
cd ${{ secrets.TARGET_DIR_PROD }}
docker stop ${{ secrets.CONTAINER_NAME }}
docker rm -f ${{ secrets.CONTAINER_NAME }}
docker images --no-trunc -aqf "dangling=true" | xargs docker rmi
docker-compose up -d --force-recreate --no-deps