Skip to content

Deploy on Server

Deploy on Server #4

name: Deploy on Server
on:
workflow_dispatch:
workflow_run:
workflows: ["Build and Push Docker image"]
branches: [main]
types:
- completed
jobs:
deploy:
environment: Backend production
runs-on: self-hosted
steps:
- name: Login to Docker Hub
run: docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
env:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
- name: Remove all containers using the old image
run: |
docker container ls -q --filter ancestor=oriolegea/passager-backend:latest | xargs -r docker container stop
docker container ls -aq --filter ancestor=oriolegea/passager-backend:latest | xargs -r docker container rm
- name: Remove old images
run: |
docker image prune -af --filter "label=oriolegea/passager-backend"
- name: Pull latest Docker image
run: docker pull oriolegea/passager-backend:latest
- name: Calculate ports
run: |
echo "PORT_RANGE=${{ secrets.AUTH_PORT }}-${{ secrets.DOCS_PORT }}" >> $GITHUB_ENV
- name: Run container with the latest image
run: '
docker run -d --restart=always
--env AUTH_ALLOWED_DOMAINS="${{ secrets.AUTH_ALLOWED_DOMAINS }}"
--env AUTH_ALLOWED_EMAILS="${{ secrets.AUTH_ALLOWED_EMAILS }}"
--env AUTH_COLLECTION="${{ secrets.AUTH_COLLECTION }}"
--env AUTH_LIMIT_ACCESS_BY_EMAIL="${{ secrets.AUTH_LIMIT_ACCESS_BY_EMAIL }}"
--env AUTH_PORT="${{ secrets.AUTH_PORT }}"
--env AUTH_PUBLIC_URL="${{ secrets.AUTH_PUBLIC_URL }}"
--env AUTH_SMTP_HOST="${{ secrets.AUTH_SMTP_HOST }}"
--env AUTH_SMTP_PASSWORD="${{ secrets.AUTH_SMTP_PASSWORD }}"
--env AUTH_SMTP_PORT="${{ secrets.AUTH_SMTP_PORT }}"
--env AUTH_SMTP_SENDER="${{ secrets.AUTH_SMTP_SENDER }}"
--env AUTH_SMTP_USER="${{ secrets.AUTH_SMTP_USER }}"
--env COMMON_APP_NAME="${{ secrets.COMMON_APP_NAME }}"
--env COMMON_MONGODB_CONNECTION_STRING="${{ secrets.COMMON_MONGODB_CONNECTION_STRING }}"
--env COMMON_MONGODB_DATABASE="${{ secrets.COMMON_MONGODB_DATABASE }}"
--env COMMON_ORGANIZATION_NAME="${{ secrets.COMMON_ORGANIZATION_NAME }}"
--env DOCS_PORT="${{ secrets.DOCS_PORT }}"
--env DOCS_PUBLIC_URL="${{ secrets.DOCS_PUBLIC_URL }}"
-p ${{ env.PORT_RANGE }}:${{ env.PORT_RANGE }}
oriolegea/passager-backend:latest'