|
| 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 | + |
| 21 | + - name: Sync upstream tags |
| 22 | + run: | |
| 23 | + git remote add upstream https://github.com/actions/runner.git |
| 24 | + git fetch upstream 'refs/tags/*:refs/tags/upstream/*' |
| 25 | +
|
| 26 | + - name: Save patch assets |
| 27 | + run: | |
| 28 | + # Save CacheEnvironmentHelper.cs to RUNNER_TEMP so it survives detached checkouts |
| 29 | + cat > "${RUNNER_TEMP}/CacheEnvironmentHelper.cs" << 'CSHARP' |
| 30 | + using System; |
| 31 | + using System.Collections.Generic; |
| 32 | +
|
| 33 | + namespace GitHub.Runner.Worker.Handlers |
| 34 | + { |
| 35 | + /// <summary> |
| 36 | + /// Helper to override ACTIONS_CACHE_URL and ACTIONS_RESULTS_URL environment variables |
| 37 | + /// with values from the actual process environment (e.g. set by the cache proxy), |
| 38 | + /// while preserving the original values for downstream use. |
| 39 | + /// </summary> |
| 40 | + public static class CacheEnvironmentHelper |
| 41 | + { |
| 42 | + /// <summary> |
| 43 | + /// Saves the current values of ACTIONS_CACHE_URL and ACTIONS_RESULTS_URL into |
| 44 | + /// ACTIONS_CACHE_URL_ORIGINAL and ACTIONS_RESULTS_URL_ORIGINAL, then overrides |
| 45 | + /// them with values from the actual process environment variables. |
| 46 | + /// Also sets ACTIONS_CACHE_SERVICE_V2 to "true". |
| 47 | + /// </summary> |
| 48 | + public static void OverrideCacheEnvironment(Dictionary<string, string> environment) |
| 49 | + { |
| 50 | + // Save original values so the cache proxy (or other consumers) can reach the real backend |
| 51 | + if (environment.TryGetValue("ACTIONS_CACHE_URL", out var originalCacheUrl)) |
| 52 | + { |
| 53 | + environment["ACTIONS_CACHE_URL_ORIGINAL"] = originalCacheUrl; |
| 54 | + } |
| 55 | +
|
| 56 | + if (environment.TryGetValue("ACTIONS_RESULTS_URL", out var originalResultsUrl)) |
| 57 | + { |
| 58 | + environment["ACTIONS_RESULTS_URL_ORIGINAL"] = originalResultsUrl; |
| 59 | + } |
| 60 | +
|
| 61 | + // Override with values from the process environment (set by the cache proxy) |
| 62 | + var envCacheUrl = System.Environment.GetEnvironmentVariable("ACTIONS_CACHE_URL"); |
| 63 | + if (!string.IsNullOrEmpty(envCacheUrl)) |
| 64 | + { |
| 65 | + environment["ACTIONS_CACHE_URL"] = envCacheUrl; |
| 66 | + } |
| 67 | +
|
| 68 | + var envResultsUrl = System.Environment.GetEnvironmentVariable("ACTIONS_RESULTS_URL"); |
| 69 | + if (!string.IsNullOrEmpty(envResultsUrl)) |
| 70 | + { |
| 71 | + environment["ACTIONS_RESULTS_URL"] = envResultsUrl; |
| 72 | + } |
| 73 | +
|
| 74 | + // Enable cache service v2 |
| 75 | + environment["ACTIONS_CACHE_SERVICE_V2"] = "true"; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + CSHARP |
| 80 | +
|
| 81 | + # Strip the leading 10-space YAML indentation from the heredoc |
| 82 | + sed -i 's/^ //' "${RUNNER_TEMP}/CacheEnvironmentHelper.cs" |
| 83 | +
|
| 84 | + - name: Find and patch new tags |
| 85 | + run: | |
| 86 | + git config user.name "bitrise-bot" |
| 87 | + git config user.email "bot@bitrise.io" |
| 88 | +
|
| 89 | + # Collect all upstream v* tags |
| 90 | + UPSTREAM_TAGS=$(git tag -l 'upstream/v*' | sed 's|^upstream/||' | sort -V) |
| 91 | +
|
| 92 | + PATCHED=0 |
| 93 | + FAILED=0 |
| 94 | + SKIPPED=0 |
| 95 | +
|
| 96 | + for TAG in $UPSTREAM_TAGS; do |
| 97 | + PATCHED_TAG="${TAG}-bitrise" |
| 98 | +
|
| 99 | + # Check if we already have this patched tag |
| 100 | + if git rev-parse "refs/tags/${PATCHED_TAG}" >/dev/null 2>&1; then |
| 101 | + SKIPPED=$((SKIPPED + 1)) |
| 102 | + continue |
| 103 | + fi |
| 104 | +
|
| 105 | + echo "::group::Patching ${TAG} -> ${PATCHED_TAG}" |
| 106 | +
|
| 107 | + # Clean slate and checkout the upstream tag |
| 108 | + git reset --hard |
| 109 | + git clean -fd |
| 110 | + git checkout "upstream/${TAG}" --detach 2>&1 |
| 111 | +
|
| 112 | + HANDLER_DIR="src/Runner.Worker/Handlers" |
| 113 | + CONTAINER_FILE="${HANDLER_DIR}/ContainerActionHandler.cs" |
| 114 | + NODE_FILE="${HANDLER_DIR}/NodeScriptActionHandler.cs" |
| 115 | + HELPER_FILE="${HANDLER_DIR}/CacheEnvironmentHelper.cs" |
| 116 | + MARKER="CacheEnvironmentHelper.OverrideCacheEnvironment" |
| 117 | +
|
| 118 | + # Skip if the patch is already present (e.g. future upstream adoption) |
| 119 | + if grep -q "${MARKER}" "${CONTAINER_FILE}" 2>/dev/null; then |
| 120 | + echo "Patch already present in ${TAG} — skipping" |
| 121 | + SKIPPED=$((SKIPPED + 1)) |
| 122 | + echo "::endgroup::" |
| 123 | + continue |
| 124 | + fi |
| 125 | +
|
| 126 | + # Check that anchor patterns exist |
| 127 | + if ! grep -q 'foreach (var variable in this\.Environment)' "${CONTAINER_FILE}" 2>/dev/null || \ |
| 128 | + ! grep -q '// Resolve the target script' "${NODE_FILE}" 2>/dev/null; then |
| 129 | + echo "::warning::Anchor pattern not found in ${TAG} — skipping" |
| 130 | + FAILED=$((FAILED + 1)) |
| 131 | + echo "::endgroup::" |
| 132 | + continue |
| 133 | + fi |
| 134 | +
|
| 135 | + # 1. Create CacheEnvironmentHelper.cs |
| 136 | + cp "${RUNNER_TEMP}/CacheEnvironmentHelper.cs" "${HELPER_FILE}" |
| 137 | +
|
| 138 | + # 2. Insert cache override call into ContainerActionHandler.cs |
| 139 | + # Anchor: "foreach (var variable in this.Environment)" |
| 140 | + awk '/foreach \(var variable in this\.Environment\)/{ |
| 141 | + print " // Apply Cache URL overrides" |
| 142 | + print " CacheEnvironmentHelper.OverrideCacheEnvironment(Environment);" |
| 143 | + print "" |
| 144 | + }1' "${CONTAINER_FILE}" > tmp && mv tmp "${CONTAINER_FILE}" |
| 145 | +
|
| 146 | + # 3. Insert cache override call into NodeScriptActionHandler.cs |
| 147 | + # Anchor: "// Resolve the target script." |
| 148 | + awk '/\/\/ Resolve the target script\./{ |
| 149 | + print " // Apply Cache URL overrides" |
| 150 | + print " CacheEnvironmentHelper.OverrideCacheEnvironment(Environment);" |
| 151 | + print "" |
| 152 | + }1' "${NODE_FILE}" > tmp && mv tmp "${NODE_FILE}" |
| 153 | +
|
| 154 | + # 4. Remove .github/ so push doesn't require workflows permission |
| 155 | + rm -rf .github/ |
| 156 | +
|
| 157 | + # 5. Commit, tag, push |
| 158 | + git add -A |
| 159 | + git commit -m "Bitrise cache integration (patched from ${TAG})" |
| 160 | +
|
| 161 | + git tag "${PATCHED_TAG}" |
| 162 | +
|
| 163 | + if git push origin "${PATCHED_TAG}"; then |
| 164 | + echo "Successfully tagged and pushed ${PATCHED_TAG}" |
| 165 | + PATCHED=$((PATCHED + 1)) |
| 166 | + else |
| 167 | + echo "::warning::Push failed for ${PATCHED_TAG}" |
| 168 | + FAILED=$((FAILED + 1)) |
| 169 | + fi |
| 170 | +
|
| 171 | + echo "::endgroup::" |
| 172 | + done |
| 173 | +
|
| 174 | + echo "" |
| 175 | + echo "=== Summary ===" |
| 176 | + echo "Patched: ${PATCHED}" |
| 177 | + echo "Skipped (already exist): ${SKIPPED}" |
| 178 | + echo "Failed: ${FAILED}" |
| 179 | +
|
| 180 | + if [ "${FAILED}" -gt 0 ]; then |
| 181 | + echo "::warning::${FAILED} tag(s) failed to patch. Check the log above for details." |
| 182 | + fi |
0 commit comments