Files
calminer/.gitea/workflows/deploy.yml

100 lines
3.8 KiB
YAML

name: Deploy to Server
on:
workflow_run:
workflows:
- Build and Push Docker Image
types:
- completed
jobs:
deploy:
if: ${{ github.event_name != 'workflow_run' || (
github.event.workflow_run.conclusion == 'success' && (
github.event.workflow_run.head_branch == 'main' ||
github.event.workflow_run.head_branch == 'refs/heads/main' ||
(
!github.event.workflow_run.head_branch && (
github.event.workflow_run.repository.default_branch == 'main' ||
github.event.workflow_run.repository.default_branch == 'refs/heads/main' ||
github.event.repository.default_branch == 'main' ||
github.event.repository.default_branch == 'refs/heads/main'
)
)
)
) }}
runs-on: ubuntu-latest
env:
DEFAULT_BRANCH: main
REGISTRY_ORG: allucanget
REGISTRY_IMAGE_NAME: calminer
REGISTRY_URL: ${{ secrets.REGISTRY_URL }}
REGISTRY_USERNAME: ${{ secrets.REGISTRY_USERNAME }}
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }}
WORKFLOW_RUN_HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }}
WORKFLOW_RUN_HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
WORKFLOW_RUN_REPO_DEFAULT_BRANCH: ${{ github.event.workflow_run.repository.default_branch }}
REPOSITORY_DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
steps:
- name: SSH and deploy
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
IMAGE_SHA="${{ env.WORKFLOW_RUN_HEAD_SHA }}"
FALLBACK_BRANCH="${{ env.WORKFLOW_RUN_HEAD_BRANCH }}"
IMAGE_TAG="${IMAGE_SHA}"
IMAGE_PATH="${{ env.REGISTRY_URL }}/${{ env.REGISTRY_ORG }}/${{ env.REGISTRY_IMAGE_NAME }}"
if [ -z "$FALLBACK_BRANCH" ]; then
FALLBACK_BRANCH="${{ env.WORKFLOW_RUN_REPO_DEFAULT_BRANCH }}"
fi
if [ -z "$FALLBACK_BRANCH" ]; then
FALLBACK_BRANCH="${{ env.REPOSITORY_DEFAULT_BRANCH }}"
fi
if [ -z "$IMAGE_TAG" ] && [ -n "$FALLBACK_BRANCH" ]; then
case "$FALLBACK_BRANCH" in
refs/heads/*)
FALLBACK_BRANCH="${FALLBACK_BRANCH#refs/heads/}"
;;
esac
if [ "$FALLBACK_BRANCH" = "${DEFAULT_BRANCH:-main}" ]; then
IMAGE_TAG="latest"
fi
fi
if [ -z "$IMAGE_TAG" ]; then
echo "Missing workflow run head SHA and no default-branch fallback available; aborting deployment." >&2
exit 1
fi
docker pull "$IMAGE_PATH:$IMAGE_TAG"
docker stop calminer || true
docker rm calminer || true
docker run -d --name calminer -p 8000:8000 \
-e DATABASE_DRIVER=${{ secrets.DATABASE_DRIVER }} \
-e DATABASE_HOST=${{ secrets.DATABASE_HOST }} \
-e DATABASE_PORT=${{ secrets.DATABASE_PORT }} \
-e DATABASE_USER=${{ secrets.DATABASE_USER }} \
-e DATABASE_PASSWORD=${{ secrets.DATABASE_PASSWORD }} \
-e DATABASE_NAME=${{ secrets.DATABASE_NAME }} \
-e DATABASE_SCHEMA=${{ secrets.DATABASE_SCHEMA }} \
"$IMAGE_PATH:$IMAGE_TAG"
for attempt in {1..10}; do
if curl -fsS http://localhost:8000/health >/dev/null; then
echo "Deployment health check passed"
exit 0
fi
echo "Health check attempt ${attempt} failed; retrying in 3s"
sleep 3
done
echo "Deployment health check failed after retries" >&2
docker logs calminer >&2 || true
exit 1