Build & Push to Quay #106
This file contains hidden or 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: Build & Push to Quay | |
on: | |
workflow_dispatch: | |
release: | |
types: [published] | |
workflow_run: | |
workflows: ["Development"] | |
types: | |
- completed | |
jobs: | |
build-and-push: | |
# Only run if: | |
# * it’s a release publish | |
# * or a completed Development workflow for any branch succeeded | |
if: | | |
github.event_name == 'release' || | |
( | |
github.event_name == 'workflow_run' && | |
github.event.workflow_run.conclusion == 'success' | |
) | |
runs-on: ubuntu-latest | |
# Permissions needed for actions/checkout and actions/download-artifact (from other workflows) | |
permissions: | |
contents: read | |
actions: read # Required to download artifacts from other workflow runs | |
steps: | |
# Checkout repository: | |
# For 'release', github.ref is the tag (e.g., refs/tags/v1.0.0). | |
# For 'workflow_run', we checkout the specific commit that triggered the Development workflow. | |
- name: Checkout repository | |
uses: actions/checkout@v4 # Using v4 for consistency with your Development workflow | |
with: | |
ref: ${{ github.event_name == 'workflow_run' && github.event.workflow_run.head_sha || github.ref }} | |
# --- Prepare the .whl artifact by downloading from the preceding workflow_run --- | |
# Download artifact if triggered by a workflow_run (this includes the workflow_run that precedes a release) | |
- name: Download ESBMC-AI Wheel from Development workflow | |
if: github.event_name == 'workflow_run' # Download artifact for any successful workflow_run | |
uses: dawidd6/action-download-artifact@v9 | |
with: | |
name: build | |
path: dist | |
run_id: ${{ github.event.workflow_run.id }} | |
# --- Find the built/downloaded wheel file path --- | |
- name: Find the wheel file path | |
id: find_wheel # ID to reference output | |
run: | | |
# Find the latest .whl file in the dist directory | |
# Use ls -t to sort by modification time (newest first) and head -n 1 to get the top one. | |
# 2>/dev/null suppresses errors if no .whl files are found initially. | |
WHEEL_FILE=$(ls -t dist/*.whl 2>/dev/null | head -n 1) | |
# Check if a wheel file was found | |
if [ -z "$WHEEL_FILE" ]; then | |
echo "Error: No .whl file found in dist/." | |
# For a release event, the artifact *must* exist from the preceding workflow_run. | |
# For a workflow_run event, it should also exist if the 'Development' workflow uploaded it. | |
exit 1 | |
fi | |
echo "Found wheel file: $WHEEL_FILE" | |
# Set the output variable to the found path | |
echo "wheel_path=$WHEEL_FILE" >> $GITHUB_OUTPUT | |
# --- Docker build and push steps --- | |
- name: Log in to Quay.io | |
uses: docker/login-action@v2 # Log in to Quay.io registry | |
with: | |
registry: quay.io | |
username: ${{ secrets.QUAY_USERNAME }} | |
password: ${{ secrets.QUAY_PASSWORD }} | |
- name: Set Docker image tags | |
id: docker_tags | |
run: | # Script to set tags based on event and branch | |
REPO="quay.io/${{ secrets.QUAY_REPO }}" | |
TAGS="" | |
if [ "${{ github.event_name }}" == "release" ]; then | |
# Tag release builds with 'latest' and the release tag | |
TAGS="$REPO:latest,$REPO:${{ github.event.release.tag_name }}" | |
elif [ "${{ github.event_name }}" == "workflow_run" ]; then | |
BRANCH="${{ github.event.workflow_run.head_branch }}" | |
if [ "$BRANCH" == "master" ]; then | |
# Master branch workflow_run builds are tagged as 'latest' | |
TAGS="$REPO:latest" | |
else | |
# Other branch workflow_run builds are tagged with the branch name | |
# Sanitize branch name for use as a Docker tag | |
SANITIZED_BRANCH=$(echo "$BRANCH" | sed -e 's/[^a-zA-Z0-9._-]/-/g' | sed -e 's/^-*//' -e 's/-*$//') | |
# Also include the commit SHA for non-master workflow_run tags for better traceability | |
COMMIT_SHORT_SHA=$(echo "${{ github.event.workflow_run.head_sha }}" | cut -c1-7) | |
TAGS="$REPO:$SANITIZED_BRANCH-$COMMIT_SHORT_SHA" | |
fi | |
fi | |
echo "tags=$TAGS" >> $GITHUB_OUTPUT # Set the output variable | |
- name: Build & push Docker image | |
uses: docker/build-push-action@v6 | |
with: | |
context: . # The ./dist directory with the .whl will be in this context | |
file: Containerfile | |
push: true | |
tags: ${{ steps.docker_tags.outputs.tags }} | |
# Pass the wheel file path as a build argument | |
build-args: | | |
ESBMCAI_WHEEL=${{ steps.find_wheel.outputs.wheel_path }} |