Skip to content

Instantly share code, notes, and snippets.

@elnexreal
Last active May 9, 2024 18:06
Show Gist options
  • Save elnexreal/a6133105b90905ce050a262612ae8a0c to your computer and use it in GitHub Desktop.
Save elnexreal/a6133105b90905ce050a262612ae8a0c to your computer and use it in GitHub Desktop.
Automatically pushes the current Docker container to the GitHub Container Registery
name: Build and Push container to GHCR
on:
push:
tags: "*"
jobs:
build_and_push:
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Login to GitHub Container Registery (GHCR)
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.repository_owner }} --password-stdin
- name: Build the image
run: |
docker build -t ${{ github.event.repository.name }} .
- name: Push the latest build
run: |
IMAGE_ID=ghcr.io/${{ github.repository_owner }}/${{ github.event.repository.name }}
IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]')
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
echo IMAGE_ID=$IMAGE_ID
echo VERSION=$VERSION
docker tag ${{ github.event.repository.name }} $IMAGE_ID:$VERSION
docker push $IMAGE_ID:$VERSION
@elnexreal
Copy link
Author

elnexreal commented May 6, 2024

NOTES

  • This works by using the Tag system (every time a tag is created the container gets pushed)
  • The version scheme should be MAJOR.MINOR.PATCH (should not contain the v prefix, otherwise the workflow won't run)
  • You should give packages access to the repo using the workflow (how to)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment