Skip to content

Instantly share code, notes, and snippets.

@iamazeem
Last active June 19, 2022 04:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamazeem/1c5821c5184a8f7eee413065291d25ba to your computer and use it in GitHub Desktop.
Save iamazeem/1c5821c5184a8f7eee413065291d25ba to your computer and use it in GitHub Desktop.
Bash script to push docker image to GitHub Container Registry
#!/bin/bash
set -e
if [[ -z $GITHUB_ACTOR || -z $GITHUB_TOKEN || -z $GITHUB_REPOSITORY ]]; then
echo "[ERR] Run $0 under GitHub Actions context!"
exit 1
fi
IMAGE_NAME=${1:-''}
IMAGE_TAG=${2:-''}
if [[ -z $IMAGE_NAME || -z $IMAGE_TAG ]]; then
echo "[ERR] Usage: $0 [IMAGE_NAME] [IMAGE_TAG]"
exit 1
fi
USERNAME="$GITHUB_ACTOR"
PASSWORD="$GITHUB_TOKEN"
REPOSITORY="$(echo -n "$GITHUB_REPOSITORY" | tr '[:upper:]' '[:lower:]')"
IMAGE="$IMAGE_NAME:$IMAGE_TAG"
GHCR_IMAGE="ghcr.io/$REPOSITORY/$IMAGE"
echo "Logging in to GitHub Container Registry"
echo "$PASSWORD" | docker login ghcr.io -u "$USERNAME" --password-stdin
echo "Retagging [$IMAGE => $GHCR_IMAGE]"
docker tag "$IMAGE" "$GHCR_IMAGE"
docker images
echo "Pushing [$GHCR_IMAGE]"
docker push "$GHCR_IMAGE"
echo "Image pushed successfully! [$GHCR_IMAGE]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment