Skip to content

Merge pull request #270 from sivertschou/oppdater-dependencies #30

Merge pull request #270 from sivertschou/oppdater-dependencies

Merge pull request #270 from sivertschou/oppdater-dependencies #30

name: Build and deploy dev
on:
push:
branches: [main]
env:
tag: dev
jobs:
build:
name: Build, tag and push image
runs-on: ubuntu-latest
outputs:
version: ${{ steps.gitversion.outputs.semVer }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install GitVersion
uses: gittools/actions/gitversion/setup@v0
with:
versionSpec: '5.x'
- name: Determine Version
id: gitversion
uses: gittools/actions/gitversion/execute@v0
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ vars.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build, tag and push backend
uses: docker/build-push-action@v5
with:
file: ./apps/backend/Dockerfile
tags: ${{ vars.DOCKERHUB_USERNAME }}/dundring-backend:${{ steps.gitversion.outputs.semVer }}
push: true
- name: Build, tag and push frontend
uses: docker/build-push-action@v5
with:
file: ./apps/frontend/Dockerfile
push: true
tags: ${{ vars.DOCKERHUB_USERNAME }}/dundring-frontend:${{ steps.gitversion.outputs.semVer }}
- name: Set version tag
run: |
git tag ${{ steps.gitversion.outputs.semVer }}
git push origin ${{ steps.gitversion.outputs.semVer }}
deploy-dev:
name: Deploy dev
runs-on: ubuntu-latest
needs: build
environment: dev
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Add docker compose file to server
uses: appleboy/[email protected]
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_KEY }}
source: docker/dev/docker-compose.yaml
target: dev
strip_components: 2
- name: Start the containers with Docker Compose
uses: appleboy/[email protected]
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_KEY }}
script: |
cd dev
# Find currently running version
TAG_BLUE=$(docker inspect --format='{{ index .Config.Image }}' backend-dev-blue 2>/dev/null | cut -d ':' -f 2)
TAG_GREEN=${{ needs.build.outputs.version }} \
TAG_BLUE=${TAG_BLUE:-${{ needs.build.outputs.version }}} \
CLOUDFLARE_DNS_API_TOKEN=${{ secrets.CLOUDFLARE_API_TOKEN }} \
DATABASE_URL=${{ secrets.DATABASE_URL }} \
REDIS_URL=${{ secrets.REDIS_URL }} \
MAIL_HOST=${{ secrets.MAIL_HOST }} \
MAIL_PORT=${{ secrets.MAIL_PORT }} \
MAIL_USER=${{ secrets.MAIL_USER }} \
MAIL_PASSWORD='${{ secrets.MAIL_PASSWORD }}' \
TOKEN_SECRET=${{ secrets.TOKEN_SECRET }} \
docker-compose up --detach
echo "Give green pod 10 seconds to (re)start before starting health checking"
sleep 10
# Wait until green is healty
url="http://$(docker inspect --format '{{index .NetworkSettings.Networks "dundring-dev" "IPAddress"}}' backend-dev-green):8080/api/health"
http_status=""
counter=0
max_retries=30
echo "Check health at $url"
while [ "$http_status" != "200" ]; do
# Increment the counter
((counter++))
# Make the HTTP request and capture the HTTP status code
http_status=$(curl -s -o /dev/null -w "%{http_code}" "$url")
if [ "$http_status" == "200" ]; then
echo "Health check successful! HTTP Status: $http_status"
else
echo "Health check failed. HTTP Status: $http_status. Retry $counter of $max_retries. Retrying in 5 seconds..."
# Check if the counter has reached the maximum retries
if [ "$counter" -ge "$max_retries" ]; then
echo "Maximum retries reached. Exiting..."
exit 1
fi
sleep 5
fi
done
# Start blue with updated version
TAG_GREEN=${{ needs.build.outputs.version }} \
TAG_BLUE=${{ needs.build.outputs.version }} \
CLOUDFLARE_DNS_API_TOKEN=${{ secrets.CLOUDFLARE_API_TOKEN }} \
DATABASE_URL=${{ secrets.DATABASE_URL }} \
REDIS_URL=${{ secrets.REDIS_URL }} \
MAIL_HOST=${{ secrets.MAIL_HOST }} \
MAIL_PORT=${{ secrets.MAIL_PORT }} \
MAIL_USER=${{ secrets.MAIL_USER }} \
MAIL_PASSWORD='${{ secrets.MAIL_PASSWORD }}' \
TOKEN_SECRET=${{ secrets.TOKEN_SECRET }} \
docker-compose up --detach
echo "Give blue pod 10 seconds to (re)start before starting health checking"
sleep 10
# Wait until blue is healty
url="http://$(docker inspect --format '{{index .NetworkSettings.Networks "dundring-dev" "IPAddress"}}' backend-dev-blue):8080/api/health"
http_status=""
counter=0
max_retries=30
echo "Check health at $url"
while [ "$http_status" != "200" ]; do
# Increment the counter
((counter++))
# Make the HTTP request and capture the HTTP status code
http_status=$(curl -s -o /dev/null -w "%{http_code}" "$url")
if [ "$http_status" == "200" ]; then
echo "Health check successful! HTTP Status: $http_status"
else
echo "Health check failed. HTTP Status: $http_status. Retry $counter of $max_retries. Retrying in 5 seconds..."
# Check if the counter has reached the maximum retries
if [ "$counter" -ge "$max_retries" ]; then
echo "Maximum retries reached. Exiting..."
exit 1
fi
sleep 5
fi
done
deploy-prod:
name: Deploy prod
runs-on: ubuntu-latest
needs: build
environment: prod
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Add docker compose file to server
uses: appleboy/[email protected]
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_KEY }}
source: docker/prod/docker-compose.yaml
target: prod
strip_components: 2
- name: Start the containers with Docker Compose
uses: appleboy/[email protected]
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_KEY }}
script: |
cd prod
# Find currently running version
TAG_BLUE=$(docker inspect --format='{{ index .Config.Image }}' backend-prod-blue 2>/dev/null | cut -d ':' -f 2)
TAG_GREEN=${{ needs.build.outputs.version }} \
TAG_BLUE=${TAG_BLUE:-${{ needs.build.outputs.version }}} \
CLOUDFLARE_DNS_API_TOKEN=${{ secrets.CLOUDFLARE_API_TOKEN }} \
DATABASE_URL=${{ secrets.DATABASE_URL }} \
REDIS_URL=${{ secrets.REDIS_URL }} \
MAIL_HOST=${{ secrets.MAIL_HOST }} \
MAIL_PORT=${{ secrets.MAIL_PORT }} \
MAIL_USER=${{ secrets.MAIL_USER }} \
MAIL_PASSWORD='${{ secrets.MAIL_PASSWORD }}' \
TOKEN_SECRET=${{ secrets.TOKEN_SECRET }} \
SLACK_TOKEN=${{ secrets.SLACK_TOKEN }} \
docker-compose up --detach
echo "Give green pod 10 seconds to (re)start before starting health checking"
sleep 10
# Wait until green is healty
url="http://$(docker inspect --format '{{index .NetworkSettings.Networks "dundring-prod" "IPAddress"}}' backend-prod-green):8080/api/health"
http_status=""
counter=0
max_retries=30
echo "Check health at $url"
while [ "$http_status" != "200" ]; do
# Increment the counter
((counter++))
# Make the HTTP request and capture the HTTP status code
http_status=$(curl -s -o /dev/null -w "%{http_code}" "$url")
if [ "$http_status" == "200" ]; then
echo "Health check successful! HTTP Status: $http_status"
else
echo "Health check failed. HTTP Status: $http_status. Retry $counter of $max_retries. Retrying in 5 seconds..."
# Check if the counter has reached the maximum retries
if [ "$counter" -ge "$max_retries" ]; then
echo "Maximum retries reached. Exiting..."
exit 1
fi
sleep 5
fi
done
# Start blue with updated version
TAG_GREEN=${{ needs.build.outputs.version }} \
TAG_BLUE=${{ needs.build.outputs.version }} \
CLOUDFLARE_DNS_API_TOKEN=${{ secrets.CLOUDFLARE_API_TOKEN }} \
DATABASE_URL=${{ secrets.DATABASE_URL }} \
REDIS_URL=${{ secrets.REDIS_URL }} \
MAIL_HOST=${{ secrets.MAIL_HOST }} \
MAIL_PORT=${{ secrets.MAIL_PORT }} \
MAIL_USER=${{ secrets.MAIL_USER }} \
MAIL_PASSWORD='${{ secrets.MAIL_PASSWORD }}' \
TOKEN_SECRET=${{ secrets.TOKEN_SECRET }} \
SLACK_TOKEN=${{ secrets.SLACK_TOKEN }} \
docker-compose up --detach
echo "Give blue pod 10 seconds to (re)start before starting health checking"
sleep 10
# Wait until blue is healty
url="http://$(docker inspect --format '{{index .NetworkSettings.Networks "dundring-prod" "IPAddress"}}' backend-prod-blue):8080/api/health"
http_status=""
counter=0
max_retries=30
echo "Check health at $url"
while [ "$http_status" != "200" ]; do
# Increment the counter
((counter++))
# Make the HTTP request and capture the HTTP status code
http_status=$(curl -s -o /dev/null -w "%{http_code}" "$url")
if [ "$http_status" == "200" ]; then
echo "Health check successful! HTTP Status: $http_status"
else
echo "Health check failed. HTTP Status: $http_status. Retry $counter of $max_retries. Retrying in 5 seconds..."
# Check if the counter has reached the maximum retries
if [ "$counter" -ge "$max_retries" ]; then
echo "Maximum retries reached. Exiting..."
exit 1
fi
sleep 5
fi
done