|
| 1 | +name: Check for Upstream Releases |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + # Check for new releases once a day at midnight UTC |
| 6 | + - cron: '0 0 * * *' |
| 7 | + workflow_dispatch: |
| 8 | + inputs: |
| 9 | + force_build: |
| 10 | + description: 'Force build regardless of version check' |
| 11 | + required: false |
| 12 | + default: 'false' |
| 13 | + |
| 14 | +jobs: |
| 15 | + check-release: |
| 16 | + runs-on: ubuntu-latest |
| 17 | + outputs: |
| 18 | + should_build: ${{ steps.check.outputs.should_build }} |
| 19 | + new_version: ${{ steps.check.outputs.new_version }} |
| 20 | + old_version: ${{ steps.check.outputs.old_version }} |
| 21 | + steps: |
| 22 | + - name: Get latest release info from upstream |
| 23 | + id: upstream |
| 24 | + run: | |
| 25 | + # Fetch latest release from anomalyco/opencode |
| 26 | + RELEASE_JSON=$(curl -s -H "Accept: application/vnd.github+json" \ |
| 27 | + -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ |
| 28 | + -H "X-GitHub-Api-Version: 2022-11-28" \ |
| 29 | + https://api.github.com/repos/anomalyco/opencode/releases/latest) |
| 30 | + |
| 31 | + VERSION=$(echo "$RELEASE_JSON" | jq -r '.tag_name') |
| 32 | + echo "Latest upstream version: $VERSION" |
| 33 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 34 | +
|
| 35 | + - name: Check if we need to build |
| 36 | + id: check |
| 37 | + run: | |
| 38 | + CURRENT_VERSION="" |
| 39 | + |
| 40 | + # Try to get the last built version from git tags |
| 41 | + if git rev-parse last-built-version >/dev/null 2>&1; then |
| 42 | + CURRENT_VERSION=$(git rev-parse last-built-version) |
| 43 | + fi |
| 44 | + |
| 45 | + # Also check if there's a recent Docker image tag |
| 46 | + if [ -z "$CURRENT_VERSION" ]; then |
| 47 | + # Try GitHub API to get existing image tags |
| 48 | + TAGS_JSON=$(curl -s -H "Accept: application/vnd.github+json" \ |
| 49 | + -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ |
| 50 | + -H "X-GitHub-Api-Version: 2022-11-28" \ |
| 51 | + https://api.github.com/repos/${{ github.repository }}/git/refs/tags) |
| 52 | + |
| 53 | + # Get the most recent tag that matches anomalyco version pattern |
| 54 | + CURRENT_VERSION=$(echo "$TAGS_JSON" | jq -r '.[] | select(.ref | startswith("refs/tags/anomalyco-")) | .ref' 2>/dev/null | head -1 | sed 's/refs\/tags\/anomalyco-//') |
| 55 | + fi |
| 56 | + |
| 57 | + # For manual dispatch or if no previous version, always build |
| 58 | + if [ "${{ github.event.inputs.force_build }}" == "true" ] || [ -z "$CURRENT_VERSION" ]; then |
| 59 | + echo "should_build=true" >> $GITHUB_OUTPUT |
| 60 | + echo "new_version=${{ steps.upstream.outputs.version }}" >> $GITHUB_OUTPUT |
| 61 | + echo "old_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT |
| 62 | + echo "No previous version found or force build enabled. Will build." |
| 63 | + exit 0 |
| 64 | + fi |
| 65 | + |
| 66 | + # Compare versions |
| 67 | + if [ "$CURRENT_VERSION" != "${{ steps.upstream.outputs.version }}" ]; then |
| 68 | + echo "should_build=true" >> $GITHUB_OUTPUT |
| 69 | + echo "new_version=${{ steps.upstream.outputs.version }}" >> $GITHUB_OUTPUT |
| 70 | + echo "old_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT |
| 71 | + echo "New version detected: ${{ steps.upstream.outputs.version }} (was: $CURRENT_VERSION)" |
| 72 | + else |
| 73 | + echo "should_build=false" >> $GITHUB_OUTPUT |
| 74 | + echo "new_version=${{ steps.upstream.outputs.version }}" >> $GITHUB_OUTPUT |
| 75 | + echo "old_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT |
| 76 | + echo "No new version. Already at: $CURRENT_VERSION" |
| 77 | + fi |
| 78 | +
|
| 79 | + trigger-build: |
| 80 | + needs: check-release |
| 81 | + if: needs.check-release.outputs.should_build == 'true' |
| 82 | + runs-on: ubuntu-latest |
| 83 | + steps: |
| 84 | + - name: Trigger Docker build workflow |
| 85 | + uses: actions/github-script@v7 |
| 86 | + with: |
| 87 | + script: | |
| 88 | + const workflowId = 'docker-build.yml'; |
| 89 | + |
| 90 | + // Get the workflow run URL |
| 91 | + const response = await github.rest.actions.createWorkflowDispatch({ |
| 92 | + owner: context.repo.owner, |
| 93 | + repo: context.repo.repo, |
| 94 | + workflow_id: workflowId, |
| 95 | + ref: 'main', |
| 96 | + inputs: { |
| 97 | + triggered_by: 'upstream-release', |
| 98 | + upstream_version: '${{ needs.check-release.outputs.new_version }}', |
| 99 | + previous_version: '${{ needs.check-release.outputs.old_version }}' |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + console.log('Triggered Docker build for version: ${{ needs.check-release.outputs.new_version }}'); |
0 commit comments