77 lines
2.8 KiB
YAML
77 lines
2.8 KiB
YAML
name: Build and deploy Backend
|
|
|
|
on:
|
|
push:
|
|
branches:
|
|
- main
|
|
|
|
jobs:
|
|
build:
|
|
runs-on: build-host
|
|
env:
|
|
SERVICE_NAME: backend
|
|
IMAGE: gitea.dsrptlab.com/mapflow/backend:${{ github.sha }}
|
|
DOKPLOY_APPLICATION_ID: ahtv545dRvRjoJnFNjiYv
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
|
|
- name: Configure Gitea registry auth
|
|
run: |
|
|
set -euo pipefail
|
|
mkdir -p ~/.docker
|
|
auth="$(printf '%s:%s' "${{ secrets.REGISTRY_USERNAME }}" "${{ secrets.REGISTRY_TOKEN }}" | base64 | tr -d '\n')"
|
|
printf '{"auths":{"gitea.dsrptlab.com":{"auth":"%s"}}}\n' "$auth" > ~/.docker/config.json
|
|
|
|
- name: Build and push image
|
|
run: |
|
|
set -euo pipefail
|
|
builder="builder"
|
|
if ! docker buildx inspect "$builder" >/dev/null 2>&1; then
|
|
docker buildx create --name "$builder" --driver docker-container --buildkitd-config /etc/buildkit/buildkitd.toml
|
|
fi
|
|
docker buildx use "$builder"
|
|
docker buildx inspect --bootstrap
|
|
docker buildx build --push --provenance=false --tag "$IMAGE" .
|
|
|
|
- name: Skip stale deployment
|
|
run: |
|
|
set -euo pipefail
|
|
latest_sha="$(git ls-remote origin refs/heads/main | awk '{print $1}')"
|
|
if [ "$latest_sha" = "${GITHUB_SHA}" ]; then
|
|
touch .deploy-current
|
|
else
|
|
echo "A newer main commit exists: $latest_sha. Skipping deploy for ${GITHUB_SHA}."
|
|
fi
|
|
|
|
- name: Update Dokploy image
|
|
run: |
|
|
set -euo pipefail
|
|
[ -f .deploy-current ] || exit 0
|
|
payload=$(cat <<JSON
|
|
{"applicationId":"$DOKPLOY_APPLICATION_ID","dockerImage":"$IMAGE","username":"${{ secrets.REGISTRY_USERNAME }}","password":"${{ secrets.REGISTRY_TOKEN }}","registryUrl":"gitea.dsrptlab.com"}
|
|
JSON
|
|
)
|
|
curl -fsS -X POST "${{ secrets.DOKPLOY_URL }}/api/application.saveDockerProvider" \
|
|
-H "x-api-key: ${{ secrets.DOKPLOY_TOKEN }}" \
|
|
-H "Content-Type: application/json" \
|
|
-d "$payload"
|
|
|
|
- name: Deploy in Dokploy
|
|
run: |
|
|
set -euo pipefail
|
|
[ -f .deploy-current ] || exit 0
|
|
short_sha="${GITHUB_SHA:0:7}"
|
|
payload=$(cat <<JSON
|
|
{"applicationId":"$DOKPLOY_APPLICATION_ID","title":"$SERVICE_NAME #${GITHUB_RUN_NUMBER:-0} $short_sha","description":"Deploy registry image $IMAGE"}
|
|
JSON
|
|
)
|
|
curl -fsS -X POST "${{ secrets.DOKPLOY_URL }}/api/application.deploy" \
|
|
-H "x-api-key: ${{ secrets.DOKPLOY_TOKEN }}" \
|
|
-H "Content-Type: application/json" \
|
|
-d "$payload"
|
|
|
|
- name: Prune shared BuildKit cache
|
|
run: |
|
|
set -euo pipefail
|
|
docker buildx prune --builder builder --all --max-used-space 40gb -f
|