Create and publish Docker images #674
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Create and publish Docker images | |
on: | |
release: | |
types: | |
- published | |
workflow_dispatch: | |
env: | |
REGISTRY: ghcr.io | |
IMAGE_NAME: ${{ github.repository }} | |
jobs: | |
build-and-push-image: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
packages: write | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 # Ensures we get all history for tag retrieval | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v2 | |
- name: Log in to the Container registry | |
uses: docker/login-action@v2 | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Extract metadata (tags, labels) for Docker | |
id: meta | |
uses: docker/metadata-action@v4 | |
with: | |
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
- name: Get lowercase repo name | |
id: get_lowercase_repo_name | |
run: | | |
REPO_NAME=${{ env.IMAGE_NAME }} | |
echo "LOWER_CASE_REPO_NAME=${REPO_NAME,,}" >> $GITHUB_ENV | |
- name: Get release version and notes (only for release events) | |
if: github.event_name == 'release' | |
id: get_release_info | |
run: | | |
RELEASE_VERSION="${{ github.event.release.tag_name }}" | |
if [ -z "$RELEASE_VERSION" ]; then | |
echo "Error: RELEASE_VERSION is empty. Ensure a valid release tag." | |
exit 1 | |
fi | |
echo "VERSION=$RELEASE_VERSION" >> $GITHUB_ENV | |
# Correct way to store multi-line release notes in GITHUB_ENV | |
{ | |
echo "RELEASE_NOTES<<EOF" | |
echo "${{ github.event.release.body }}" | |
echo "EOF" | |
} >> $GITHUB_ENV | |
- name: Update version.py (only on release) | |
if: github.event_name == 'release' | |
run: | | |
TIMESTAMP=$(date +"%Y-%m-%d") | |
VERSION_FILE="data/version.py" | |
# Create version.py if it doesn't exist | |
if [ ! -f "$VERSION_FILE" ]; then | |
echo "__version__ = \"0.0.0\"" > "$VERSION_FILE" | |
echo "" >> "$VERSION_FILE" | |
fi | |
# Prepend new release info | |
TEMP_FILE=$(mktemp) | |
echo "__version__ = \"${{ env.VERSION }}\"" > "$TEMP_FILE" | |
echo "" >> "$TEMP_FILE" | |
echo "\"\"\"" >> "$TEMP_FILE" | |
echo "Changelog for version ${{ env.VERSION }} ($TIMESTAMP):" >> "$TEMP_FILE" | |
echo "" >> "$TEMP_FILE" | |
echo "${{ env.RELEASE_NOTES }}" >> "$TEMP_FILE" | |
echo "\"\"\"" >> "$TEMP_FILE" | |
echo "" >> "$TEMP_FILE" | |
cat "$VERSION_FILE" >> "$TEMP_FILE" | |
mv "$TEMP_FILE" "$VERSION_FILE" | |
- name: Commit and push version update (only on release) | |
if: github.event_name == 'release' | |
run: | | |
git checkout master # Ensure we're on master branch | |
git config --global user.name "github-actions[bot]" | |
git config --global user.email "github-actions[bot]@users.noreply.github.com" | |
git add data/version.py | |
git commit -m "Update version.py to ${{ env.VERSION }} with changelog" | |
git push origin master | |
- name: Build and push Docker image | |
uses: docker/build-push-action@v3 | |
with: | |
context: . | |
push: true | |
tags: ${{ env.REGISTRY }}/${{ env.LOWER_CASE_REPO_NAME }}:${{ env.VERSION }}, ${{ steps.meta.outputs.tags }} | |
labels: ${{ steps.meta.outputs.labels }} | |
cache-from: type=gha | |
cache-to: type=gha,mode=max |