Skip to content

update-templates

update-templates #150

name: Update Templates
on:
workflow_dispatch:
inputs:
base_image:
description: 'Base image to update'
required: true
type: string
module_version:
description: 'Playwright or Puppeteer version'
required: false
type: string
default_runtime_version:
description: 'Default runtime version (example: 24 for node, 3.14 for python)'
required: false
type: string
repository_dispatch:
types:
- update-templates
env:
BASE_IMAGE_RAW: ${{ github.event.client_payload.base_image || inputs.base_image }}
RAW_BRANCH_NAME: ci/${{ github.event.client_payload.base_image || inputs.base_image }}
BASE_IMAGE: ${{ github.event.client_payload.base_image || inputs.base_image }}
DEFAULT_RUNTIME_VERSION: ${{ github.event.client_payload.default_runtime_version || inputs.default_runtime_version }}
MODULE_VERSION: ${{ github.event.client_payload.module_version || inputs.module_version }}
jobs:
update-templates:
runs-on: ubuntu-latest
steps:
- name: Sanitize base image
id: branch-name
run: |
echo "BRANCH_NAME=$(echo '${{ env.RAW_BRANCH_NAME }}' | sed 's/,/_/g')" >> $GITHUB_OUTPUT
echo "BASE_IMAGE_FORMATTED=$(echo '${{ env.BASE_IMAGE_RAW }}' | sed 's/,/, /g')" >> $GITHUB_OUTPUT
- name: Checkout repository
uses: actions/checkout@v6
with:
token: ${{ secrets.APIFY_SERVICE_ACCOUNT_GITHUB_TOKEN }}
fetch-depth: 0
- name: Configure Git
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Check and manage branch
run: |
# Fetch all branches
git fetch origin
# Check if branch exists on remote
if git ls-remote --heads origin "${{ steps.branch-name.outputs.BRANCH_NAME }}" | grep -q "${{ steps.branch-name.outputs.BRANCH_NAME }}"; then
echo "Branch ${{ steps.branch-name.outputs.BRANCH_NAME }} exists, checking it out and resetting to origin/master"
git checkout "${{ steps.branch-name.outputs.BRANCH_NAME }}"
git reset --hard origin/master
else
echo "Branch ${{ steps.branch-name.outputs.BRANCH_NAME }} does not exist, creating it"
git checkout -b "${{ steps.branch-name.outputs.BRANCH_NAME }}"
fi
- name: Setup Node.js
uses: actions/setup-node@v6
with:
node-version: 24
- name: Run update script
env:
BASE_IMAGE: ${{ env.BASE_IMAGE_RAW }}
DEFAULT_RUNTIME_VERSION: ${{ env.DEFAULT_RUNTIME_VERSION }}
MODULE_VERSION: ${{ env.MODULE_VERSION }}
run: node ./scripts/actions/update-templates.mts
- name: Commit and push changes
id: commit-and-push
env:
GH_TOKEN: ${{ secrets.APIFY_SERVICE_ACCOUNT_GITHUB_TOKEN }}
run: |
BRANCH="${{ steps.branch-name.outputs.BRANCH_NAME }}"
# Check if there are any changes
if git diff --quiet && git diff --cached --quiet; then
echo "No changes to commit"
else
git add -A
git commit -m "chore: Update templates for base image: ${{ env.BASE_IMAGE_RAW }}"
echo "committed=true" >> $GITHUB_OUTPUT
# Disable auto-merge on any existing PR before force-pushing,
# so the old commit can't be merged while we push the new one.
PR_NUMBER=$(gh pr list --head "$BRANCH" --base master --json number --jq '.[0].number // empty')
if [ -n "$PR_NUMBER" ]; then
gh pr merge "$PR_NUMBER" --disable-auto || true
fi
git push --force origin "$BRANCH"
fi
- name: Create or update Pull Request
env:
GH_TOKEN: ${{ secrets.APIFY_SERVICE_ACCOUNT_GITHUB_TOKEN }}
if: steps.commit-and-push.outputs.committed == 'true'
run: |
BRANCH="${{ steps.branch-name.outputs.BRANCH_NAME }}"
PR_BODY="Automated update of templates for base image \`${{ steps.branch-name.outputs.BASE_IMAGE_FORMATTED }}\`
**Parameters:**
- Base Image: \`${{ steps.branch-name.outputs.BASE_IMAGE_FORMATTED }}\`
- Runtime Version: \`${{ env.DEFAULT_RUNTIME_VERSION }}\`
- Module Version: \`${{ env.MODULE_VERSION || 'N/A' }}\`
> Generated by [Update Templates](https://github.com/apify/actor-templates/actions/workflows/update-templates.yml) workflow."
# Check if PR already exists
PR_NUMBER=$(gh pr list --head "$BRANCH" --base master --json number --jq '.[0].number // empty')
if [ -z "$PR_NUMBER" ]; then
echo "Creating new PR"
gh pr create \
--title "chore: Update templates for base image: ${{ steps.branch-name.outputs.BASE_IMAGE_FORMATTED }}" \
--body "$PR_BODY" \
--base master \
--head "$BRANCH" \
--reviewer B4nan \
--reviewer vladfrangu
# Retrieve PR number by head branch after creation
PR_NUMBER=$(gh pr list --head "$BRANCH" --base master --json number --jq '.[0].number // empty')
else
echo "PR #$PR_NUMBER already exists for branch $BRANCH, updating it"
gh pr edit "$PR_NUMBER" --body "$PR_BODY"
fi
# Validate that we have a PR number before proceeding
if [ -z "$PR_NUMBER" ]; then
echo "::error::Failed to determine pull request number for branch $BRANCH"
exit 1
fi
# Enable auto-merge so the PR merges automatically when CI passes.
# If CI fails, the PR stays open for manual review.
echo "Enabling auto-merge for PR #$PR_NUMBER"
gh pr merge "$PR_NUMBER" --auto --squash --delete-branch || echo "::warning::Failed to enable auto-merge. The PR remains open for manual review."