|
| 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 | + # Minimum upstream version to process — older tags are ignored |
| 40 | + MIN_TAG="v2.328.0" |
| 41 | +
|
| 42 | + # Collect upstream v* tags >= MIN_TAG, newest first |
| 43 | + UPSTREAM_TAGS=$(git tag -l 'upstream/v*' | sed 's|^upstream/||' | while read -r TAG; do |
| 44 | + if [ "$(printf '%s\n%s\n' "$MIN_TAG" "$TAG" | sort -V | head -n1)" = "$MIN_TAG" ]; then |
| 45 | + echo "$TAG" |
| 46 | + fi |
| 47 | + done | sort -rV) |
| 48 | +
|
| 49 | + MAX_PATCHES=1 |
| 50 | + PATCHED=0 |
| 51 | + FAILED=0 |
| 52 | + SKIPPED=0 |
| 53 | +
|
| 54 | + for TAG in $UPSTREAM_TAGS; do |
| 55 | + if [ "${PATCHED}" -ge "${MAX_PATCHES}" ]; then |
| 56 | + echo "Reached patch limit (${MAX_PATCHES}), stopping. Remaining tags will be patched in the next run." |
| 57 | + break |
| 58 | + fi |
| 59 | +
|
| 60 | + PATCHED_TAG="${TAG}-bitrise" |
| 61 | +
|
| 62 | + # Check if we already have a release branch for this tag |
| 63 | + if git ls-remote --exit-code origin "refs/heads/releases/${PATCHED_TAG}" >/dev/null 2>&1; then |
| 64 | + SKIPPED=$((SKIPPED + 1)) |
| 65 | + continue |
| 66 | + fi |
| 67 | +
|
| 68 | + echo "::group::Patching ${TAG} -> ${PATCHED_TAG}" |
| 69 | +
|
| 70 | + # Clean slate and checkout the upstream tag |
| 71 | + git reset --hard |
| 72 | + git clean -fd |
| 73 | + git checkout "upstream/${TAG}" --detach 2>&1 |
| 74 | +
|
| 75 | + HANDLER_DIR="src/Runner.Worker/Handlers" |
| 76 | + CONTAINER_FILE="${HANDLER_DIR}/ContainerActionHandler.cs" |
| 77 | + NODE_FILE="${HANDLER_DIR}/NodeScriptActionHandler.cs" |
| 78 | + HELPER_FILE="${HANDLER_DIR}/CacheEnvironmentHelper.cs" |
| 79 | + MARKER="CacheEnvironmentHelper.OverrideCacheEnvironment" |
| 80 | +
|
| 81 | + # Skip if the patch is already present (e.g. future upstream adoption) |
| 82 | + if grep -q "${MARKER}" "${CONTAINER_FILE}" 2>/dev/null; then |
| 83 | + echo "Patch already present in ${TAG} — skipping" |
| 84 | + SKIPPED=$((SKIPPED + 1)) |
| 85 | + echo "::endgroup::" |
| 86 | + continue |
| 87 | + fi |
| 88 | +
|
| 89 | + # Check that anchor patterns exist |
| 90 | + if ! grep -q 'foreach (var variable in this\.Environment)' "${CONTAINER_FILE}" 2>/dev/null || \ |
| 91 | + ! grep -q '// Resolve the target script' "${NODE_FILE}" 2>/dev/null; then |
| 92 | + echo "::warning::Anchor pattern not found in ${TAG} — skipping" |
| 93 | + FAILED=$((FAILED + 1)) |
| 94 | + echo "::endgroup::" |
| 95 | + continue |
| 96 | + fi |
| 97 | +
|
| 98 | + # 1. Create CacheEnvironmentHelper.cs from the patch file |
| 99 | + cp "${RUNNER_TEMP}/CacheEnvironmentHelper.cs" "${HELPER_FILE}" |
| 100 | + cp "${RUNNER_TEMP}/bitrise.yml" . |
| 101 | +
|
| 102 | + # 2. Insert cache override call into ContainerActionHandler.cs |
| 103 | + # Anchor: "foreach (var variable in this.Environment)" |
| 104 | + awk '/foreach \(var variable in this\.Environment\)/{ |
| 105 | + print " // Apply Cache URL overrides" |
| 106 | + print " CacheEnvironmentHelper.OverrideCacheEnvironment(Environment);" |
| 107 | + print "" |
| 108 | + }1' "${CONTAINER_FILE}" > tmp && mv tmp "${CONTAINER_FILE}" |
| 109 | +
|
| 110 | + # 3. Insert cache override call into NodeScriptActionHandler.cs |
| 111 | + # Anchor: "// Resolve the target script." |
| 112 | + awk '/\/\/ Resolve the target script\./{ |
| 113 | + print " // Apply Cache URL overrides" |
| 114 | + print " CacheEnvironmentHelper.OverrideCacheEnvironment(Environment);" |
| 115 | + print "" |
| 116 | + }1' "${NODE_FILE}" > tmp && mv tmp "${NODE_FILE}" |
| 117 | +
|
| 118 | + # 4. Replace .github/workflows/release.yml with our version |
| 119 | + cp "${RUNNER_TEMP}/release.yml" .github/workflows/release.yml |
| 120 | +
|
| 121 | + # 5. Remove upstream CI/CD workflows |
| 122 | + rm -f .github/workflows/build.yml |
| 123 | + rm -f .github/workflows/release.yml |
| 124 | +
|
| 125 | + # 6. Commit, tag, push |
| 126 | + git add -A |
| 127 | + git commit -m "Bitrise cache integration (patched from ${TAG})" |
| 128 | +
|
| 129 | + git checkout -b "releases/${PATCHED_TAG}" |
| 130 | +
|
| 131 | + if git push origin "releases/${PATCHED_TAG}"; then |
| 132 | + echo "Successfully pushed to release/${PATCHED_TAG} branch" |
| 133 | + PATCHED=$((PATCHED + 1)) |
| 134 | + else |
| 135 | + echo "::warning::Push failed for ${PATCHED_TAG}" |
| 136 | + FAILED=$((FAILED + 1)) |
| 137 | + fi |
| 138 | +
|
| 139 | + echo "::endgroup::" |
| 140 | + done |
| 141 | +
|
| 142 | + echo "" |
| 143 | + echo "=== Summary ===" |
| 144 | + echo "Patched: ${PATCHED}" |
| 145 | + echo "Skipped (already exist): ${SKIPPED}" |
| 146 | + echo "Failed: ${FAILED}" |
| 147 | +
|
| 148 | + if [ "${FAILED}" -gt 0 ]; then |
| 149 | + echo "::warning::${FAILED} tag(s) failed to patch. Check the log above for details." |
| 150 | + fi |
0 commit comments