Skip to content

Commit

Permalink
Merge pull request #23 from t0yv0/master
Browse files Browse the repository at this point in the history
  • Loading branch information
jaxxstorm authored May 19, 2022
2 parents bcf3b80 + 7f3add2 commit cceeb32
Show file tree
Hide file tree
Showing 1,990 changed files with 339,147 additions and 68 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,28 @@ jobs:
arch: x86-64
```
### Caching
This action can use [actions/cache](https://github.com/actions/cache) under the hood. Caching needs to be enabled explicitly and only works for specific tags.
```yaml
# ...
jobs:
my_job:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Github token scoped to job
steps:
- name: Install tf2pulumi
uses: jaxxstorm/[email protected]
with: # Grab a specific tag with caching
repo: pulumi/tf2pulumi
tag: v0.7.0
cache: enable
```
Caching helps avoid
[Rate limiting](https://docs.github.com/en/rest/overview/resources-in-the-rest-api#requests-from-github-actions), since this action does not need to scan tags and releases on a cache hit. Caching currently is not expected to speed up installation.
## Finding a release
By default, this action will lookup the Platform and Architecture of the runner and use those values to interpolate and match a release package. **The release package name is first converted to lowercase**. The match pattern is:
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ inputs:
arch:
description: "OS Architecture to match in release package. Specify this parameter if the repository releases do not follow a normal convention otherwise it will be auto-detected."
required: false
cache:
description: "When set to 'enable', caches the downloads of known tags with actions/cache"
required: false
branding:
icon: "archive"
color: "green"
Expand Down
62 changes: 55 additions & 7 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
Expand All @@ -14,7 +18,7 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Expand All @@ -28,10 +32,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const os = __importStar(require("os"));
const path = __importStar(require("path"));
const cache = __importStar(require("@actions/cache"));
const core = __importStar(require("@actions/core"));
const tc = __importStar(require("@actions/tool-cache"));
const utils_1 = require("@actions/github/lib/utils");
const os = __importStar(require("os"));
const plugin_throttling_1 = require("@octokit/plugin-throttling");
const ThrottlingOctokit = utils_1.GitHub.plugin(plugin_throttling_1.throttling);
function run() {
Expand All @@ -53,7 +59,7 @@ function run() {
core.info(`Retrying after ${retryAfter} seconds.`);
return true;
},
} }, utils_1.getOctokitOptions(token)));
} }, (0, utils_1.getOctokitOptions)(token)));
const repo = core.getInput("repo");
if (!repo) {
throw new Error(`Repo was not specified`);
Expand All @@ -62,6 +68,9 @@ function run() {
if (!tag) {
throw new Error(`Tag not specified`);
}
const cacheEnabled = (core.getInput("cache") === "enable")
&& tag !== "latest"
&& tag !== "";
const [owner, project] = repo.split("/");
let osMatch = [];
// Determine Platform
Expand Down Expand Up @@ -103,6 +112,24 @@ function run() {
}
core.info(`==> System reported arch: ${os.arch()}`);
core.info(`==> Using arch: ${osArch}`);
let toolInfo = {
owner: owner,
project: project,
tag: tag,
osArch: osArch,
osPlatform: osPlatform
};
let dest = toolPath(toolInfo);
// Look in the cache first.
let cacheKey = cachePrimaryKey(toolInfo);
if (cacheEnabled && cacheKey !== undefined) {
let ok = yield cache.restoreCache([dest], cacheKey);
if (ok !== undefined) {
core.info(`Found ${project} in the cache: ${dest}`);
core.addPath(dest);
return;
}
}
let getReleaseUrl;
if (tag === "latest") {
getReleaseUrl = yield octokit.rest.repos.getLatestRelease({
Expand Down Expand Up @@ -132,9 +159,12 @@ function run() {
const url = asset.browser_download_url;
core.info(`Downloading ${project} from ${url}`);
const binPath = yield tc.downloadTool(url);
const extractedPath = yield extractFn(binPath);
core.info(`Successfully extracted ${project} to ${extractedPath}`);
core.addPath(extractedPath);
yield extractFn(binPath, dest);
if (cacheEnabled && cacheKey !== undefined) {
yield cache.saveCache([dest], cacheKey);
}
core.addPath(dest);
core.info(`Successfully extracted ${project} to ${dest}`);
}
catch (error) {
if (error instanceof Error) {
Expand All @@ -146,6 +176,24 @@ function run() {
}
});
}
function cachePrimaryKey(info) {
// Currently not caching "latest" verisons of the tool.
if (info.tag === "latest") {
return undefined;
}
return "action-install-gh-release/" +
`${info.owner}/${info.project}/${info.tag}/${info.osPlatform}-${info.osArch}`;
}
function toolPath(info) {
return path.join(getCacheDirectory(), info.owner, info.project, info.tag, `${info.osPlatform}-${info.osArch}`);
}
function getCacheDirectory() {
const cacheDirectory = process.env['RUNNER_TOOL_CACHE'] || '';
if (cacheDirectory === '') {
core.warning('Expected RUNNER_TOOL_CACHE to be defined');
}
return cacheDirectory;
}
function getExtractFn(assetName) {
if (assetName.endsWith('.tar.gz')) {
return tc.extractTar;
Expand Down
File renamed without changes.
Loading

0 comments on commit cceeb32

Please sign in to comment.