Files
vintage-story/.gitea/workflows/vintage-story-build-server.yml
T
fithwum 138d3e7ed9
vintage-story-build-latest / Docker prune (vm-docker-build2) (push) Successful in 2s
vintage-story-build-latest / poll-debian-base-and-detect-changes (push) Successful in 6s
vintage-story-build-latest / Build and Push latest (push) Skipped
vintage-story-build-latest / generate-changelogs (push) Skipped
vintage-story-build-latest / generate-build-info (push) Skipped
test
2026-07-28 10:18:53 -07:00

410 lines
14 KiB
YAML

name: vintage-story-build-latest
on:
push:
branches:
- main
paths:
- '.gitea/workflows/vintage-story-build-server.yml'
- 'latest/Dockerfile'
- 'latest/*.sh'
- '!.cache/*'
- '!latest/CHANGES.md'
- '!latest/build-info.json'
schedule:
- cron: '5 22 * * 0'
env:
IMAGE_REGISTRY: gitea.fithwum.tech
IMAGE_ORG: fithwum
IMAGE_REPO_DEBIAN: debian-base
IMAGE_REPO_VINTAGE_STORY: vintage-story
IMAGE_TAG_DEBIAN: bookworm
DIGEST_FILE: .cache/debian-base.digest
VERSIONS: latest
jobs:
docker-prune:
name: Docker prune (${{ matrix.runner }})
runs-on: ${{ matrix.runner }}
strategy:
fail-fast: false
matrix:
runner:
- vm-docker-build2
steps:
- name: Prune unused Docker images
run: |
echo "[INFO] Pruning Docker images on ${{ matrix.runner }}..."
docker image prune -a -f || true
poll-debian-base-and-detect-changes:
runs-on: vm-docker-build2
needs: docker-prune
outputs:
digest_changed: ${{ steps.compare_digest.outputs.changed }}
versions_changed: ${{ steps.check_changed_versions.outputs.versions_changed }}
new_versions: ${{ steps.check_new_versions.outputs.new_versions }}
steps:
- name: Checkout repo
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Ensure .cache exists
run: mkdir -p .cache && touch .cache/.gitkeep
- name: Log in to Gitea Registry
run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login gitea.fithwum.tech -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin
- name: Get current digest of debian-base image
id: get_digest
run: |
IMAGE="${{ env.IMAGE_REGISTRY }}/${{ env.IMAGE_ORG }}/${{ env.IMAGE_REPO_DEBIAN }}:${{ env.IMAGE_TAG_DEBIAN }}"
docker pull "$IMAGE" > /dev/null
DIGEST=$(docker inspect --format='{{index .RepoDigests 0}}' "$IMAGE")
echo "digest=$DIGEST" >> $GITHUB_OUTPUT
echo "$DIGEST" > .cache/debian-base.digest.new
- name: Compare with stored digest
id: compare_digest
run: |
CURRENT_DIGEST="${{ steps.get_digest.outputs.digest }}"
LAST_DIGEST="$(cat ${{ env.DIGEST_FILE }} 2>/dev/null || echo '')"
if [ "$CURRENT_DIGEST" = "$LAST_DIGEST" ]; then
echo "[INFO] Digest unchanged"
echo "changed=false" >> $GITHUB_OUTPUT
exit 0
fi
echo "[INFO] Digest changed → updating cache"
echo "$CURRENT_DIGEST" > ${{ env.DIGEST_FILE }}
git config user.name "${{ secrets.GIT_USERNAME }}"
git config user.email "${{ secrets.GIT_EMAIL }}"
git add ${{ env.DIGEST_FILE }}
git commit -m "Update base digest to $CURRENT_DIGEST" || true
# --- Safe rebase with auto conflict resolution ---
git fetch origin main
if ! git rebase --strategy-option=theirs origin/main; then
echo "[WARN] Rebase conflict, auto-resolving by preferring local digest"
git rebase --abort
git fetch origin main
git reset --hard origin/main
echo "$CURRENT_DIGEST" > ${{ env.DIGEST_FILE }}
git add ${{ env.DIGEST_FILE }}
git commit -m "Force-update base digest to $CURRENT_DIGEST (auto-resolved)"
fi
git push origin HEAD:main
echo "changed=true" >> $GITHUB_OUTPUT
- name: Detect changed vintage-story versions
id: check_changed_versions
run: |
BEFORE_COMMIT="${{ github.event.before }}"
if [ -z "$BEFORE_COMMIT" ] || ! git cat-file -e "$BEFORE_COMMIT^{commit}" 2>/dev/null; then
BEFORE_COMMIT=$(git rev-parse HEAD~1)
fi
CHANGED=$(git diff --name-only "$BEFORE_COMMIT" HEAD || true)
CHANGED_VERSIONS=""
for version in $VERSIONS; do
echo "$CHANGED" | grep -q "^$version/.*\.\(sh\|Dockerfile\)$" && CHANGED_VERSIONS="$CHANGED_VERSIONS $version"
done
CHANGED_VERSIONS=$(echo "$CHANGED_VERSIONS" | xargs) # trim whitespace
echo "versions_changed=$CHANGED_VERSIONS" >> $GITHUB_OUTPUT
- name: Detect vintage-story versions without build-info
id: check_new_versions
run: |
NEW_VERSIONS=""
GIT_CREDENTIAL="${{ secrets.GIT_TOKEN || secrets.GIT_PASSWORD }}"
git clone --depth=1 "https://${{ secrets.GIT_USERNAME }}:${GIT_CREDENTIAL}@gitea.fithwum.tech/fithwum/vintage-story.git" upload-repo
for version in $VERSIONS; do
if [ ! -f "upload-repo/$version/build-info.json" ]; then
echo "[INFO] No build-info.json for $version → treating as new"
NEW_VERSIONS="$NEW_VERSIONS $version"
fi
done
echo "new_versions=$NEW_VERSIONS" >> $GITHUB_OUTPUT
build-vintage-story-images:
runs-on: vm-docker-build2
needs:
- docker-prune
- poll-debian-base-and-detect-changes
if: |
needs.poll-debian-base-and-detect-changes.outputs.digest_changed == 'true' ||
needs.poll-debian-base-and-detect-changes.outputs.versions_changed != '' ||
needs.poll-debian-base-and-detect-changes.outputs.new_versions != ''
strategy:
matrix:
version: [latest]
name: Build and Push ${{ matrix.version }}
steps:
- name: Checkout repo
uses: actions/checkout@v3
- name: Log in to Gitea Registry
run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login gitea.fithwum.tech -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin
- name: Clone vintage-story repo (with retries)
run: |
GIT_CREDENTIAL="${{ secrets.GIT_TOKEN || secrets.GIT_PASSWORD }}"
for i in {1..5}; do
git clone "https://${{ secrets.GIT_USERNAME }}:${GIT_CREDENTIAL}@gitea.fithwum.tech/fithwum/vintage-story.git" upload-repo && break
echo "[WARN] Clone attempt $i failed. Retrying in 10s..."
sleep 10
done
if [ ! -d upload-repo ]; then
echo "[ERROR] Failed to clone vintage-story repo after retries."
exit 1
fi
- name: Clear all build caches
run: |
docker builder prune -af || true
docker buildx prune -af || true
- name: Build and push image
run: |
IMAGE="$IMAGE_REGISTRY/$IMAGE_ORG/$IMAGE_REPO_VINTAGE_STORY:${{ matrix.version }}"
docker buildx build --platform linux/amd64 --pull --no-cache --push -t "$IMAGE" "./${{ matrix.version }}"
generate-changelogs:
needs: build-vintage-story-images
runs-on: vm-docker-build2
steps:
- name: Checkout repo
uses: actions/checkout@v3
- name: Create temporary changelog workspace
run: mkdir -p changelogs
- name: Clone vintage-story repo
run: |
GIT_CREDENTIAL="${{ secrets.GIT_TOKEN || secrets.GIT_PASSWORD }}"
git clone --depth=3 "https://${{ secrets.GIT_USERNAME }}:${GIT_CREDENTIAL}@gitea.fithwum.tech/fithwum/vintage-story.git" upload-repo
- name: Generate changelogs
run: |
for version in $VERSIONS; do
echo "[INFO] Generating changelog for $version"
changelog="changelogs/$version/CHANGES.md"
mkdir -p "$(dirname "$changelog")"
touch "$changelog"
cd upload-repo
infofile="$version/build-info.json"
last_commit=""
if [ -f "$infofile" ]; then
last_commit=$(jq -r '.commit' "$infofile")
fi
echo -e "## $(date -u +'%Y-%m-%dT%H:%M:%SZ')\n" > "../$changelog"
if [ -n "$last_commit" ]; then
COMMITS=$(git log "${last_commit}..HEAD" --pretty=format:"- %s (%an)" || true)
fi
if [ -z "$COMMITS" ]; then
echo "[INFO] No new commits since last build. Showing latest 5 instead."
COMMITS=$(git log -n 5 --pretty=format:"- %s (%an)" || true)
fi
echo "$COMMITS" >> "../$changelog"
cd ..
done
- name: Copy changelogs into repo
run: |
for version in $VERSIONS; do
mkdir -p upload-repo/$version
cp changelogs/$version/CHANGES.md upload-repo/$version/
done
- name: Commit and push changelogs (temp branch)
run: |
cd upload-repo
git config user.name "${{ secrets.GIT_USERNAME }}"
git config user.email "${{ secrets.GIT_EMAIL }}"
TEMP_BRANCH="tmp-changelog-$(date +%s)"
# Create temporary branch
git checkout -b "$TEMP_BRANCH"
# Stage changes
git add */CHANGES.md
# Nothing changed?
if git diff --cached --quiet; then
echo "[INFO] No changelog changes."
exit 0
fi
git commit -m "Update changelogs on $(date -u +'%Y-%m-%dT%H:%M:%SZ') [skip ci]"
# Push temporary branch
git push origin HEAD:$TEMP_BRANCH
# Rebase temp branch onto latest main
git fetch origin main
git checkout "$TEMP_BRANCH"
if ! git rebase origin/main; then
echo "[WARN] Rebase failed, using merge instead."
git rebase --abort || true
git merge origin/main --no-edit
fi
git push origin HEAD:$TEMP_BRANCH --force
# Fast-forward main
git checkout main
git reset --hard origin/main
git merge --no-ff "$TEMP_BRANCH" \
-m "Merge changelog updates"
git push origin main
git push origin --delete "$TEMP_BRANCH" || true
generate-build-info:
needs: generate-changelogs
runs-on: vm-docker-build2
steps:
- name: Checkout repo
uses: actions/checkout@v3
- name: Prepare temporary build-info workspace
run: mkdir -p buildinfo
- name: Clone vintage-story repo
run: |
GIT_CREDENTIAL="${{ secrets.GIT_TOKEN || secrets.GIT_PASSWORD }}"
git clone --depth=1 "https://${{ secrets.GIT_USERNAME }}:${GIT_CREDENTIAL}@gitea.fithwum.tech/fithwum/vintage-story.git" upload-repo
- name: Generate build-info files
run: |
human_size() {
local b=$1 d='' s=0 S=(B KB MB GB TB)
while ((b >= 1024 && s < ${#S[@]}-1)); do
d=$((b % 1024)); b=$((b / 1024)); s=$((s + 1))
done
printf "%s%s\n" "$b" "${S[$s]}"
}
for version in $VERSIONS; do
echo "[INFO] Generating build-info.json for $version"
image="${IMAGE_REGISTRY}/${IMAGE_ORG}/${IMAGE_REPO_VINTAGE_STORY}:$version"
infofile="buildinfo/$version/build-info.json"
mkdir -p "$(dirname "$infofile")"
if ! docker pull "$image"; then
echo "[WARN] Failed to pull $image — setting fields to unknown/0"
digest="unknown"
size_bytes=0
else
digest=$(docker inspect --format='{{if .RepoDigests}}{{index .RepoDigests 0}}{{else}}unknown{{end}}' "$image" 2>/dev/null || echo "unknown")
size_bytes=$(docker image inspect "$image" --format='{{.Size}}' 2>/dev/null || echo "0")
size_bytes=${size_bytes//[^0-9]/}
if [[ -z "$size_bytes" ]]; then size_bytes=0; fi
fi
size_human=$(human_size "$size_bytes")
cd upload-repo
commit=$(git rev-parse HEAD)
cd ..
jq -n \
--arg version "$version" \
--arg commit "$commit" \
--arg build_time "$(date -u +'%Y-%m-%dT%H:%M:%SZ')" \
--arg image_tag "$image" \
--arg digest "$digest" \
--arg image_size "$size_human" \
--argjson image_size_bytes "$size_bytes" \
'{
version: $version,
commit: $commit,
build_time: $build_time,
image_tag: $image_tag,
digest: $digest,
image_size: $image_size,
image_size_bytes: $image_size_bytes
}' > "$infofile"
done
- name: Copy build-info.json into repo
run: |
for version in $VERSIONS; do
mkdir -p upload-repo/$version
cp "buildinfo/$version/build-info.json" "upload-repo/$version/"
done
- name: Commit and push build-info (temp branch)
run: |
cd upload-repo
git config user.name "${{ secrets.GIT_USERNAME }}"
git config user.email "${{ secrets.GIT_EMAIL }}"
TEMP_BRANCH="tmp-buildinfo-$(date +%s)"
# Create temporary branch
git checkout -b "$TEMP_BRANCH"
# Stage changes
git add */build-info.json
# Nothing changed?
if git diff --cached --quiet; then
echo "[INFO] No build-info changes."
exit 0
fi
git commit -m "Update build-info on $(date -u +'%Y-%m-%dT%H:%M:%SZ') [skip ci]"
# Push temporary branch
git push origin HEAD:$TEMP_BRANCH
# Rebase temp branch onto latest main
git fetch origin main
git checkout "$TEMP_BRANCH"
if ! git rebase origin/main; then
echo "[WARN] Rebase failed, using merge instead."
git rebase --abort || true
git merge origin/main --no-edit
fi
git push origin HEAD:$TEMP_BRANCH --force
# Merge into main
git checkout main
git reset --hard origin/main
git merge --no-ff "$TEMP_BRANCH" \
-m "Merge build-info updates"
git push origin main
git push origin --delete "$TEMP_BRANCH" || true
for version in $VERSIONS; do
TAG="build-$version"
git tag -d "$TAG" 2>/dev/null || true
git push origin ":refs/tags/$TAG" 2>/dev/null || true
git tag "$TAG"
git push origin "$TAG" --force
done