Skip to content

Check for Upstream Releases #23

Check for Upstream Releases

Check for Upstream Releases #23

name: Check for Upstream Releases
on:
schedule:
- cron: '0 0 * * *'
workflow_dispatch:
inputs:
force_build:
description: 'Force build regardless of version check'
required: false
default: 'false'
jobs:
check-release:
runs-on: ubuntu-latest
permissions:
contents: read
packages: read
actions: write
outputs:
should_build: ${{ steps.check.outputs.should_build }}
new_version: ${{ steps.check.outputs.new_version }}
old_version: ${{ steps.check.outputs.old_version }}
steps:
- name: Get latest release info from upstream
id: upstream
run: |
RELEASE_JSON=$(curl -s -H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/anomalyco/opencode/releases/latest)
VERSION=$(echo "$RELEASE_JSON" | jq -r '.tag_name')
echo "Latest upstream version: $VERSION"
echo "version=$VERSION" >> $GITHUB_OUTPUT
- name: Check if we need to build
id: check
run: |
UPSTREAM_VERSION="${{ steps.upstream.outputs.version }}"
OWNER_LOWER=$(echo "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]')
PACKAGE_NAME=$(echo "${{ github.repository }}" | sed 's/.*\///' | tr '[:upper:]' '[:lower:]')
TAGS_RESPONSE=$(curl -s -H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/users/$OWNER_LOWER/packages/container/$PACKAGE_NAME/versions?per_page=100")
CURRENT_VERSION=$(echo "$TAGS_RESPONSE" | jq -r '.[].metadata.container.tags[]' 2>/dev/null | grep '^upstream-' | head -1 | sed 's/upstream-//')
echo "Current built version: $CURRENT_VERSION"
echo "Latest upstream version: $UPSTREAM_VERSION"
if [ "${{ github.event.inputs.force_build }}" == "true" ] || [ -z "$CURRENT_VERSION" ]; then
echo "should_build=true" >> $GITHUB_OUTPUT
echo "new_version=$UPSTREAM_VERSION" >> $GITHUB_OUTPUT
echo "old_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
echo "No previous version found or force build enabled. Will build."
exit 0
fi
if [ "$CURRENT_VERSION" != "$UPSTREAM_VERSION" ]; then
echo "should_build=true" >> $GITHUB_OUTPUT
echo "new_version=$UPSTREAM_VERSION" >> $GITHUB_OUTPUT
echo "old_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
echo "New version detected: $UPSTREAM_VERSION (was: $CURRENT_VERSION)"
else
echo "should_build=false" >> $GITHUB_OUTPUT
echo "new_version=$UPSTREAM_VERSION" >> $GITHUB_OUTPUT
echo "old_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
echo "No new version. Already at: $CURRENT_VERSION"
fi
trigger-build:
needs: check-release
if: needs.check-release.outputs.should_build == 'true'
runs-on: ubuntu-latest
permissions:
contents: write
actions: write
steps:
- name: Trigger Docker build workflow
uses: actions/github-script@v7
with:
script: |
const workflowId = 'docker-build.yml';
const response = await github.rest.actions.createWorkflowDispatch({
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: workflowId,
ref: 'main',
inputs: {
triggered_by: 'upstream-release',
upstream_version: '${{ needs.check-release.outputs.new_version }}',
previous_version: '${{ needs.check-release.outputs.old_version }}'
}
});
console.log('Triggered Docker build for version: ${{ needs.check-release.outputs.new_version }}');