Files
SteamWar/.gitea/workflows/build.yml
T
YoyoNow cbb2987504
Pull Request Build / Build (pull_request) Successful in 2m0s
Only build and deploy changed projects in CI
Every project gets a checkAffected task that decides whether it has to
be rebuilt. A project is affected when one of these changed:
- its own files, or paths it adds with an affected { } block
- the versions of its resolved dependencies
- a project it depends on
- the root project, which owns the shared build and CI files

Projects list what they ship in a deploy { } block, and can add
afterDeploy commands there. Jars are now byte-reproducible. Only files
whose hash differs from the deployed one are uploaded, and only their
afterDeploy commands are run.

The deploy workflow diffs against the last deployed commit, which is
stored on the server with the dependency versions. PR builds diff
against the merge base. Shared setup steps moved into composite actions
under .gitea/actions.
2026-11-01 09:36:19 +01:00

127 lines
4.3 KiB
YAML

name: Deploy
on:
push:
branches:
- main
- version/*
jobs:
deploy:
name: Build and deploy
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Setup build
uses: ./.gitea/actions/setup-build
with:
maven-credentials: ${{ secrets.SW_MAVEN_CREDENTIALS }}
- name: Setup SSH
uses: ./.gitea/actions/setup-ssh
with:
host: ${{ secrets.DEPLOY_HOST }}
user: ${{ secrets.DEPLOY_USER }}
port: ${{ secrets.DEPLOY_PORT }}
key: ${{ secrets.DEPLOY_SSH_KEY }}
- name: Resolve deploy target
id: target
shell: bash
run: |
set -euo pipefail
if [[ "${GITHUB_REF_NAME}" == "main" ]]; then
echo "path=/jars/current" >> "$GITHUB_OUTPUT"
elif [[ "${GITHUB_REF_NAME}" == version/* ]]; then
version="${GITHUB_REF_NAME#version/}"
if [[ ! "$version" =~ ^[A-Za-z0-9._-]+$ ]]; then
echo "Unsupported version branch name: ${GITHUB_REF_NAME}" >&2
exit 1
fi
echo "path=/jars/${version}" >> "$GITHUB_OUTPUT"
else
echo "Unsupported deployment branch: ${GITHUB_REF_NAME}" >&2
exit 1
fi
- name: Read deployed state
id: deployed
shell: bash
env:
DEPLOY_PATH: ${{ steps.target.outputs.path }}
run: |
set -euo pipefail
mkdir -p .ci
ssh deploy "cat '$DEPLOY_PATH/.dependency-versions' 2>/dev/null || true" > .ci/previous-dependency-versions
ssh deploy "cd '$DEPLOY_PATH' 2>/dev/null && sha256sum -- *.jar *.zip 2>/dev/null || true" > .ci/deployed-hashes
commit="$(ssh deploy "cat '$DEPLOY_PATH/.deployed-commit' 2>/dev/null || true")"
if [[ ! "$commit" =~ ^[0-9a-f]{40}$ ]]; then
echo "No valid deployed commit found in ${DEPLOY_PATH}"
commit=""
fi
echo "Deployed commit: ${commit:-none}"
echo "commit=${commit}" >> "$GITHUB_OUTPUT"
- name: Determine affected projects
id: affected
shell: bash
run: bash .gitea/scripts/affected-projects.sh .ci "${{ steps.deployed.outputs.commit }}"
- name: Build with Gradle
if: steps.affected.outputs.tasks != ''
shell: bash
env:
TASKS: ${{ steps.affected.outputs.tasks }}
run: |
set -euo pipefail
read -ra tasks <<< "$TASKS"
./gradlew clean "${tasks[@]}" --no-daemon --no-build-cache --refresh-dependencies
# Only jars whose content differs from the deployed one, so dependents that merely compile against a change stay untouched
- name: Stage changed artifacts
shell: bash
run: bash .gitea/scripts/stage-artifacts.sh .ci .ci/jars "${{ steps.affected.outputs.projects }}"
- name: Deploy
shell: bash
env:
DEPLOY_PATH: ${{ steps.target.outputs.path }}
run: |
set -euo pipefail
ssh deploy "mkdir -p '$DEPLOY_PATH'"
if compgen -G ".ci/jars/*" > /dev/null; then
ssh deploy "rm -rf '$DEPLOY_PATH/upload' && mkdir -p '$DEPLOY_PATH/upload'"
scp .ci/jars/* "deploy:$DEPLOY_PATH/upload"
ssh deploy "mv -f '$DEPLOY_PATH'/upload/* '$DEPLOY_PATH' && rm -r '$DEPLOY_PATH/upload' && chmod o-w '$DEPLOY_PATH'/*"
else
echo "No changed artifacts to deploy"
fi
# The next run only builds what changed since this commit
scp .ci/dependency-versions "deploy:$DEPLOY_PATH/.dependency-versions"
ssh deploy "echo '$GITHUB_SHA' > '$DEPLOY_PATH/.deployed-commit'"
# The afterDeploy commands of the deploy blocks of all projects with changed artifacts
- name: Run after deploy commands
if: github.ref_name == 'main'
shell: bash
env:
DEPLOY_PATH: ${{ steps.target.outputs.path }}
run: |
set -euo pipefail
while read -r project command; do
if grep -qxF "$project" .ci/deployed-projects; then
echo "Running '${command}' for ${project}"
ssh -n deploy "cd '$DEPLOY_PATH' && ${command}"
fi
done < .ci/deploy-commands