ShiftSync/.github/workflows/api-deploy-nonprod.yml
2026-02-07 23:55:12 -05:00

132 lines
4.3 KiB
YAML

name: API Deployment Non-Production
on:
workflow_call:
inputs:
environments:
type: string
description: An optional list of environments to deploy to.
default: 'dev'
workflow_type:
type: string
description: An optional string for workflow types.
default: 'dev'
branch:
type: string
description: An optional string to define which branch to checkout.
default: 'main'
current_version:
type: string
description: Current Version of the package.json.
default: '0.0.0'
secrets:
DOCKERHUB_USER: {}
DOCKERHUB_TOKEN: {}
TEST: {}
TEAMCITY_API_ID: {}
TEAMCITY_URL: {}
TEAMCITY_USERNAME: {}
TEAMCITY_PASSWORD: {}
jobs:
check-inputs:
runs-on: 'ubuntu-latest'
environment: ${{ inputs.environments }}
steps:
- name: Check secrets present
run: |
if [[ -z "${{ secrets.TEAMCITY_API_ID }}" ]]; then
echo "TEAMCITY_API_ID secret is empty or missing"
exit 1
else
echo "TEAMCITY_API_ID secret is set"
fi
if [[ -z "${{ secrets.TEAMCITY_URL }}" ]]; then
echo "TEAMCITY_URL secret is empty or missing"
exit 1
else
echo "TEAMCITY_URL secret is set"
fi
if [[ -z "${{ secrets.TEAMCITY_USERNAME }}" ]]; then
echo "TEAMCITY_USERNAME secret is empty or missing"
exit 1
else
echo "TEAMCITY_USERNAME secret is set"
fi
if [[ -z "${{ secrets.TEAMCITY_PASSWORD }}" ]]; then
echo "TEAMCITY_PASSWORD secret is empty or missing"
exit 1
else
echo "TEAMCITY_PASSWORD secret is set"
fi
if [[ -z "${{ secrets.DOCKERHUB_USER }}" ]]; then
echo "DOCKERHUB_USER secret is empty or missing"
exit 1
else
echo "DOCKERHUB_USER secret is set"
fi
if [[ -z "${{ secrets.DOCKERHUB_TOKEN }}" ]]; then
echo "DOCKERHUB_TOKEN secret is empty or missing"
exit 1
else
echo "DOCKERHUB_TOKEN secret is set"
fi
echo "Current Version: ${{inputs.current_version}}"
build:
needs: check-inputs
if: needs.check-inputs.result == 'success' && inputs.workflow_type != 'release'
environment: ${{ inputs.environments }}
runs-on: 'ubuntu-latest'
permissions:
contents: read
packages: write
steps:
- name: Branch Checkout
uses: actions/checkout@v4
with:
ref: ${{ inputs.branch }}
- name: Login to Docker
uses: docker/login-action@v3
with:
registry: docker.io
username: ${{ secrets.DOCKERHUB_USER }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Docker Build Backend
run: docker build -f api/Dockerfile -t john4064/shiftsync:latest_api ./api --build-arg ENVIRONMENT=dev
- name: Docker Push Backend
run: docker push john4064/shiftsync:latest_api
deploy:
needs: build
if: needs.build.result == 'success' && inputs.workflow_type != 'release'
environment: ${{ inputs.environments }}
runs-on: 'ubuntu-latest'
permissions:
contents: read
packages: write
steps:
- name: Trigger TeamCity Build
id: trigger
run: |
echo "Triggering TeamCity build..."
# Trigger build and get the build ID
BUILD_ID=$(curl -s -u "$TEAMCITY_USER:$TEAMCITY_TOKEN" \
-X POST \
-H "Content-Type: application/json" \
-d "{\"buildType\": {\"id\": \"$TEAMCITY_BUILD_ID\"}}" \
"$TEAMCITY_URL/httpAuth/app/rest/buildQueue" \
| grep -oP 'id="\K[0-9]+')
echo "Build queued with ID: $BUILD_ID"
# Query only the build state
BUILD_STATE=$(curl -s -u "$TEAMCITY_USER:$TEAMCITY_TOKEN" \
"$TEAMCITY_URL/httpAuth/app/rest/buildQueue/id:$BUILD_ID?fields=build(state)" \
| grep -oP 'state="\K[^"]+')
echo "Build state: $BUILD_STATE"
# Set GitHub Actions output
echo "build_state=$BUILD_STATE" >> $GITHUB_OUTPUT
- name: Show Build State
run: echo "TeamCity build is ${{ steps.trigger.outputs.build_state }}"