69 lines
2.8 KiB
YAML
69 lines
2.8 KiB
YAML
name: Publish release container
|
|
|
|
on:
|
|
release:
|
|
types: [published]
|
|
|
|
jobs:
|
|
container:
|
|
if: ${{ gitea.repository == 'laysense/laysite' && !gitea.event.release.prerelease }}
|
|
runs-on: host
|
|
timeout-minutes: 30
|
|
steps:
|
|
- name: Build and publish the release image
|
|
shell: bash
|
|
env:
|
|
RELEASE_TAG: ${{ gitea.event.release.tag_name }}
|
|
PACKAGE_USERNAME: ${{ secrets.PACKAGE_USERNAME }}
|
|
PACKAGE_TOKEN: ${{ secrets.PACKAGE_TOKEN }}
|
|
run: |
|
|
set -euo pipefail
|
|
if ! printf '%s\n' "$RELEASE_TAG" | grep -Eq '^v[0-9]+\.[0-9]+\.[0-9]+$'; then
|
|
echo 'The release tag must use vMAJOR.MINOR.PATCH.' >&2
|
|
exit 1
|
|
fi
|
|
: "${PACKAGE_USERNAME:?Set the PACKAGE_USERNAME Actions secret.}"
|
|
: "${PACKAGE_TOKEN:?Set the PACKAGE_TOKEN Actions secret.}"
|
|
|
|
work="$(mktemp -d)"
|
|
trap 'rm -rf "$work"' EXIT
|
|
export DOCKER_CONFIG="$work/docker"
|
|
mkdir -m 700 "$DOCKER_CONFIG"
|
|
|
|
registry=git.laysense.com
|
|
source_url="https://$registry/laysense/laysite"
|
|
image="$registry/laysense/laysite:$RELEASE_TAG"
|
|
git init --quiet "$work/source"
|
|
git -C "$work/source" fetch --quiet --depth 1 "$source_url.git" "refs/tags/$RELEASE_TAG"
|
|
git -C "$work/source" checkout --quiet --detach FETCH_HEAD
|
|
if [ "$(cat "$work/source/VERSION")" != "${RELEASE_TAG#v}" ]; then
|
|
echo 'VERSION in the release commit must match the tag.' >&2
|
|
exit 1
|
|
fi
|
|
|
|
docker build --pull --tag "$image" \
|
|
--label "org.opencontainers.image.source=$source_url" \
|
|
--label "org.opencontainers.image.version=${RELEASE_TAG#v}" \
|
|
--label "org.opencontainers.image.revision=$(git -C "$work/source" rev-parse HEAD)" \
|
|
"$work/source"
|
|
printf '%s' "$PACKAGE_TOKEN" | docker login "$registry" --username "$PACKAGE_USERNAME" --password-stdin
|
|
docker push "$image"
|
|
package_url="https://$registry/api/v1/packages/laysense/container/laysite"
|
|
package_api() {
|
|
printf 'Authorization: token %s\n' "$PACKAGE_TOKEN" \
|
|
| curl --fail --silent --show-error --header @- "$@"
|
|
}
|
|
linked_repository() {
|
|
package_api "$package_url/$RELEASE_TAG" | python3 -c \
|
|
'import json, sys; print((json.load(sys.stdin).get("repository") or {}).get("full_name", ""))'
|
|
}
|
|
linked_repo="$(linked_repository)"
|
|
if [ -z "$linked_repo" ]; then
|
|
if ! package_api --request POST "$package_url/-/link/laysite"; then
|
|
[ "$(linked_repository)" = 'laysense/laysite' ] || exit 1
|
|
fi
|
|
elif [ "$linked_repo" != 'laysense/laysite' ]; then
|
|
echo "Package is already linked to $linked_repo." >&2
|
|
exit 1
|
|
fi
|