Skip to content

Commit 4bdaef8

Browse files
author
bitrise-bot
committed
Bitrise cache integration (patched from v2.330.0)
1 parent e2088f1 commit 4bdaef8

5 files changed

Lines changed: 185 additions & 1 deletion

File tree

.github/workflows/release.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ jobs:
207207
env:
208208
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
209209
with:
210-
tag_name: "v${{ steps.releaseNote.outputs.version }}"
210+
tag_name: "v${{ steps.releaseNote.outputs.version }}-bitrise"
211211
release_name: "v${{ steps.releaseNote.outputs.version }}"
212212
body: |
213213
${{ steps.releaseNote.outputs.note }}
@@ -284,6 +284,7 @@ jobs:
284284
asset_content_type: application/octet-stream
285285

286286
publish-image:
287+
if: false
287288
needs: release
288289
runs-on: ubuntu-latest
289290
permissions:
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace GitHub.Runner.Worker.Handlers
5+
{
6+
/// <summary>
7+
/// Helper to override ACTIONS_CACHE_URL and ACTIONS_RESULTS_URL environment variables
8+
/// with values from the actual process environment (e.g. set by the cache proxy),
9+
/// while preserving the original values for downstream use.
10+
/// </summary>
11+
public static class CacheEnvironmentHelper
12+
{
13+
/// <summary>
14+
/// Saves the current values of ACTIONS_CACHE_URL and ACTIONS_RESULTS_URL into
15+
/// ACTIONS_CACHE_URL_ORIGINAL and ACTIONS_RESULTS_URL_ORIGINAL, then overrides
16+
/// them with values from the actual process environment variables.
17+
/// Also sets ACTIONS_CACHE_SERVICE_V2 to "true".
18+
/// </summary>
19+
public static void OverrideCacheEnvironment(Dictionary<string, string> environment)
20+
{
21+
// Save original values so the cache proxy (or other consumers) can reach the real backend
22+
if (environment.TryGetValue("ACTIONS_CACHE_URL", out var originalCacheUrl))
23+
{
24+
environment["ACTIONS_CACHE_URL_ORIGINAL"] = originalCacheUrl;
25+
}
26+
27+
if (environment.TryGetValue("ACTIONS_RESULTS_URL", out var originalResultsUrl))
28+
{
29+
environment["ACTIONS_RESULTS_URL_ORIGINAL"] = originalResultsUrl;
30+
}
31+
32+
// Override with values from the process environment (set by the cache proxy)
33+
var envCacheUrl = System.Environment.GetEnvironmentVariable("ACTIONS_CACHE_URL");
34+
if (!string.IsNullOrEmpty(envCacheUrl))
35+
{
36+
environment["ACTIONS_CACHE_URL"] = envCacheUrl;
37+
}
38+
39+
var envResultsUrl = System.Environment.GetEnvironmentVariable("ACTIONS_RESULTS_URL");
40+
if (!string.IsNullOrEmpty(envResultsUrl))
41+
{
42+
environment["ACTIONS_RESULTS_URL"] = envResultsUrl;
43+
}
44+
45+
// Enable cache service v2
46+
environment["ACTIONS_CACHE_SERVICE_V2"] = "true";
47+
}
48+
}
49+
}

src/Runner.Worker/Handlers/ContainerActionHandler.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,9 @@ public async Task RunAsync(ActionRunStage stage)
245245
Environment["ACTIONS_RESULTS_URL"] = resultsUrl;
246246
}
247247

248+
// Apply Cache URL overrides
249+
CacheEnvironmentHelper.OverrideCacheEnvironment(Environment);
250+
248251
foreach (var variable in this.Environment)
249252
{
250253
container.ContainerEnvironmentVariables[variable.Key] = container.TranslateToContainerPath(variable.Value);

src/Runner.Worker/Handlers/NodeScriptActionHandler.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ public async Task RunAsync(ActionRunStage stage)
7777
Environment["ACTIONS_CACHE_SERVICE_V2"] = bool.TrueString;
7878
}
7979

