chore: add image publish and mirror sync scripts

This commit is contained in:
3252a8
2026-05-26 22:30:10 +03:00
parent 53f1cec401
commit c3381bdd31
7 changed files with 394 additions and 19 deletions
+53
View File
@@ -0,0 +1,53 @@
$ErrorActionPreference = "Stop"
if (-not $env:IMAGE_TAG) {
throw "Set IMAGE_TAG to the release tag you want to build and push"
}
$imageRegistriesRaw = if ($env:IMAGE_REGISTRIES) { $env:IMAGE_REGISTRIES } else { "ghcr.io,docker.io" }
$imageRegistries = @($imageRegistriesRaw -split "[,;\s]+" | Where-Object { $_ })
$imageNamespace = if ($env:IMAGE_NAMESPACE) { $env:IMAGE_NAMESPACE } else { "3252a8" }
$imageTag = $env:IMAGE_TAG
$imagePrefix = if ($env:IMAGE_PREFIX) { $env:IMAGE_PREFIX } else { "remnawave-minishop" }
$dockerfile = if ($env:DOCKERFILE) { $env:DOCKERFILE } else { "deploy/docker/Dockerfile" }
$targetsRaw = if ($env:TARGETS) { $env:TARGETS } else { "backend,worker,frontend" }
$targets = @($targetsRaw -split "[,;\s]+" | Where-Object { $_ })
function Get-ImageName {
param(
[string]$Registry,
[string]$Target
)
return "$Registry/$imageNamespace/$imagePrefix-$Target`:$imageTag"
}
function Build-Image {
param([string]$Target)
$tagArgs = @()
foreach ($registry in $imageRegistries) {
$tagArgs += @("-t", (Get-ImageName -Registry $registry -Target $Target))
}
Write-Host "Building $Target for: $($imageRegistries -join ', ')" -ForegroundColor Cyan
docker build -f $dockerfile --target $Target @tagArgs .
}
function Push-Image {
param([string]$Target)
foreach ($registry in $imageRegistries) {
$image = Get-ImageName -Registry $registry -Target $Target
Write-Host "Pushing $image" -ForegroundColor Cyan
docker push $image
}
}
foreach ($target in $targets) {
Build-Image -Target $target
}
foreach ($target in $targets) {
Push-Image -Target $target
}
+58
View File
@@ -0,0 +1,58 @@
#!/usr/bin/env bash
set -euo pipefail
IMAGE_REGISTRIES="${IMAGE_REGISTRIES:-ghcr.io docker.io}"
IMAGE_NAMESPACE="${IMAGE_NAMESPACE:-3252a8}"
IMAGE_TAG="${IMAGE_TAG:?Set IMAGE_TAG to the release tag you want to build and push}"
IMAGE_PREFIX="${IMAGE_PREFIX:-remnawave-minishop}"
DOCKERFILE="${DOCKERFILE:-deploy/docker/Dockerfile}"
TARGETS="${TARGETS:-backend worker frontend}"
normalize_list() {
local value="$1"
value="${value//,/ }"
value="${value//;/ }"
echo "$value"
}
read -r -a registries <<< "$(normalize_list "$IMAGE_REGISTRIES")"
read -r -a targets <<< "$(normalize_list "$TARGETS")"
image_name() {
local registry="$1"
local target="$2"
echo "$registry/$IMAGE_NAMESPACE/$IMAGE_PREFIX-$target:$IMAGE_TAG"
}
build_image() {
local target="$1"
local tags=()
local registry
for registry in "${registries[@]}"; do
tags+=("-t" "$(image_name "$registry" "$target")")
done
echo "Building $target for: ${registries[*]}"
docker build -f "$DOCKERFILE" --target "$target" "${tags[@]}" .
}
push_image() {
local target="$1"
local registry
local image
for registry in "${registries[@]}"; do
image="$(image_name "$registry" "$target")"
echo "Pushing $image"
docker push "$image"
done
}
for target in "${targets[@]}"; do
build_image "$target"
done
for target in "${targets[@]}"; do
push_image "$target"
done
+101
View File
@@ -0,0 +1,101 @@
param(
[string]$PrimaryRemote,
[string]$MirrorRemote,
[string[]]$Branches,
[switch]$Force,
[switch]$Prune,
[switch]$NoTags,
[switch]$DryRun
)
$ErrorActionPreference = "Stop"
if (-not $PrimaryRemote) {
$PrimaryRemote = if ($env:PRIMARY_REMOTE) { $env:PRIMARY_REMOTE } else { "origin" }
}
if (-not $MirrorRemote) {
$MirrorRemote = if ($env:MIRROR_REMOTE) { $env:MIRROR_REMOTE } else { "gitlab" }
}
if (-not $Branches -and $env:SYNC_BRANCHES) {
$Branches = @($env:SYNC_BRANCHES -split "[,;\s]+" | Where-Object { $_ })
}
if ($env:FORCE -eq "true") {
$Force = $true
}
if ($env:PRUNE -eq "true") {
$Prune = $true
}
if ($env:SYNC_TAGS -eq "false") {
$NoTags = $true
}
if ($env:DRY_RUN -eq "true") {
$DryRun = $true
}
function Invoke-Git {
param([string[]]$Arguments)
if ($DryRun) {
Write-Host ("git " + ($Arguments -join " "))
return
}
& git @Arguments
if ($LASTEXITCODE -ne 0) {
throw "git $($Arguments -join ' ') failed with exit code $LASTEXITCODE"
}
}
Invoke-Git -Arguments @("fetch", $PrimaryRemote, "--prune", "--tags")
if ($Force) {
Invoke-Git -Arguments @("fetch", $MirrorRemote, "--prune")
}
if (-not $Branches) {
$Branches = @(
git for-each-ref "--format=%(refname:strip=3)" "refs/remotes/$PrimaryRemote" |
Where-Object { $_ -and $_ -ne "HEAD" }
)
}
foreach ($branch in $Branches) {
if (-not $branch) {
continue
}
$src = "refs/remotes/$PrimaryRemote/$branch"
$dst = "refs/heads/$branch"
$args = @("push", $MirrorRemote)
if ($Force) {
$args += "--force-with-lease=$dst"
}
$args += "${src}:${dst}"
Invoke-Git -Arguments $args
}
if (-not $NoTags) {
Invoke-Git -Arguments @("push", $MirrorRemote, "--tags")
}
if ($Prune) {
$primaryBranchSet = @{}
foreach ($branch in $Branches) {
$primaryBranchSet[$branch] = $true
}
$mirrorBranches = @(
git ls-remote --heads $MirrorRemote |
ForEach-Object {
if ($_ -match "refs/heads/(.+)$") {
$Matches[1]
}
}
)
foreach ($mirrorBranch in $mirrorBranches) {
if (-not $primaryBranchSet.ContainsKey($mirrorBranch)) {
Invoke-Git -Arguments @("push", $MirrorRemote, ":refs/heads/$mirrorBranch")
}
}
}
+99
View File
@@ -0,0 +1,99 @@
#!/usr/bin/env bash
set -euo pipefail
PRIMARY_REMOTE="${PRIMARY_REMOTE:-origin}"
MIRROR_REMOTE="${MIRROR_REMOTE:-gitlab}"
SYNC_BRANCHES="${SYNC_BRANCHES:-}"
SYNC_TAGS="${SYNC_TAGS:-true}"
FORCE="${FORCE:-false}"
PRUNE="${PRUNE:-false}"
DRY_RUN="${DRY_RUN:-false}"
usage() {
cat <<'EOF'
Usage: scripts/sync-gitlab-mirror.sh [--force] [--prune] [--no-tags] [--dry-run]
Mirrors branches and tags from the primary GitHub remote to the GitLab backup remote.
Environment:
PRIMARY_REMOTE Primary remote name. Default: origin
MIRROR_REMOTE Mirror remote name. Default: gitlab
SYNC_BRANCHES Space/comma separated branch list. Default: all PRIMARY_REMOTE branches
SYNC_TAGS Push tags too. Default: true
FORCE Use --force-with-lease for branches. Default: false
PRUNE Delete GitLab branches missing on GitHub. Default: false
DRY_RUN Print git commands without running them. Default: false
EOF
}
while [[ $# -gt 0 ]]; do
case "$1" in
--force) FORCE="true" ;;
--prune) PRUNE="true" ;;
--no-tags) SYNC_TAGS="false" ;;
--dry-run) DRY_RUN="true" ;;
-h|--help) usage; exit 0 ;;
*) echo "Unknown argument: $1" >&2; usage; exit 2 ;;
esac
shift
done
run_git() {
if [[ "$DRY_RUN" == "true" ]]; then
printf 'git'
printf ' %q' "$@"
printf '\n'
return
fi
git "$@"
}
normalize_list() {
local value="$1"
value="${value//,/ }"
value="${value//;/ }"
echo "$value"
}
run_git fetch "$PRIMARY_REMOTE" --prune --tags
if [[ "$FORCE" == "true" ]]; then
run_git fetch "$MIRROR_REMOTE" --prune
fi
if [[ -n "$SYNC_BRANCHES" ]]; then
read -r -a branches <<< "$(normalize_list "$SYNC_BRANCHES")"
else
branches=()
while IFS= read -r branch; do
[[ "$branch" != "HEAD" ]] || continue
branches+=("$branch")
done < <(git for-each-ref --format='%(refname:strip=3)' "refs/remotes/$PRIMARY_REMOTE")
fi
for branch in "${branches[@]}"; do
[[ -n "$branch" ]] || continue
src="refs/remotes/$PRIMARY_REMOTE/$branch"
dst="refs/heads/$branch"
args=("push" "$MIRROR_REMOTE")
if [[ "$FORCE" == "true" ]]; then
args+=("--force-with-lease=$dst")
fi
args+=("$src:$dst")
run_git "${args[@]}"
done
if [[ "$SYNC_TAGS" == "true" ]]; then
run_git push "$MIRROR_REMOTE" --tags
fi
if [[ "$PRUNE" == "true" ]]; then
primary_branches="$(printf '%s\n' "${branches[@]}")"
while read -r mirror_branch; do
[[ -n "$mirror_branch" ]] || continue
if ! grep -Fxq "$mirror_branch" <<< "$primary_branches"; then
run_git push "$MIRROR_REMOTE" ":refs/heads/$mirror_branch"
fi
done < <(git ls-remote --heads "$MIRROR_REMOTE" | sed 's#.*refs/heads/##')
fi