Skip to content

Commit

Permalink
feat: Add publish script SQUASH
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuacurtiss committed Feb 26, 2025
1 parent e368293 commit ef7e868
Showing 1 changed file with 36 additions and 26 deletions.
62 changes: 36 additions & 26 deletions shared/bin/publish.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ red=$(tput setaf 1)
yellow=$(tput setaf 3)
norm=$(tput sgr0)

arches_default=linux/arm64,linux/amd64
arches_default=linux/aarch64,linux/amd64
org_default=uicpharm
reg_default=ghcr.io

Expand All @@ -19,7 +19,10 @@ dry_run=false
exact=false
push=true
verbose=false
cmd_note="${yellow}Command:$norm "

err() { echo "$red$1$norm" >&2; }
warn() { echo "$yellow$1$norm" >&2; }
exec_cmd() { if $dry_run; then echo "${yellow}Command:${norm} $1" | tr -s ' '; else eval "$1"; fi }

display_help() {
cat <<EOF
Expand Down Expand Up @@ -50,7 +53,7 @@ EOF

# Positional parameter: Docker file
if [[ $1 == -* || -z $1 ]]; then
[[ $1 == -h || $1 == --help ]] || echo "${red}You must provide a Docker file.$norm" >&2
[[ $1 == -h || $1 == --help ]] || err "You must provide a Docker file."
display_help; exit 1;
else
dockerfile=$(realpath "$1")
Expand Down Expand Up @@ -85,57 +88,59 @@ while getopts heva:n:o:r:-: OPT; do
o | org) org=$OPTARG ;;
r | registry) reg=$OPTARG ;;
v | verbose) verbose=true ;;
\?) echo "${red}Invalid option: -$OPT$norm" >&2 ;;
*) echo "${red}Some of these options are invalid:$norm $*" >&2; exit 2 ;;
\?) err "Invalid option: -$OPT" ;;
*) err "Some of these options are invalid: $*"; exit 2 ;;
esac
done
shift $((OPTIND - 1))

# Check dependencies, and abort if any are missing
for c in tput realpath dirname uname basename docker jq; do
for c in tput realpath dirname basename docker jq; do
if ! which "$c" &>/dev/null; then
echo "${red}The $ul$c$rmul command is required by this script. Aborting.$norm" >&2
err "The $ul$c$rmul command is required by this script. Aborting."
exit 1
fi
done

# Detect podman
[[ $(docker --version) == podman* ]] && is_podman=true || is_podman=false

# Check arch when running as podman, which will only support native arch
native_arch=linux/$(uname -m | sed -e 's/aarch/arm/' -e 's/x86_/amd/')
# Check arch... when running as podman, only support native arch.
# Get arch info from docker/podman itself. Sadly, they report that info differently.
docker_info=$(docker info -f json)
native_arch=$(jq -r '.OSType' <<< "$docker_info")/$(jq -r '.Architecture' <<< "$docker_info")
$is_podman && native_arch=$(jq -r '.host.os' <<< "$docker_info")/$(jq -r '.host.arch' <<< "$docker_info")
if $is_podman && [[ $arches != "$native_arch" ]]; then
arches=$native_arch
echo "${yellow}When running podman, you can only run your native architecture. Changing target arch to $ul$arches$rmul.$norm"
warn "When running podman, you can only run your native architecture. Changing target arch to $ul$arches$rmul."
fi

# Repo package info
name=$(jq -r '.name' "$context/package.json" | tr -d ' ')
ver=$(jq -r '.version' "$context/package.json")
homepage=$(jq -r '.homepage' "$context/package.json")
ver=$(jq -r '.version' "$context/package.json" | tr -d ' ')
homepage=$(jq -r '.homepage' "$context/package.json" | tr -d ' ')

# Validation of name/version as required values
[[ $name == null ]] && name=
[[ -z $name ]] && echo "${red}No ${ul}repository$rmul name provided in package.json.$norm" && exit 1
[[ -z $name ]] && err "No ${ul}name$rmul provided in package.json." && exit 1
[[ $ver == null ]] && ver=
[[ -z $ver ]] && echo "${red}No ${ul}version$rmul provided in package.json.$norm" && exit 1
[[ -z $ver ]] && err "No ${ul}version$rmul provided in package.json." && exit 1
img=$reg/$org/$name${name_append:+/$name_append}

# We want homepage without any anchor or query params
homepage=${homepage%#*}
homepage=${homepage%\?*}
# Warn if homepage is not provided
[[ $homepage == null ]] && homepage=
[[ -z $homepage ]] && echo "${yellow}No ${ul}homepage$rmul provided in package.json. The image will be pushed, but will not be assigned to a repository.$norm" >&2
[[ -z $homepage ]] && warn "No ${ul}homepage$rmul provided in package.json. The image will be pushed, but will not be assigned to a repository."

# Make sure container builder exists (not needed for podman)
if ! $is_podman; then
builder="$org-builder"
builder_param="--builder $builder"
if ! docker builder inspect "$builder" &>/dev/null; then
$verbose && echo -n 'Creating custom builder: '
cmd="docker builder create --name $builder"
if $dry_run; then echo "$cmd_note$cmd"; else eval "$cmd"; fi
exec_cmd "docker builder create --name $builder"
fi
fi

Expand All @@ -151,7 +156,7 @@ tag_pat=$img:$maj.$min.$pat
tag_full=$img:$ver
latest=$img:latest

# Only use all variations for normal versions, not prereleases
# Use all variations for normal versions, but not for prereleases
if ! $exact && [[ $tag_full == "$tag_pat" ]]; then
tags=("$tag_maj" "$tag_min" "$tag_pat" "$latest")
else
Expand All @@ -175,23 +180,28 @@ if $verbose; then
echo
fi

#
# Docker builds the image and only keeps it in the builder cache unless you push it.
# Podman tags the images in your local images, but doesn't support a `--push` flag. So,
# if the user is pushing the images, we use `--push` for Docker, but for podman we use
# additional `docker push <tag>` calls.
#

# Build images and push them to the registry
$push && ! $is_podman && push_param=--push
cmd="docker build \
--file $dockerfile \
$builder_param \
$(for tag in "${tags[@]}"; do echo -n "--tag $tag "; done) \
exec_cmd "docker build \
-f $dockerfile \
--platform $arches \
$(for tag in "${tags[@]}"; do echo -n "-t $tag "; done) \
${homepage:+--annotation "org.opencontainers.image.source=$homepage"} \
$builder_param \
$push_param \
$context"
if $dry_run; then echo "$cmd_note$cmd" | tr -s ' '; else eval "$cmd"; fi

# In podman, we have to push the tags after building
$is_podman && $push && for tag in "${tags[@]}"; do
cmd="docker push $tag"
if $dry_run; then echo "$cmd_note$cmd"; else eval "$cmd"; fi
exec_cmd "docker push $tag"
done

# Stop builder when done (not needed for podman)
! $is_podman && ! $dry_run && docker builder stop "$builder"
$is_podman || exec_cmd "docker builder stop $builder"

0 comments on commit ef7e868

Please sign in to comment.