|
| 1 | +name: Auto-patch upstream runner tags |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + - cron: '0 6 * * *' # daily at 06:00 UTC |
| 6 | + workflow_dispatch: |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: write |
| 10 | + |
| 11 | +jobs: |
| 12 | + patch-and-tag: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + steps: |
| 15 | + - name: Checkout repo with full history and tags |
| 16 | + uses: actions/checkout@v4 |
| 17 | + with: |
| 18 | + fetch-depth: 0 |
| 19 | + fetch-tags: true |
| 20 | + token: ${{ secrets.RELEASE_PAT }} |
| 21 | + |
| 22 | + - name: Sync upstream tags |
| 23 | + run: | |
| 24 | + git remote add upstream https://github.com/actions/runner.git |
| 25 | + git fetch upstream 'refs/tags/*:refs/tags/upstream/*' |
| 26 | +
|
| 27 | + - name: Save patch assets |
| 28 | + run: | |
| 29 | + # Copy CacheEnvironmentHelper.cs source to RUNNER_TEMP so it survives detached checkouts |
| 30 | + cp CacheEnvironmentHelper.cs "${RUNNER_TEMP}/CacheEnvironmentHelper.cs" |
| 31 | + cp bitrise.yml "${RUNNER_TEMP}/bitrise.yml" |
| 32 | + cp .github/workflows/release.yml "${RUNNER_TEMP}/release.yml" |
| 33 | +
|
| 34 | + - name: Find and patch new tags |
| 35 | + run: | |
| 36 | + git config user.name "bitrise-bot" |
| 37 | + git config user.email "bot@bitrise.io" |
| 38 | +
|
| 39 | + # Collect all upstream v* tags |
| 40 | + UPSTREAM_TAGS=$(git tag -l 'upstream/v*' | sed 's|^upstream/||' | sort -V) |
| 41 | +
|
| 42 | + PATCHED=0 |
| 43 | + FAILED=0 |
| 44 | + SKIPPED=0 |
| 45 | +
|
| 46 | + for TAG in $UPSTREAM_TAGS; do |
| 47 | + PATCHED_TAG="${TAG}-bitrise" |
| 48 | +
|
| 49 | + # Check if we already have this patched tag |
| 50 | + if git rev-parse "refs/tags/${PATCHED_TAG}" >/dev/null 2>&1; then |
| 51 | + SKIPPED=$((SKIPPED + 1)) |
| 52 | + continue |
| 53 | + fi |
| 54 | +
|
| 55 | + echo "::group::Patching ${TAG} -> ${PATCHED_TAG}" |
| 56 | +
|
| 57 | + # Clean slate and checkout the upstream tag |
| 58 | + git reset --hard |
| 59 | + git clean -fd |
| 60 | + git checkout "upstream/${TAG}" --detach 2>&1 |
| 61 | +
|
| 62 | + HANDLER_DIR="src/Runner.Worker/Handlers" |
| 63 | + CONTAINER_FILE="${HANDLER_DIR}/ContainerActionHandler.cs" |
| 64 | + NODE_FILE="${HANDLER_DIR}/NodeScriptActionHandler.cs" |
| 65 | + HELPER_FILE="${HANDLER_DIR}/CacheEnvironmentHelper.cs" |
| 66 | + MARKER="CacheEnvironmentHelper.OverrideCacheEnvironment" |
| 67 | +
|
| 68 | + # Skip if the patch is already present (e.g. future upstream adoption) |
| 69 | + if grep -q "${MARKER}" "${CONTAINER_FILE}" 2>/dev/null; then |
| 70 | + echo "Patch already present in ${TAG} — skipping" |
| 71 | + SKIPPED=$((SKIPPED + 1)) |
| 72 | + echo "::endgroup::" |
| 73 | + continue |
| 74 | + fi |
| 75 | +
|
| 76 | + # Check that anchor patterns exist |
| 77 | + if ! grep -q 'foreach (var variable in this\.Environment)' "${CONTAINER_FILE}" 2>/dev/null || \ |
| 78 | + ! grep -q '// Resolve the target script' "${NODE_FILE}" 2>/dev/null; then |
| 79 | + echo "::warning::Anchor pattern not found in ${TAG} — skipping" |
| 80 | + FAILED=$((FAILED + 1)) |
| 81 | + echo "::endgroup::" |
| 82 | + continue |
| 83 | + fi |
| 84 | +
|
| 85 | + # 1. Create CacheEnvironmentHelper.cs from the patch file |
| 86 | + cp "${RUNNER_TEMP}/CacheEnvironmentHelper.cs" "${HELPER_FILE}" |
| 87 | + cp "${RUNNER_TEMP}/bitrise.yml" . |
| 88 | +
|
| 89 | + # 2. Insert cache override call into ContainerActionHandler.cs |
| 90 | + # Anchor: "foreach (var variable in this.Environment)" |
| 91 | + awk '/foreach \(var variable in this\.Environment\)/{ |
| 92 | + print " // Apply Cache URL overrides" |
| 93 | + print " CacheEnvironmentHelper.OverrideCacheEnvironment(Environment);" |
| 94 | + print "" |
| 95 | + }1' "${CONTAINER_FILE}" > tmp && mv tmp "${CONTAINER_FILE}" |
| 96 | +
|
| 97 | + # 3. Insert cache override call into NodeScriptActionHandler.cs |
| 98 | + # Anchor: "// Resolve the target script." |
| 99 | + awk '/\/\/ Resolve the target script\./{ |
| 100 | + print " // Apply Cache URL overrides" |
| 101 | + print " CacheEnvironmentHelper.OverrideCacheEnvironment(Environment);" |
| 102 | + print "" |
| 103 | + }1' "${NODE_FILE}" > tmp && mv tmp "${NODE_FILE}" |
| 104 | +
|
| 105 | + # 4. Replace .github/workflows/release.yml with our version |
| 106 | + cp "${RUNNER_TEMP}/release.yml" .github/workflows/release.yml |
| 107 | +
|
| 108 | + # 5. Remove upstream build workflow |
| 109 | + rm -f .github/workflows/build.yml |
| 110 | +
|
| 111 | + # 6. Commit, tag, push |
| 112 | + git add -A |
| 113 | + git commit -m "Bitrise cache integration (patched from ${TAG})" |
| 114 | +
|
| 115 | + git checkout -b "releases/${PATCHED_TAG}" |
| 116 | +
|
| 117 | + if git push origin "releases/${PATCHED_TAG}"; then |
| 118 | + echo "Successfully pushed to release/${PATCHED_TAG} branch" |
| 119 | + PATCHED=$((PATCHED + 1)) |
| 120 | + else |
| 121 | + echo "::warning::Push failed for ${PATCHED_TAG}" |
| 122 | + FAILED=$((FAILED + 1)) |
| 123 | + fi |
| 124 | +
|
| 125 | + echo "::endgroup::" |
| 126 | + done |
| 127 | +
|
| 128 | + echo "" |
| 129 | + echo "=== Summary ===" |
| 130 | + echo "Patched: ${PATCHED}" |
| 131 | + echo "Skipped (already exist): ${SKIPPED}" |
| 132 | + echo "Failed: ${FAILED}" |
| 133 | +
|
| 134 | + if [ "${FAILED}" -gt 0 ]; then |
| 135 | + echo "::warning::${FAILED} tag(s) failed to patch. Check the log above for details." |
| 136 | + fi |
0 commit comments