Build and Push Docker Images #175
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 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: trieb-work/saleor-apps | |
fetch-depth: 0 | |
path: saleor-apps | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Log in to GitHub Container Registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Setup Node.js | |
uses: actions/setup-node@v4 | |
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 valid app names from the apps directory | |
echo "Getting valid app names..." | |
declare -A latest_releases | |
# Create a list of valid app names from the apps directory | |
for app_dir in apps/*; do | |
if [ -f "$app_dir/package.json" ]; then | |
app_name=$(node -p "require('./$app_dir/package.json').name") | |
echo "Found app: $app_name" | |
# Get the latest release for this app using GitHub API | |
latest_release=$(curl -s "https://api.github.com/repos/trieb-work/saleor-apps/releases" | \ | |
jq -r --arg app "$app_name" \ | |
'.[] | select(.tag_name | test("^" + $app + "@[0-9]+\\.[0-9]+\\.[0-9]+$")) | .tag_name' | \ | |
head -n 1) | |
if [ -n "$latest_release" ]; then | |
echo "Latest release for $app_name: $latest_release" | |
echo "$latest_release" >> ../apps_to_build.txt | |
else | |
echo "No release found for $app_name, using package.json version" | |
version=$(node -p "require('./$app_dir/package.json').version") | |
echo "$app_name@$version" >> ../apps_to_build.txt | |
fi | |
fi | |
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 saleor-app- or app- prefix if present) | |
app_path=$app_name | |
[[ $app_name == saleor-app-* ]] && app_path=${app_name#saleor-app-} | |
[[ $app_name == app-* ]] && app_path=${app_name#app-} | |
# Special case mapping for certain apps | |
[[ $app_name == "payment-stripe" ]] && app_path="stripe" | |
[[ $app_name == "saleor-app-payment-stripe" ]] && app_path="stripe" | |
# Check if image already exists | |
image_tag="ghcr.io/trieb-work/saleor-apps/${app_name}:${version}" | |
echo "Checking if image ${image_tag} exists..." | |
if docker manifest inspect "${image_tag}" > /dev/null 2>&1; then | |
echo "Image exists! Skipping build..." | |
continue | |
else | |
echo "Image does not exist, proceeding with build..." | |
fi | |
echo "Building $app_name version $version" | |
# Checkout the specific tag | |
cd ../saleor-apps | |
git checkout $app_name@$version | |
cd ../docker-config | |
# Copy the patch-next-config.sh script to the saleor-apps directory | |
cp patch-next-config.sh ../saleor-apps/ | |
# 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 |