80+
// Apply Cache URL overrides
81+
CacheEnvironmentHelper.OverrideCacheEnvironment(Environment);
82+
8083
// Resolve the target script.
8184
string target = null;
8285
if (stage == ActionRunStage.Main)
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
---
2+
format_version: '23'
3+
default_step_lib_source: https://github.com/bitrise-io/bitrise-steplib.git
4+
project_type: other
5+
6+
app:
7+
envs:
8+
- R2_BUCKET: $R2_BUCKET
9+
- R2_ACCESS_KEY_ID: $R2_ACCESS_KEY_ID
10+
- R2_SECRET_ACCESS_KEY: $R2_SECRET_ACCESS_KEY
11+
- R2_ENDPOINT: $R2_ENDPOINT
12+
- GCS_BUCKET: $GCS_BUCKET
13+
- GCS_SERVICE_ACCOUNT_JSON: $GCS_SERVICE_ACCOUNT_JSON
14+
15+
trigger_map:
16+
- tag: v\d+.\d+.\d+-bitrise
17+
workflow: upload-release-assets
18+
19+
workflows:
20+
test:
21+
after_run:
22+
- upload-release-assets
23+
envs:
24+
- BITRISE_GIT_TAG: "v2.331.0-bitrise"
25+
upload-release-assets:
26+
envs:
27+
- TAG: $BITRISE_GIT_TAG
28+
steps:
29+
- script@1:
30+
title: Download GitHub release assets
31+
inputs:
32+
- content: |
33+
#!/usr/bin/env bash
34+
set -euo pipefail
35+
36+
echo "--- Fetching release for tag: ${TAG}"
37+
38+
RELEASE_JSON=$(curl -sf \
39+
-H "Accept: application/vnd.github+json" \
40+
"https://api.github.com/repos/bitrise-io/github-runner/releases/tags/${TAG}")
41+
42+
RELEASE_NAME=$(echo "${RELEASE_JSON}" | jq -r '.name')
43+
echo "Found release: ${RELEASE_NAME}"
44+
45+
ASSETS=$(echo "${RELEASE_JSON}" | jq -c '.assets[]')
46+
47+
if [ -z "${ASSETS}" ]; then
48+
echo "No assets found for release ${TAG}"
49+
exit 1
50+
fi
51+
52+
DOWNLOAD_DIR="${BITRISE_SOURCE_DIR}/release-assets"
53+
mkdir -p "${DOWNLOAD_DIR}"
54+
55+
echo "${ASSETS}" | while IFS= read -r asset; do
56+
NAME=$(echo "${asset}" | jq -r '.name')
57+
DOWNLOAD_URL=$(echo "${asset}" | jq -r '.url')
58+
59+
echo "Downloading: ${NAME}"
60+
curl -sfL \
61+
-H "Accept: application/octet-stream" \
62+
"${DOWNLOAD_URL}" \
63+
-o "${DOWNLOAD_DIR}/${NAME}"
64+
done
65+
66+
echo "Downloaded assets:"
67+
ls -lh "${DOWNLOAD_DIR}"
68+
69+
envman add --key RELEASE_ASSETS_DIR --value "${DOWNLOAD_DIR}"
70+
envman add --key RELEASE_TAG --value "${TAG}"
71+
72+
- script@1:
73+
title: Upload assets to R2
74+
run_if: "false"
75+
inputs:
76+
- content: |
77+
#!/usr/bin/env bash
78+
set -euo pipefail
79+
80+
echo "--- Configuring AWS CLI for R2"
81+
82+
export AWS_ACCESS_KEY_ID="${R2_ACCESS_KEY_ID}"
83+
export AWS_SECRET_ACCESS_KEY="${R2_SECRET_ACCESS_KEY}"
84+
export AWS_DEFAULT_REGION="auto"
85+
86+
R2_PREFIX="${RELEASE_TAG}"
87+
88+
echo "--- Uploading assets to s3://${R2_BUCKET}/${R2_PREFIX}/"
89+
90+
for file in "${RELEASE_ASSETS_DIR}"/*; do
91+
FILENAME=$(basename "${file}")
92+
echo "Uploading: ${FILENAME}"
93+
aws s3 cp "${file}" "s3://${R2_BUCKET}/${R2_PREFIX}/${FILENAME}" \
94+
--endpoint-url "${R2_ENDPOINT}"
95+
done
96+
97+
echo "--- Upload complete. Verifying:"
98+
aws s3 ls "s3://${R2_BUCKET}/${R2_PREFIX}/" \
99+
--endpoint-url "${R2_ENDPOINT}"
100+
101+
- script@1:
102+
title: Upload assets to GCS
103+
run_if: "false"
104+
inputs:
105+
- content: |
106+
#!/usr/bin/env bash
107+
set -euo pipefail
108+
109+
echo "--- Authenticating with GCP"
110+
111+
echo "${GCS_SERVICE_ACCOUNT_JSON}" > /tmp/gcs-key.json
112+
gcloud auth activate-service-account --key-file=/tmp/gcs-key.json
113+
114+
GCS_PREFIX="${RELEASE_TAG}"
115+
116+
echo "--- Uploading assets to gs://${GCS_BUCKET}/${GCS_PREFIX}/"
117+
118+
gsutil -m cp "${RELEASE_ASSETS_DIR}"/* "gs://${GCS_BUCKET}/${GCS_PREFIX}/"
119+
120+
echo "--- Upload complete. Verifying:"
121+
gsutil ls -l "gs://${GCS_BUCKET}/${GCS_PREFIX}/"
122+
123+
rm -f /tmp/gcs-key.json
124+
125+
meta:
126+
bitrise.io:
127+
stack: ubuntu-noble-24.04-bitrise-2025-base
128+
machine_type_id: g2.linux.medium

0 commit comments

Comments
 (0)