Skip to content

Commit

Permalink
fix: #dev-deploy tag name fix
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinbator authored Dec 3, 2024
1 parent f0106cc commit abbb91c
Showing 1 changed file with 159 additions and 164 deletions.
323 changes: 159 additions & 164 deletions .github/workflows/dev.kodemy.deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,180 +36,163 @@ env:
JWT_KEY: ${{ secrets.JWT_KEY }}

jobs:
prepare:
name: Prepare
runs-on: self-hosted
environment: dev
steps:
- name: Check environments
run: |
REQUIRED_VARS=\
(
"WORKING_DIRECTORY"
"NETWORK_NAME"
"DATASOURCE_CONTAINER"
"DATASOURCE_USERNAME"
"DATASOURCE_PASSWORD"
"DATASOURCE_DB"
"RABBITMQ_HOST"
"RABBITMQ_PORT"
"RABBITMQ_USERNAME"
"RABBITMQ_PASSWORD"
"RABBITMQ_PASSWORD"
"ELASTICSEARCH_USERNAME"
"ELASTICSEARCH_PASSWORD"
"EUREKA_URL"
"FRONTEND_PUBLIC_HOST"
"CORS_ALLOWED_ORIGINS"
"GATEWAY_PUBLIC_HOST"
"OAUTH_GITHUB_ID"
"OAUTH_GITHUB_KEY"
"JWT_KEY"
"REDIS_HOST"
"REDIS_PORT"
)
EXIT=0
for VAR in "${REQUIRED_VARS[@]}"; do
[ -z "${!VAR}" ] && echo "Environment variable $VAR is missing." && EXIT=1
done
exit $EXIT
update-branch:
name: Update branch
needs: [ prepare ]
runs-on: self-hosted
environment: dev
steps:
- name: Update ${{ github.ref_name }}
uses: appleboy/ssh-action@master
env:
BRANCH: ${{ github.ref_name }}
with:
host: ${{ secrets.REMOTE_HOST }}
username: ${{ secrets.REMOTE_USER }}
port: ${{ secrets.REMOTE_PORT }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
envs: WORKING_DIRECTORY,BRANCH
script: |
cd $WORKING_DIRECTORY
git checkout $BRANCH || exit 1
git pull origin $BRANCH || exit 1
validate:
name: Validate services
runs-on: self-hosted
needs: [ update-branch ]
environment: dev
steps:
- name: Validate services
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.REMOTE_HOST }}
username: ${{ secrets.REMOTE_USER }}
port: ${{ secrets.REMOTE_PORT }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
envs: WORKING_DIRECTORY,NETWORK_NAME,DATASOURCE_CONTAINER,DATASOURCE_USERNAME,DATASOURCE_DB
script: |
echo "Validate network"
docker network inspect $NETWORK_NAME > /dev/null 2>&1 \
|| { echo "Network $NETWORK_NAME is not available."; exit 1; }
# prepare:
# name: Prepare
# runs-on: self-hosted
# environment: dev
# steps:
# - name: Check environments
# run: |
# REQUIRED_VARS=\
# (
# "WORKING_DIRECTORY"
# "NETWORK_NAME"
# "DATASOURCE_CONTAINER"
# "DATASOURCE_USERNAME"
# "DATASOURCE_PASSWORD"
# "DATASOURCE_DB"
# "RABBITMQ_HOST"
# "RABBITMQ_PORT"
# "RABBITMQ_USERNAME"
# "RABBITMQ_PASSWORD"
# "RABBITMQ_PASSWORD"
# "ELASTICSEARCH_USERNAME"
# "ELASTICSEARCH_PASSWORD"
# "EUREKA_URL"
# "FRONTEND_PUBLIC_HOST"
# "CORS_ALLOWED_ORIGINS"
# "GATEWAY_PUBLIC_HOST"
# "OAUTH_GITHUB_ID"
# "OAUTH_GITHUB_KEY"
# "JWT_KEY"
# "REDIS_HOST"
# "REDIS_PORT"
# )
# EXIT=0
# for VAR in "${REQUIRED_VARS[@]}"; do
# [ -z "${!VAR}" ] && echo "Environment variable $VAR is missing." && EXIT=1
# done
# exit $EXIT
# update-branch:
# name: Update branch
# needs: [ prepare ]
# runs-on: self-hosted
# environment: dev
# steps:
# - name: Update ${{ github.ref_name }}
# uses: appleboy/ssh-action@master
# env:
# BRANCH: ${{ github.ref_name }}
# with:
# host: ${{ secrets.REMOTE_HOST }}
# username: ${{ secrets.REMOTE_USER }}
# port: ${{ secrets.REMOTE_PORT }}
# key: ${{ secrets.SSH_PRIVATE_KEY }}
# envs: WORKING_DIRECTORY,BRANCH
# script: |
# cd $WORKING_DIRECTORY
# git checkout $BRANCH || exit 1
# git pull origin $BRANCH || exit 1
# validate:
# name: Validate services
# runs-on: self-hosted
# needs: [ update-branch ]
# environment: dev
# steps:
# - name: Validate services
# uses: appleboy/ssh-action@master
# with:
# host: ${{ secrets.REMOTE_HOST }}
# username: ${{ secrets.REMOTE_USER }}
# port: ${{ secrets.REMOTE_PORT }}
# key: ${{ secrets.SSH_PRIVATE_KEY }}
# envs: WORKING_DIRECTORY,NETWORK_NAME,DATASOURCE_CONTAINER,DATASOURCE_USERNAME,DATASOURCE_DB
# script: |
# echo "Validate network"
# docker network inspect $NETWORK_NAME > /dev/null 2>&1 \
# || { echo "Network $NETWORK_NAME is not available."; exit 1; }

echo "Validate database - container"
docker exec -t $DATASOURCE_CONTAINER pg_isready \
|| { echo "PostgreSQL is not running."; exit 1; }
# echo "Validate database - container"
# docker exec -t $DATASOURCE_CONTAINER pg_isready \
# || { echo "PostgreSQL is not running."; exit 1; }

echo "Validate database - database"
docker exec -t $DATASOURCE_CONTAINER psql -lqt -U $DATASOURCE_USERNAME | grep -q $DATASOURCE_DB \
|| { echo "Database $DATASOURCE_DB does not exist."; exit 1; }
# echo "Validate database - database"
# docker exec -t $DATASOURCE_CONTAINER psql -lqt -U $DATASOURCE_USERNAME | grep -q $DATASOURCE_DB \
# || { echo "Database $DATASOURCE_DB does not exist."; exit 1; }

echo "Validate database - schemas"
DATASOURCE_SCHEMAS=("kodemy-auth" "kodemy-backend" "kodemy-notification")
for SCHEMA in "${DATASOURCE_SCHEMAS[@]}"
do
docker exec -t $DATASOURCE_CONTAINER psql \
-U $DATASOURCE_USERNAME \
-d $DATASOURCE_DB \
-tAc "SELECT 1 FROM information_schema.schemata WHERE schema_name = '$SCHEMA'" \
| grep -q 1 || { echo "Schema $SCHEMA does not exist."; exit 1; }
done
# echo "Validate database - schemas"
# DATASOURCE_SCHEMAS=("kodemy-auth" "kodemy-backend" "kodemy-notification")
# for SCHEMA in "${DATASOURCE_SCHEMAS[@]}"
# do
# docker exec -t $DATASOURCE_CONTAINER psql \
# -U $DATASOURCE_USERNAME \
# -d $DATASOURCE_DB \
# -tAc "SELECT 1 FROM information_schema.schemata WHERE schema_name = '$SCHEMA'" \
# | grep -q 1 || { echo "Schema $SCHEMA does not exist."; exit 1; }
# done

build-and-deploy:
name: Build, Test, Deploy
runs-on: self-hosted
needs: [ validate ]
environment: dev
steps:
- name: Build, Test, Deploy
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.REMOTE_HOST }}
username: ${{ secrets.REMOTE_USER }}
port: ${{ secrets.REMOTE_PORT }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
envs: |
WORKING_DIRECTORY,
DATASOURCE_CONTAINER,DATASOURCE_USERNAME,DATASOURCE_PASSWORD,DATASOURCE_DB,
RABBITMQ_HOST,RABBITMQ_PORT,RABBITMQ_USERNAME,RABBITMQ_PASSWORD,
ELASTICSEARCH_HOSTS,ELASTICSEARCH_USERNAME,ELASTICSEARCH_PASSWORD,
EUREKA_URL,
FRONTEND_PUBLIC_HOST,CORS_ALLOWED_ORIGINS,
GATEWAY_PUBLIC_HOST,
OAUTH_GITHUB_ID,OAUTH_GITHUB_KEY,
JWT_KEY,
REDIS_HOST,REDIS_PORT
script: |
# build-and-deploy:
# name: Build, Test, Deploy
# runs-on: self-hosted
# needs: [ validate ]
# environment: dev
# steps:
# - name: Build, Test, Deploy
# uses: appleboy/ssh-action@master
# with:
# host: ${{ secrets.REMOTE_HOST }}
# username: ${{ secrets.REMOTE_USER }}
# port: ${{ secrets.REMOTE_PORT }}
# key: ${{ secrets.SSH_PRIVATE_KEY }}
# envs: |
# WORKING_DIRECTORY,
# DATASOURCE_CONTAINER,DATASOURCE_USERNAME,DATASOURCE_PASSWORD,DATASOURCE_DB,
# RABBITMQ_HOST,RABBITMQ_PORT,RABBITMQ_USERNAME,RABBITMQ_PASSWORD,
# ELASTICSEARCH_HOSTS,ELASTICSEARCH_USERNAME,ELASTICSEARCH_PASSWORD,
# EUREKA_URL,
# FRONTEND_PUBLIC_HOST,CORS_ALLOWED_ORIGINS,
# GATEWAY_PUBLIC_HOST,
# OAUTH_GITHUB_ID,OAUTH_GITHUB_KEY,
# JWT_KEY,
# REDIS_HOST,REDIS_PORT
# script: |

cd $WORKING_DIRECTORY/commons
./gradlew clean assemble
# cd $WORKING_DIRECTORY/commons
# ./gradlew clean assemble

cd $WORKING_DIRECTORY/kodemy-api-gateway
./gradlew clean assemble -x test
cd $WORKING_DIRECTORY/kodemy-auth
./gradlew clean assemble -x test
cd $WORKING_DIRECTORY/kodemy-backend
./gradlew clean assemble -x test
cd $WORKING_DIRECTORY/kodemy-notification
./gradlew clean assemble -x test
cd $WORKING_DIRECTORY/kodemy-search
./gradlew clean assemble -x test
cd $WORKING_DIRECTORY/kodemy-service-registry
./gradlew clean assemble -x test
# cd $WORKING_DIRECTORY/kodemy-api-gateway
# ./gradlew clean assemble -x test
# cd $WORKING_DIRECTORY/kodemy-auth
# ./gradlew clean assemble -x test
# cd $WORKING_DIRECTORY/kodemy-backend
# ./gradlew clean assemble -x test
# cd $WORKING_DIRECTORY/kodemy-notification
# ./gradlew clean assemble -x test
# cd $WORKING_DIRECTORY/kodemy-search
# ./gradlew clean assemble -x test
# cd $WORKING_DIRECTORY/kodemy-service-registry
# ./gradlew clean assemble -x test

export DATASOURCE_URL=jdbc:postgresql://$DATASOURCE_CONTAINER/$DATASOURCE_DB
# export DATASOURCE_URL=jdbc:postgresql://$DATASOURCE_CONTAINER/$DATASOURCE_DB

cd $WORKING_DIRECTORY
docker compose -f docker-compose.app.yml -f docker-compose.app.expose.yml up --build -d
- name: Cleanup
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.REMOTE_HOST }}
username: ${{ secrets.REMOTE_USER }}
port: ${{ secrets.REMOTE_PORT }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
docker system prune -f
# cd $WORKING_DIRECTORY
# docker compose -f docker-compose.app.yml -f docker-compose.app.expose.yml up --build -d
# - name: Cleanup
# uses: appleboy/ssh-action@master
# with:
# host: ${{ secrets.REMOTE_HOST }}
# username: ${{ secrets.REMOTE_USER }}
# port: ${{ secrets.REMOTE_PORT }}
# key: ${{ secrets.SSH_PRIVATE_KEY }}
# script: |
# docker system prune -f

create-tag:
name: Create tag
runs-on: self-hosted
environment: dev
needs: [ build-and-deploy ]
steps:
- name: Determine new tag
id: tag
run: |
tags=$(git tag -l)
latest_tag=$(echo "$tags" | grep -E '^0\.0\.[0-9]+$' | sort -V | tail -n 1)
if [ -n "$latest_tag" ]; then
version_number=$(echo "$latest_tag" | sed 's/^0\.0\.//')
new_version=$((version_number + 1))
new_tag="0.0.$new_version"
else
new_tag="0.0.1"
fi
echo "New tag: $new_tag"
echo "TAG_NAME=$new_tag" >> $GITHUB_ENV

# needs: [ build-and-deploy ]
steps:
- name: Create tag
uses: appleboy/ssh-action@master
with:
Expand All @@ -218,8 +201,20 @@ jobs:
port: ${{ secrets.REMOTE_PORT }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
envs: |
WORKING_DIRECTORY,TAG_NAME
WORKING_DIRECTORY
script: |
cd $WORKING_DIRECTORY
git tag $TAG_NAME
tags=$(git tag -l)
latest_tag=$(echo "$tags" | grep -E '^0\.0\.[0-9]+$' | sort -V | tail -n 1)
if [ -n "$latest_tag" ]; then
version_number=$(echo "$latest_tag" | sed 's/^0\.0\.//')
new_version=$((version_number + 1))
new_tag="0.0.$new_version"
else
new_tag="0.0.1"
fi
echo "New tag: $new_tag"

git tag $new_tag
git push origin --tags

0 comments on commit abbb91c

Please sign in to comment.