Build and Push Docker Images #34
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: Build and Push Docker Images | |
on: | |
release: | |
types: [published] | |
schedule: | |
# Run every day at midnight UTC | |
- cron: '0 0 * * *' | |
workflow_dispatch: | |
inputs: | |
build_all: | |
description: 'Build all apps with current versions' | |
type: boolean | |
default: false | |
required: false | |
specific_app: | |
description: 'Specific app to build (leave empty to process releases)' | |
type: string | |
required: false | |
jobs: | |
build-and-push: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
packages: write | |
steps: | |
- name: Checkout Docker configuration | |
uses: actions/checkout@v3 | |
with: | |
path: docker-config | |
- name: Checkout Saleor Apps | |
uses: actions/checkout@v3 | |
with: | |
repository: saleor/apps | |
fetch-depth: 0 | |
path: saleor-apps | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v2 | |
- name: Log in to GitHub Container Registry | |
uses: docker/login-action@v2 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Setup Node.js | |
uses: actions/setup-node@v3 | |
with: | |
node-version: '22' | |
- name: Get apps to build | |
id: get_apps | |
working-directory: saleor-apps | |
run: | | |
if [[ "${{ github.event_name }}" == "workflow_dispatch" && "${{ github.event.inputs.build_all }}" == "true" ]]; then | |
# Find all apps and their versions from package.json files | |
echo "Building all apps..." | |
for app_dir in apps/*; do | |
if [ -f "$app_dir/package.json" ]; then | |
app_name=$(node -p "require('./$app_dir/package.json').name") | |
version=$(node -p "require('./$app_dir/package.json').version") | |
echo "$app_name@$version" >> ../apps_to_build.txt | |
fi | |
done | |
elif [[ "${{ github.event_name }}" == "workflow_dispatch" && -n "${{ github.event.inputs.specific_app }}" ]]; then | |
# Build specific app | |
app_name="${{ github.event.inputs.specific_app }}" | |
if [ -f "apps/${app_name}/package.json" ]; then | |
version=$(node -p "require('./apps/${app_name}/package.json').version") | |
echo "$app_name@$version" >> ../apps_to_build.txt | |
else | |
echo "Error: App $app_name not found" | |
exit 1 | |
fi | |
else | |
# Get releases from GitHub API and filter only for actual apps | |
releases=$(curl -s "https://api.github.com/repos/saleor/apps/releases") | |
# Create a list of valid app names from the apps directory | |
echo "Getting valid app names..." | |
valid_apps=() | |
for app_dir in apps/*; do | |
if [ -f "$app_dir/package.json" ]; then | |
app_name=$(node -p "require('./$app_dir/package.json').name") | |
valid_apps+=("$app_name") | |
fi | |
done | |
# Filter releases to only include valid apps with exact matching | |
for app in "${valid_apps[@]}"; do | |
echo "$releases" | jq -r --arg app "$app" \ | |
'.[] | select(.tag_name | test("^" + $app + "@[0-9]+\\.[0-9]+\\.[0-9]+$")) | .tag_name' >> ../apps_to_build.txt | |
done | |
fi | |
echo "Apps to build:" | |
cat ../apps_to_build.txt | |
- name: Build and push Docker images | |
working-directory: docker-config | |
run: | | |
while IFS= read -r release; do | |
# Extract app name and version from release tag | |
app_name=$(echo $release | cut -d'@' -f1) | |
version=$(echo $release | cut -d'@' -f2) | |
# Get the app path (without app- prefix if present) | |
app_path=$app_name | |
[[ $app_name == app-* ]] && app_path=${app_name#app-} | |
# Check if image already exists | |
image_exists=false | |
if curl -s -f -L -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ | |
"https://ghcr.io/v2/trieb-work/saleor-apps/${app_name}/manifests/${version}" >/dev/null 2>&1; then | |
echo "Image ghcr.io/trieb-work/saleor-apps/${app_name}:${version} already exists, skipping..." | |
continue | |
fi | |
echo "Building $app_name version $version" | |
# Checkout the specific tag | |
cd ../saleor-apps | |
git checkout $app_name@$version | |
cd ../docker-config | |
# Build and push the Docker image | |
docker buildx build \ | |
--platform linux/amd64 \ | |
--push \ | |
--build-arg APP_NAME=$app_name \ | |
--build-arg APP_PATH=$app_path \ | |
-t ghcr.io/trieb-work/saleor-apps/${app_name}:${version} \ | |
-f Dockerfile \ | |
../saleor-apps | |
# Reset git state for next iteration | |
cd ../saleor-apps | |
git checkout main | |
cd ../docker-config | |
done < ../apps_to_build.txt |