feat(ci): enhance CI workflow with metadata outputs and add Coolify deployment workflow
This commit is contained in:
@@ -6,6 +6,11 @@ on:
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
|
outputs:
|
||||||
|
allow_push: ${{ steps.meta.outputs.allow_push }}
|
||||||
|
ref_name: ${{ steps.meta.outputs.ref_name }}
|
||||||
|
event_name: ${{ steps.meta.outputs.event_name }}
|
||||||
|
sha: ${{ steps.meta.outputs.sha }}
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
env:
|
env:
|
||||||
DEFAULT_BRANCH: main
|
DEFAULT_BRANCH: main
|
||||||
@@ -23,9 +28,16 @@ jobs:
|
|||||||
env:
|
env:
|
||||||
DEFAULT_BRANCH: ${{ env.DEFAULT_BRANCH }}
|
DEFAULT_BRANCH: ${{ env.DEFAULT_BRANCH }}
|
||||||
run: |
|
run: |
|
||||||
ref_name="${GITHUB_REF_NAME:-${GITHUB_REF##*/}}"
|
git_ref="${GITEA_REF:-${GITHUB_REF:-}}"
|
||||||
event_name="${GITHUB_EVENT_NAME:-}"
|
ref_name="${GITEA_REF_NAME:-${GITHUB_REF_NAME:-}}"
|
||||||
sha="${GITHUB_SHA:-}"
|
if [ -z "$ref_name" ] && [ -n "$git_ref" ]; then
|
||||||
|
ref_name="${git_ref##*/}"
|
||||||
|
fi
|
||||||
|
event_name="${GITEA_EVENT_NAME:-${GITHUB_EVENT_NAME:-}}"
|
||||||
|
sha="${GITEA_SHA:-${GITHUB_SHA:-}}"
|
||||||
|
if [ -z "$sha" ]; then
|
||||||
|
sha="$(git rev-parse HEAD)"
|
||||||
|
fi
|
||||||
|
|
||||||
if [ "$ref_name" = "${DEFAULT_BRANCH:-main}" ] && [ "$event_name" != "pull_request" ]; then
|
if [ "$ref_name" = "${DEFAULT_BRANCH:-main}" ] && [ "$event_name" != "pull_request" ]; then
|
||||||
echo "allow_push=true" >> "$GITHUB_OUTPUT"
|
echo "allow_push=true" >> "$GITHUB_OUTPUT"
|
||||||
@@ -37,6 +49,27 @@ jobs:
|
|||||||
echo "event_name=$event_name" >> "$GITHUB_OUTPUT"
|
echo "event_name=$event_name" >> "$GITHUB_OUTPUT"
|
||||||
echo "sha=$sha" >> "$GITHUB_OUTPUT"
|
echo "sha=$sha" >> "$GITHUB_OUTPUT"
|
||||||
|
|
||||||
|
- name: Validate registry configuration
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
if [ -z "${REGISTRY_URL}" ]; then
|
||||||
|
echo "::error::REGISTRY_URL secret not configured. Configure it with your Gitea container registry host." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
server_url="${GITEA_SERVER_URL:-${GITHUB_SERVER_URL:-}}"
|
||||||
|
server_host="${server_url#http://}"
|
||||||
|
server_host="${server_host#https://}"
|
||||||
|
server_host="${server_host%%/*}"
|
||||||
|
server_host="${server_host%%:*}"
|
||||||
|
registry_host="${REGISTRY_URL#http://}"
|
||||||
|
registry_host="${registry_host#https://}"
|
||||||
|
registry_host="${registry_host%%/*}"
|
||||||
|
registry_host="${registry_host%%:*}"
|
||||||
|
if [ -n "${server_host}" ] && ! printf '%s' "${registry_host}" | grep -qi "${server_host}"; then
|
||||||
|
echo "::warning::REGISTRY_URL (${REGISTRY_URL}) does not match current Gitea host (${server_host}). Ensure this registry endpoint is managed by Gitea." >&2
|
||||||
|
fi
|
||||||
|
|
||||||
- name: Set up QEMU and Buildx
|
- name: Set up QEMU and Buildx
|
||||||
uses: docker/setup-buildx-action@v3
|
uses: docker/setup-buildx-action@v3
|
||||||
|
|
||||||
@@ -83,7 +116,7 @@ jobs:
|
|||||||
|
|
||||||
deploy:
|
deploy:
|
||||||
needs: build
|
needs: build
|
||||||
if: github.ref == 'refs/heads/main' && github.event_name != 'pull_request'
|
if: needs.build.outputs.allow_push == 'true'
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
env:
|
env:
|
||||||
REGISTRY_URL: ${{ secrets.REGISTRY_URL }}
|
REGISTRY_URL: ${{ secrets.REGISTRY_URL }}
|
||||||
@@ -95,22 +128,29 @@ jobs:
|
|||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Capture commit metadata
|
||||||
|
id: commit_meta
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
message="$(git log -1 --pretty=%B | tr '\n' ' ')"
|
||||||
|
echo "message=$message" >> "$GITHUB_OUTPUT"
|
||||||
|
|
||||||
- name: Set up kubectl for staging
|
- name: Set up kubectl for staging
|
||||||
if: github.event.head_commit && contains(github.event.head_commit.message, '[deploy staging]')
|
if: contains(steps.commit_meta.outputs.message, '[deploy staging]')
|
||||||
uses: azure/k8s-set-context@v3
|
uses: azure/k8s-set-context@v3
|
||||||
with:
|
with:
|
||||||
method: kubeconfig
|
method: kubeconfig
|
||||||
kubeconfig: ${{ env.STAGING_KUBE_CONFIG }}
|
kubeconfig: ${{ env.STAGING_KUBE_CONFIG }}
|
||||||
|
|
||||||
- name: Set up kubectl for production
|
- name: Set up kubectl for production
|
||||||
if: github.event.head_commit && contains(github.event.head_commit.message, '[deploy production]')
|
if: contains(steps.commit_meta.outputs.message, '[deploy production]')
|
||||||
uses: azure/k8s-set-context@v3
|
uses: azure/k8s-set-context@v3
|
||||||
with:
|
with:
|
||||||
method: kubeconfig
|
method: kubeconfig
|
||||||
kubeconfig: ${{ env.PROD_KUBE_CONFIG }}
|
kubeconfig: ${{ env.PROD_KUBE_CONFIG }}
|
||||||
|
|
||||||
- name: Deploy to staging
|
- name: Deploy to staging
|
||||||
if: github.event.head_commit && contains(github.event.head_commit.message, '[deploy staging]')
|
if: contains(steps.commit_meta.outputs.message, '[deploy staging]')
|
||||||
run: |
|
run: |
|
||||||
kubectl set image deployment/calminer-app calminer=${REGISTRY_URL}/allucanget/${REGISTRY_CONTAINER_NAME}:latest
|
kubectl set image deployment/calminer-app calminer=${REGISTRY_URL}/allucanget/${REGISTRY_CONTAINER_NAME}:latest
|
||||||
kubectl apply -f k8s/configmap.yaml
|
kubectl apply -f k8s/configmap.yaml
|
||||||
@@ -118,7 +158,7 @@ jobs:
|
|||||||
kubectl rollout status deployment/calminer-app
|
kubectl rollout status deployment/calminer-app
|
||||||
|
|
||||||
- name: Collect staging deployment logs
|
- name: Collect staging deployment logs
|
||||||
if: github.event.head_commit && contains(github.event.head_commit.message, '[deploy staging]')
|
if: contains(steps.commit_meta.outputs.message, '[deploy staging]')
|
||||||
run: |
|
run: |
|
||||||
mkdir -p logs/deployment/staging
|
mkdir -p logs/deployment/staging
|
||||||
kubectl get pods -o wide > logs/deployment/staging/pods.txt
|
kubectl get pods -o wide > logs/deployment/staging/pods.txt
|
||||||
@@ -126,7 +166,7 @@ jobs:
|
|||||||
kubectl logs deployment/calminer-app --all-containers=true --tail=500 > logs/deployment/staging/calminer-app.log
|
kubectl logs deployment/calminer-app --all-containers=true --tail=500 > logs/deployment/staging/calminer-app.log
|
||||||
|
|
||||||
- name: Deploy to production
|
- name: Deploy to production
|
||||||
if: github.event.head_commit && contains(github.event.head_commit.message, '[deploy production]')
|
if: contains(steps.commit_meta.outputs.message, '[deploy production]')
|
||||||
run: |
|
run: |
|
||||||
kubectl set image deployment/calminer-app calminer=${REGISTRY_URL}/allucanget/${REGISTRY_CONTAINER_NAME}:latest
|
kubectl set image deployment/calminer-app calminer=${REGISTRY_URL}/allucanget/${REGISTRY_CONTAINER_NAME}:latest
|
||||||
kubectl apply -f k8s/configmap.yaml
|
kubectl apply -f k8s/configmap.yaml
|
||||||
@@ -134,7 +174,7 @@ jobs:
|
|||||||
kubectl rollout status deployment/calminer-app
|
kubectl rollout status deployment/calminer-app
|
||||||
|
|
||||||
- name: Collect production deployment logs
|
- name: Collect production deployment logs
|
||||||
if: github.event.head_commit && contains(github.event.head_commit.message, '[deploy production]')
|
if: contains(steps.commit_meta.outputs.message, '[deploy production]')
|
||||||
run: |
|
run: |
|
||||||
mkdir -p logs/deployment/production
|
mkdir -p logs/deployment/production
|
||||||
kubectl get pods -o wide > logs/deployment/production/pods.txt
|
kubectl get pods -o wide > logs/deployment/production/pods.txt
|
||||||
|
|||||||
105
.gitea/workflows/deploy-coolify.yml
Normal file
105
.gitea/workflows/deploy-coolify.yml
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
name: Deploy - Coolify
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
deploy:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
env:
|
||||||
|
COOLIFY_BASE_URL: ${{ secrets.COOLIFY_BASE_URL }}
|
||||||
|
COOLIFY_API_TOKEN: ${{ secrets.COOLIFY_API_TOKEN }}
|
||||||
|
COOLIFY_APPLICATION_ID: ${{ secrets.COOLIFY_APPLICATION_ID }}
|
||||||
|
COOLIFY_DEPLOY_ENV: ${{ secrets.COOLIFY_DEPLOY_ENV }}
|
||||||
|
DOCKER_COMPOSE_PATH: docker-compose.prod.yml
|
||||||
|
ENV_FILE_PATH: deploy/.env
|
||||||
|
steps:
|
||||||
|
- name: Checkout repository
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Capture deployment context
|
||||||
|
id: context
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
repo="${GITEA_REPOSITORY:-${GITHUB_REPOSITORY:-}}"
|
||||||
|
if [ -z "$repo" ]; then
|
||||||
|
repo="$(git remote get-url origin | sed 's#.*/\(.*\)\.git#\1#')"
|
||||||
|
fi
|
||||||
|
ref_name="${GITEA_REF_NAME:-${GITHUB_REF_NAME:-}}"
|
||||||
|
full_ref="${GITEA_REF:-${GITHUB_REF:-}}"
|
||||||
|
if [ -z "$ref_name" ] && [ -n "$full_ref" ]; then
|
||||||
|
ref_name="${full_ref##*/}"
|
||||||
|
fi
|
||||||
|
if [ -z "$ref_name" ]; then
|
||||||
|
ref_name="$(git rev-parse --abbrev-ref HEAD)"
|
||||||
|
fi
|
||||||
|
sha="${GITEA_SHA:-${GITHUB_SHA:-}}"
|
||||||
|
if [ -z "$sha" ]; then
|
||||||
|
sha="$(git rev-parse HEAD)"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "repository=$repo" >> "$GITHUB_OUTPUT"
|
||||||
|
echo "ref=${ref_name:-main}" >> "$GITHUB_OUTPUT"
|
||||||
|
echo "sha=$sha" >> "$GITHUB_OUTPUT"
|
||||||
|
|
||||||
|
- name: Prepare compose bundle
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
mkdir -p deploy
|
||||||
|
cp "$DOCKER_COMPOSE_PATH" deploy/docker-compose.yml
|
||||||
|
if [ -n "$COOLIFY_DEPLOY_ENV" ]; then
|
||||||
|
printf '%s\n' "$COOLIFY_DEPLOY_ENV" > "$ENV_FILE_PATH"
|
||||||
|
elif [ ! -f "$ENV_FILE_PATH" ]; then
|
||||||
|
echo "::error::COOLIFY_DEPLOY_ENV secret not configured and deploy/.env missing" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
- name: Validate Coolify secrets
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
missing=0
|
||||||
|
for var in COOLIFY_BASE_URL COOLIFY_API_TOKEN COOLIFY_APPLICATION_ID; do
|
||||||
|
if [ -z "${!var}" ]; then
|
||||||
|
echo "::error::Missing required secret: $var"
|
||||||
|
missing=1
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
if [ "$missing" -eq 1 ]; then
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
- name: Trigger deployment via Coolify API
|
||||||
|
env:
|
||||||
|
HEAD_SHA: ${{ steps.context.outputs.sha }}
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
api_url="$COOLIFY_BASE_URL/api/v1/applications/${COOLIFY_APPLICATION_ID}/deploy"
|
||||||
|
payload=$(jq -n --arg sha "$HEAD_SHA" '{ commitSha: $sha }')
|
||||||
|
response=$(curl -sS -w '\n%{http_code}' \
|
||||||
|
-X POST "$api_url" \
|
||||||
|
-H "Authorization: Bearer $COOLIFY_API_TOKEN" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-d "$payload")
|
||||||
|
body=$(echo "$response" | head -n -1)
|
||||||
|
status=$(echo "$response" | tail -n1)
|
||||||
|
echo "Deploy response status: $status"
|
||||||
|
echo "$body"
|
||||||
|
printf '%s' "$body" > deploy/coolify-response.json
|
||||||
|
if [ "$status" -ge 400 ]; then
|
||||||
|
echo "::error::Deployment request failed"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
- name: Upload deployment bundle
|
||||||
|
if: always()
|
||||||
|
uses: actions/upload-artifact@v3
|
||||||
|
with:
|
||||||
|
name: coolify-deploy-bundle
|
||||||
|
path: |
|
||||||
|
deploy/docker-compose.yml
|
||||||
|
deploy/.env
|
||||||
|
deploy/coolify-response.json
|
||||||
|
if-no-files-found: warn
|
||||||
Reference in New Issue
Block a user