Skip to content

Commit d30148c

Browse files
committed
Add a composite wrapper to get action metadata
1 parent 0e040fb commit d30148c

3 files changed

Lines changed: 44 additions & 37 deletions

File tree

‎action.yaml‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@ inputs:
77
description: "The authentication token used to push store paths"
88
required: true
99
github-token:
10-
description: "Optional GitHub token used to resolve tags for commit-pinned action refs"
10+
description: "GitHub token used to download release artifacts from private repos"
1111
required: false
1212

1313
runs:
14-
using: "node24"
15-
main: "js/main.js"
16-
post: "js/post.js"
14+
using: "composite"
15+
steps:
16+
- uses: ./js
17+
with:
18+
auth-token: ${{ inputs.auth-token }}
19+
github-token: ${{ inputs.github-token }}
20+
env:
21+
GITHUB_ACTION_REF: ${{ github.action_ref }}
22+
GITHUB_ACTION_REPOSITORY: ${{ github.action_repository }}

‎js/action.yaml‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: "cache action runtime"
2+
3+
inputs:
4+
auth-token:
5+
required: true
6+
github-token:
7+
required: false
8+
9+
runs:
10+
using: "node24"
11+
main: "main.js"
12+
post: "post.js"

‎js/main.js‎

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,7 @@ const requiredEnv = (name) => {
2828
return value;
2929
};
3030

31-
/**
32-
* @param {string} name
33-
* @returns {string}
34-
*/
35-
const envValueOrUnset = (name) => {
36-
return process.env[name]?.trim() || "<unset>";
37-
};
3831

39-
const logActionSourceEnv = () => {
40-
core.info(
41-
`GITHUB_ACTION_REPOSITORY=${envValueOrUnset("GITHUB_ACTION_REPOSITORY")}`,
42-
);
43-
core.info(`GITHUB_ACTION_REF=${envValueOrUnset("GITHUB_ACTION_REF")}`);
44-
};
4532

4633
/**
4734
* @returns {string}
@@ -152,32 +139,36 @@ const releaseTag = async (actionRepository, actionRef, githubToken) => {
152139
* @returns {Promise<{ url: string, headers: Record<string, string> }>}
153140
*/
154141
const releaseAssetRequest = async (assetName, githubToken) => {
155-
// const actionRepository = requiredEnv("GITHUB_ACTION_REPOSITORY");
156-
// const actionRef = requiredEnv("GITHUB_ACTION_REF");
157-
const actionRepository = "sub60/cache-action";
158-
const actionRef = "v0.0.2";
159-
const tag = await releaseTag(actionRepository, actionRef, githubToken);
142+
const actionRepository =
143+
process.env.GITHUB_ACTION_REPOSITORY?.trim() || "sub60/cache-action";
144+
145+
// TODO: remove the fallback once the repo is public and local checkouts are
146+
// no longer needed.
147+
const actionRef = process.env.GITHUB_ACTION_REF?.trim();
160148

161149
if (!githubToken) {
162-
return {
163-
url: `https://github.com/${actionRepository}/releases/download/${tag}/${assetName}`,
164-
headers: {},
165-
};
150+
const base = `https://github.com/${actionRepository}/releases`;
151+
const url = actionRef
152+
? `${base}/download/${await releaseTag(actionRepository, actionRef, githubToken)}/${assetName}`
153+
: `${base}/latest/download/${assetName}`;
154+
155+
return { url, headers: {} };
166156
}
167157

168158
const apiUrl = requiredEnv("GITHUB_API_URL");
169159

170-
const response = await fetch(
171-
`${apiUrl}/repos/${actionRepository}/releases/tags/${encodeURIComponent(tag)}`,
172-
{
173-
headers: githubApiHeaders(githubToken),
174-
redirect: "follow",
175-
},
176-
);
160+
const releaseUrl = actionRef
161+
? `${apiUrl}/repos/${actionRepository}/releases/tags/${encodeURIComponent(await releaseTag(actionRepository, actionRef, githubToken))}`
162+
: `${apiUrl}/repos/${actionRepository}/releases/latest`;
163+
164+
const response = await fetch(releaseUrl, {
165+
headers: githubApiHeaders(githubToken),
166+
redirect: "follow",
167+
});
177168

178169
if (!response.ok) {
179170
throw new Error(
180-
`Failed to resolve release '${tag}' in '${actionRepository}': ${response.status} ${response.statusText}`,
171+
`Failed to resolve release in '${actionRepository}': ${response.status} ${response.statusText}`,
181172
);
182173
}
183174

@@ -186,7 +177,7 @@ const releaseAssetRequest = async (assetName, githubToken) => {
186177

187178
if (!asset) {
188179
throw new Error(
189-
`Release '${tag}' in '${actionRepository}' does not contain asset '${assetName}'`,
180+
`Release '${release.tag_name}' in '${actionRepository}' does not contain asset '${assetName}'`,
190181
);
191182
}
192183

@@ -332,8 +323,6 @@ const main = async () => {
332323
const configPath = path.join(daemonDir, "nix.conf");
333324
const assetName = `${binaryName}-${target}`;
334325

335-
logActionSourceEnv();
336-
337326
const { url, headers } = await releaseAssetRequest(assetName, githubToken);
338327

339328
core.info(`Downloading daemon '${assetName}' from '${url}'`);

0 commit comments

Comments
 (0)