This commit is contained in:
2026-05-26 16:50:34 +08:00
parent f4f2393f72
commit 165e97bff7
5 changed files with 198 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
param(
[Parameter(Mandatory=$true)][string]$Registry,
[Parameter(Mandatory=$true)][string]$Owner,
[Parameter(Mandatory=$true)][string]$Repo,
[string]$Tag = 'latest',
[string]$Username = $null,
[string]$Token = $null
)
if (-not $Registry -or -not $Owner -or -not $Repo) {
Write-Error "Usage: .\scripts\build_and_push.ps1 -Registry <registry> -Owner <owner> -Repo <repo> [-Tag <tag>] [-Username <user>] [-Token <token>]"
exit 1
}
$localImage = "$Repo:$Tag"
$remoteImage = "$Registry/$Owner/$Repo:$Tag"
Write-Host "Building image: $localImage"
docker build -t $localImage .
Write-Host "Tagging image -> $remoteImage"
docker tag $localImage $remoteImage
if ($Token) {
Write-Host "Logging in to $Registry using token"
$secureToken = ConvertTo-SecureString $Token -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($Username, $secureToken)
$Token | docker login $Registry -u ${Username:-$Owner} --password-stdin
} else {
Write-Host "Logging in to $Registry interactively"
docker login $Registry -u ${Username:-$Owner}
}
Write-Host "Pushing $remoteImage"
docker push $remoteImage
Write-Host "Push finished: $remoteImage"

40
scripts/build_and_push.sh Normal file
View File

@@ -0,0 +1,40 @@
#!/usr/bin/env bash
set -euo pipefail
# build_and_push.sh <registry> <owner> <repo> [tag] [username] [token]
# Example:
# ./scripts/build_and_push.sh git.templarz.com templarz wsmud latest tempalrz <TOKEN>
REGISTRY=${1:-}
OWNER=${2:-}
REPO=${3:-}
TAG=${4:-latest}
USERNAME=${5:-}
TOKEN=${6:-}
if [ -z "$REGISTRY" ] || [ -z "$OWNER" ] || [ -z "$REPO" ]; then
echo "Usage: $0 <registry> <owner> <repo> [tag] [username] [token]"
exit 1
fi
LOCAL_IMAGE="${REPO}:${TAG}"
REMOTE_IMAGE="${REGISTRY}/${OWNER}/${REPO}:${TAG}"
echo "Building image: $LOCAL_IMAGE"
docker build -t "$LOCAL_IMAGE" .
echo "Tagging image -> $REMOTE_IMAGE"
docker tag "$LOCAL_IMAGE" "$REMOTE_IMAGE"
if [ -n "$TOKEN" ]; then
echo "Logging in to $REGISTRY using token (password-stdin)"
echo "$TOKEN" | docker login "$REGISTRY" -u "${USERNAME:-$OWNER}" --password-stdin
else
echo "Logging in to $REGISTRY interactively (you will be prompted)"
docker login "$REGISTRY" -u "${USERNAME:-$OWNER}"
fi
echo "Pushing $REMOTE_IMAGE"
docker push "$REMOTE_IMAGE"
echo "Push finished: $REMOTE_IMAGE"