diff --git a/.github/workflows/entry-push-latest.yml b/.github/workflows/entry-push-latest.yml index e9c11bdd25..ab93c1601d 100644 --- a/.github/workflows/entry-push-latest.yml +++ b/.github/workflows/entry-push-latest.yml @@ -22,11 +22,29 @@ permissions: security-events: write jobs: + sync-main: + name: 🔄 Sync main + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v7 + with: + fetch-depth: 0 + + - name: Sync main from source repository + shell: bash + env: + CI_SYNC_MAIN_SOURCE_REPOSITORY_DEFAULT: infinito-nexus/core + CI_SYNC_MAIN_SOURCE_REPOSITORY: ${{ vars.CI_SYNC_MAIN_SOURCE_REPOSITORY }} + CI_SYNC_MAIN_SOURCE_REPOSITORY_IS_SET: ${{ contains(toJSON(vars), '"CI_SYNC_MAIN_SOURCE_REPOSITORY"') }} + run: ./scripts/github/sync/main_from_repository.sh + # Skip CI when the push is to main and CI_RUN_ON_MAIN is not 'true', or # when the push is a branch creation whose commit is already in origin/main. # All other pushes run CI. run-policy: name: 🧭 Decide CI policy + needs: sync-main runs-on: ubuntu-latest outputs: should_run: ${{ steps.decide.outputs.should_run }} diff --git a/docs/contributing/artefact/git/pipeline.md b/docs/contributing/artefact/git/pipeline.md index 34008c7e8c..cd92f6cd76 100644 --- a/docs/contributing/artefact/git/pipeline.md +++ b/docs/contributing/artefact/git/pipeline.md @@ -14,6 +14,10 @@ Every external CI trigger MUST route through one of the entry workflows. Each en - [entry-push-latest.yml](../../../../.github/workflows/entry-push-latest.yml): calls the orchestrator for pushes on the supported branch prefixes and additionally invokes the release workflow on version tags. For the contributor-facing release procedure see [release.md](../../actions/release.md). - [entry-manual.yml](../../../../.github/workflows/entry-manual.yml): dispatches the orchestrator manually for a chosen distro set and whitelist. +The push entry synchronizes the current repository's `main` branch from the configured source repository before CI policy and deploy discovery run. +The default source is `infinito-nexus/core`, and `CI_SYNC_MAIN_SOURCE_REPOSITORY` MAY override or disable the sync as documented in [configuration.md](../../tools/github/actions/configuration.md). +This sync **force-overwrites** the local `main`, so on a synced repository you MUST NOT work directly in `main`; use a `feature/**`, `fix/**`, or `hotfix/**` branch and treat `main` as a read-only mirror. + ## PR Scope Detection 🔎 Before the full pipeline runs, CI detects the scope of the PR based on the changed files and the branch prefix (see [branch.md](branch.md)). diff --git a/docs/contributing/tools/github/actions/configuration.md b/docs/contributing/tools/github/actions/configuration.md index 8eb7ecb425..1932c5f36d 100644 --- a/docs/contributing/tools/github/actions/configuration.md +++ b/docs/contributing/tools/github/actions/configuration.md @@ -9,6 +9,7 @@ Repository variables are set under **Settings → Secrets and variables → Acti | Variable | Workflow | Default (unset) | Set to activate | |---|---|---|---| | `CI_CANCEL_IN_PROGRESS` | [entry-push-latest.yml](../../../../../.github/workflows/entry-push-latest.yml) | Cancels in-progress runs on new push | `false` to keep in-progress runs alive | +| `CI_SYNC_MAIN_SOURCE_REPOSITORY` | [entry-push-latest.yml](../../../../../.github/workflows/entry-push-latest.yml) | Syncs `main` from `infinito-nexus/core` before CI scope discovery | `/` to use another source, or `false`, empty, or the current repository to skip | | `CI_RUN_ON_MAIN` | [entry-push-latest.yml](../../../../../.github/workflows/entry-push-latest.yml) | Pushes to `main` skip CI | `true` to run CI on `main` pushes too | | `CI_ENABLE_AUTO_UPDATES` | [update.yml](../../../../../.github/workflows/update.yml), [dependabot-close.yml](../../../../../.github/workflows/dependabot-close.yml) | Update jobs skipped; Dependabot PRs auto-closed | `true` to allow update PRs (workflow-driven and Dependabot) | | `INFINITO_PLAYWRIGHT_KEEP` | [test-deploy-server.yml](../../../../../.github/workflows/test-deploy-server.yml), [test-deploy-universal.yml](../../../../../.github/workflows/test-deploy-universal.yml), [test-deploy-workstation.yml](../../../../../.github/workflows/test-deploy-workstation.yml), [test-deploy-local.yml](../../../../../.github/workflows/test-deploy-local.yml) | Playwright keeps trace, screenshot and video only when a test fails | `true` to keep them for every test (passing runs included) | @@ -45,6 +46,40 @@ cancel-in-progress: ${{ vars.CI_CANCEL_IN_PROGRESS != 'false' }} | `false` | `'false' != 'false'` → `false` | Does **not** cancel ✓ | | `true` | `'true' != 'false'` → `true` | Cancels in-progress runs ✓ | +## `CI_SYNC_MAIN_SOURCE_REPOSITORY` 🔄 + +Controls whether the push workflow updates the current repository's `main` branch before any later job derives deploy scope from `origin/main`. +This keeps fork branch pushes aligned with the canonical default branch so diff-driven role discovery sees only the branch's own changes. + +> ⚠️ **Forced overwrite — do not work in `main`.** +> When the sync runs, the current repository's `main` is **force-pushed** from the source repository's `main`. Any commit that exists only on the fork's `main` is **discarded without warning** on the next push to any covered branch. +> Never commit or merge work directly into `main` on a synced repository. Always work on a `feature/**`, `fix/**`, or `hotfix/**` branch; treat `main` as a read-only mirror of the source. +> If you must use `main` as a working branch, disable the sync first (see **To disable the sync** below). + +**Default behaviour (variable not set):** +The workflow uses `infinito-nexus/core` as the source repository. +When the current repository is already `infinito-nexus/core`, the same-repository skip applies. + +**To use a different source repository:** + +1. Open the repository on GitHub. +2. Go to **Settings → Secrets and variables → Actions**. +3. Switch to the **Variables** tab. +4. Click **New repository variable**. +5. Set **Name** to `CI_SYNC_MAIN_SOURCE_REPOSITORY` and **Value** to `/`. +6. Save. + +**To disable the sync:** + +Set the variable to `false`, `0`, `no`, `off`, `none`, an empty value, or the current repository name. + +| Variable value | Behaviour | +|---|---| +| *(not set)* | Uses `infinito-nexus/core`; syncs forks and skips in `infinito-nexus/core` itself ✓ | +| `infinito-nexus/core` in a fork | Syncs the fork's `main` from `infinito-nexus/core` ✓ | +| current repository | Sync skipped ✓ | +| `false`, `0`, `no`, `off`, `none`, or empty | Sync skipped ✓ | + ## `CI_RUN_ON_MAIN` 🎯 Controls whether pushes to `main` trigger the CI pipeline. Pushes to all other branches covered by the workflow (`feature/**`, `hotfix/**`, `fix/**`, `alert-autofix-*`) are unaffected. diff --git a/docs/contributing/tools/github/actions/workflows.md b/docs/contributing/tools/github/actions/workflows.md index 754e715c0e..ceb8df878c 100644 --- a/docs/contributing/tools/github/actions/workflows.md +++ b/docs/contributing/tools/github/actions/workflows.md @@ -26,7 +26,7 @@ Trigger column legend: **auto** = fires automatically (push, pull_request, sched | Workflow | Description | Trigger | Inputs | |---|---|---|---| | [entry-pull-request-change.yml](../../../../../.github/workflows/entry-pull-request-change.yml): `⚡ CI: Pull Request` | Detects PR scope from changed files + branch prefix, then conditionally calls the orchestrator. | auto (`pull_request`, `pull_request_target`: opened, synchronize, reopened, ready_for_review) | none | -| [entry-push-latest.yml](../../../../../.github/workflows/entry-push-latest.yml): `⚡ CI: Push` | Runs the orchestrator on pushes to `main`, `feature/**`, `hotfix/**`, `fix/**`, `chore/**`, `alert-autofix-*`; on tagged `main` it also calls `release-version.yml`. Respects the `CI_CANCEL_IN_PROGRESS` and `CI_RUN_ON_MAIN` repository variables, see [configuration.md](configuration.md). | auto (`push`) | none | +| [entry-push-latest.yml](../../../../../.github/workflows/entry-push-latest.yml): `⚡ CI: Push` | Syncs `main` from the configured source repository, then runs the orchestrator on pushes to `main`, `feature/**`, `hotfix/**`, `fix/**`, `chore/**`, `alert-autofix-*`; on tagged `main` it also calls `release-version.yml`. Respects the `CI_CANCEL_IN_PROGRESS`, `CI_SYNC_MAIN_SOURCE_REPOSITORY`, and `CI_RUN_ON_MAIN` repository variables, see [configuration.md](configuration.md). | auto (`push`) | none | | [entry-manual.yml](../../../../../.github/workflows/entry-manual.yml): `⚡ CI: Manual` | Manual dispatch of the full orchestrator for a chosen distro set and app whitelist. The `whitelist` input accepts the `__ALL__` sentinel for forced full deploy; see the "Diff-driven app selection" subsection in [pipeline.md](../../../artefact/git/pipeline.md). | manual | `distros` (default `debian`), `whitelist` (optional; `__ALL__` forces full deploy, empty triggers diff derivation, any other value is verbatim) | | [entry-pull-request-cancel.yml](../../../../../.github/workflows/entry-pull-request-cancel.yml): `🚫 Cancel: PR Runs on Close` | Cancels active workflow runs for a PR when it is closed or converted to draft. | auto (`pull_request_target`: closed, converted_to_draft) | none | | [delete-branch.yml](../../../../../.github/workflows/delete-branch.yml): `🚫 Cancel: Runs on Branch Delete` | Enters the push-entry concurrency group for the deleted branch so that any in-flight run on it is cancelled; a fallback step cancels remaining runs via the API. | auto (`delete`) | none | diff --git a/scripts/github/README.md b/scripts/github/README.md index 97d0447c91..2b43483f9b 100644 --- a/scripts/github/README.md +++ b/scripts/github/README.md @@ -9,6 +9,7 @@ This directory holds the shell helpers that GitHub Actions workflows under [`.gi | [runner/](runner/) | Host-runner hygiene around image builds and deploys (disk, swap, Docker daemon, diagnostics). | | [cancel/](cancel/) | Cancellation of in-progress workflow runs on PR-close and branch-delete events. | | [resolve/](resolve/) | Derivation of structured workflow inputs and outputs from repository state. | +| [sync/](sync/) | Ref synchronization before workflow jobs derive CI scope from git history. | | [update/](update/) | The scheduled `Update: Versions` pipeline (version bumps and PR open/refresh). | | [release/](release/) | Release-time gates that decide whether a tagged commit may be released. | diff --git a/scripts/github/sync/README.md b/scripts/github/sync/README.md new file mode 100644 index 0000000000..27f8b3560e --- /dev/null +++ b/scripts/github/sync/README.md @@ -0,0 +1,3 @@ +# GitHub Sync Helpers 🔄 + +This directory contains shell helpers that synchronize repository refs before later workflow jobs derive CI scope from git history. diff --git a/scripts/github/sync/main_from_repository.sh b/scripts/github/sync/main_from_repository.sh new file mode 100755 index 0000000000..c19bbdb0cc --- /dev/null +++ b/scripts/github/sync/main_from_repository.sh @@ -0,0 +1,51 @@ +#!/usr/bin/env bash +set -euo pipefail + +: "${GITHUB_REPOSITORY:?Missing GITHUB_REPOSITORY}" + +default_source="${CI_SYNC_MAIN_SOURCE_REPOSITORY_DEFAULT:-infinito-nexus/core}" +configured_source="${CI_SYNC_MAIN_SOURCE_REPOSITORY:-}" +configured_source_is_set="${CI_SYNC_MAIN_SOURCE_REPOSITORY_IS_SET:-false}" + +if [[ "${configured_source_is_set}" == "true" ]]; then + source_repository="${configured_source}" +else + source_repository="${default_source}" +fi + +source_trimmed="${source_repository//[[:space:]]/}" +source_lower="$(printf '%s' "${source_trimmed}" | tr '[:upper:]' '[:lower:]')" + +case "${source_lower}" in +"" | "false" | "0" | "no" | "off" | "none") + echo "Main sync skipped because CI_SYNC_MAIN_SOURCE_REPOSITORY is disabled." + exit 0 + ;; +esac + +normalize_repository() { + local value="${1}" + value="${value#https://github.com/}" + value="${value#http://github.com/}" + value="${value#ssh://git@github.com/}" + value="${value#git@github.com:}" + value="${value%.git}" + printf '%s' "${value}" | tr '[:upper:]' '[:lower:]' +} + +target_repository="$(normalize_repository "${GITHUB_REPOSITORY}")" +source_repository="$(normalize_repository "${source_trimmed}")" + +if [[ "${source_repository}" == "${target_repository}" ]]; then + echo "Main sync skipped because source repository matches current repository: ${GITHUB_REPOSITORY}." + exit 0 +fi + +if [[ "${source_repository}" != */* ]]; then + echo "ERROR: CI_SYNC_MAIN_SOURCE_REPOSITORY must be '/', a GitHub URL, or a disabled value." >&2 + exit 1 +fi + +echo "Syncing ${GITHUB_REPOSITORY}:main from ${source_repository}:main." +git fetch "https://github.com/${source_repository}.git" main:refs/remotes/main-sync-source/main --force +git push origin refs/remotes/main-sync-source/main:refs/heads/main --force