Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions .github/workflows/deploy-staging-branch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
name: Deploy to staging remote server on a distinct developer branch

on:
push:
branches:
- staging/**

jobs:
get-environment-variables:
runs-on: ubuntu-latest
outputs:
staging_name: ${{ steps.set_subdomain.outputs.staging_name }}
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Retrieve subdomain from branch
id: set_subdomain
run: |
BRANCH_NAME=${GITHUB_REF#refs/heads/}
STAGING_NAME=$(echo "${BRANCH_NAME}" | sed 's/staging\///g')

echo "staging_name=${STAGING_NAME}" >> $GITHUB_OUTPUT
deploy:
name: Build and deploy to staging branch
needs:
- get-environment-variables

runs-on: ubuntu-latest
env:
STAGING_NAME: ${{needs.get-environment-variables.outputs.staging_name}}
STAGING_DIRECTORY: ${{format('{0}{1}', secrets.SSH_STAGING_ROOT_PATH, needs.get-environment-variables.outputs.staging_name)}}
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push docker images with compose
run: |
printf "\nSTAGING_NAME=${STAGING_NAME}\n" >> .default.env


docker compose -f docker-compose.prod.yml --env-file .default.env build
docker compose -f docker-compose.prod.yml --env-file .default.env push
- name: Setup SSH
run: |
mkdir -p ~/.ssh
echo "${{secrets.SSH_PRIVATE_KEY}}" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan -p ${{secrets.SSH_PORT}} ${{ secrets.SSH_REMOTE_IP}} >> ~/.ssh/known_hosts

- name: Install rsync
run: sudo apt-get install -y rsync

- name: Deploy via rsync
run: |

rsync -avz --rsync-path="mkdir -p ${STAGING_DIRECTORY} && rsync" --no-times --no-perms --no-group -e "ssh -p ${{ secrets.SSH_PORT }} -o StrictHostKeyChecking=no" docker-compose.deploy.yml .default.env ${{secrets.SSH_USER}}@${{secrets.SSH_REMOTE_IP}}:${STAGING_DIRECTORY}

- name: Rebuild containers
run: |
ssh -o StrictHostKeyChecking=no -p ${{secrets.SSH_PORT}} ${{secrets.SSH_USER}}@${{secrets.SSH_REMOTE_IP}} << EOF
cd ${{env.STAGING_DIRECTORY}}


echo "${{secrets.DOCKERHUB_TOKEN}}" | docker login -u "${{secrets.DOCKERHUB_USERNAME}}" --password-stdin
docker compose -f docker-compose.deploy.yml down --rmi all -v || true

docker compose -f docker-compose.deploy.yml --env-file .default.env build
docker compose -f docker-compose.deploy.yml --env-file .default.env up -d
docker compose -f docker-compose.deploy.yml --env-file .default.env exec products-api dotnet console/Backend.Core.Console.dll --update-db
docker compose -f docker-compose.deploy.yml --env-file .default.env exec products-api dotnet console/Backend.Core.Console.dll --fetch

docker system prune -f
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Deploy to remove server
name: Deploy to staging remove server

on:
push:
Expand Down
12 changes: 6 additions & 6 deletions docker-compose.deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ x-common-env-config: &env-config
services:
products:
<<: *env-config
image: sercudo/frontend-products-image-prod:latest
hostname: ${SERVICE_HOST:-products}
image: sercudo/frontend-products-image-prod:${STAGING_NAME:-latest}
hostname: ${STAGING_NAME:-products}
networks:
- proxy_network
- default_products_deployment_network
products-frontend-next:
<<: *env-config
image: sercudo/frontend-next-products-image-prod:latest
image: sercudo/frontend-next-products-image-prod:${STAGING_NAME:-latest}

networks:
- default_products_deployment_network
Expand All @@ -25,7 +25,7 @@ services:

products-api:
<<: *env-config
image: sercudo/api-products-image-prod:latest
image: sercudo/api-products-image-prod:${STAGING_NAME:-latest}
environment:
- ASPNETCORE_ENVIRONMENT=Production
restart: unless-stopped
Expand All @@ -35,7 +35,7 @@ services:

products-postgres:
<<: *env-config
image: sercudo/postgres-products-image-prod:latest
image: sercudo/postgres-products-image-prod:${STAGING_NAME:-latest}
networks:
- default_products_deployment_network

Expand All @@ -44,6 +44,6 @@ services:

networks:
default_products_deployment_network:
name: products_deployment_network
name: ${STAGING_NAME:-products_deployment_network}
proxy_network:
external: true
Loading