Skip to content
This repository has been archived by the owner on Oct 13, 2023. It is now read-only.

Use GitHub Actions cache for installed binaries #5

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions action.yml
Expand Up @@ -16,10 +16,12 @@ inputs:
description: Use tool cache to speed up installation
required: false
default: false
use-cache:
flip1995 marked this conversation as resolved.
Show resolved Hide resolved
description: Store installed binary in the GitHub Actions cache (not used yet)
key:
description: Store installed binary in the GitHub Actions cache with the specified key
required: false
restore-keys:
description: Restore keys as used in the GitHub cache action
required: false
default: true

runs:
using: 'node12'
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

3,020 changes: 1,901 additions & 1,119 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Expand Up @@ -28,15 +28,15 @@
},
"homepage": "https://github.com/actions-rs/install",
"dependencies": {
"@actions-rs/core": "0.0.9",
"@actions-rs/core": "^0.1.5",
"@actions/core": "^1.2.3",
"@actions/exec": "^1.0.3",
"@actions/http-client": "^1.0.7",
"@actions/tool-cache": "^1.3.3"
"@actions/tool-cache": "^1.6.0"
},
"devDependencies": {
"@types/jest": "^25.2.1",
"@types/node": "^13.13.0",
"@types/node": "^13.13.15",
"@typescript-eslint/eslint-plugin": "^2.28.0",
"@typescript-eslint/parser": "^2.28.0",
"@zeit/ncc": "^0.22.1",
Expand All @@ -48,6 +48,6 @@
"npm-check-updates": "^4.1.2",
"prettier": "^2.0.4",
"ts-jest": "^25.4.0",
"typescript": "^3.8.3"
"typescript": "^3.9.7"
}
}
16 changes: 1 addition & 15 deletions src/download.ts
Expand Up @@ -2,10 +2,10 @@ import os from "os";
import { promises as fs } from "fs";
import path from "path";

import { resolveVersion } from "@actions-rs/core";
import * as core from "@actions/core";
import * as exec from "@actions/exec";
import * as tc from "@actions/tool-cache";
import * as http from "@actions/http-client";

const CLOUDFRONT_ROOT = "https://d1ad61wkrfbmp3.cloudfront.net";
// Path to the public key of the sign certificate.
Expand All @@ -32,20 +32,6 @@ function getRunner(): string {
}
}

async function resolveVersion(crate: string): Promise<string> {
flip1995 marked this conversation as resolved.
Show resolved Hide resolved
const url = `https://crates.io/api/v1/crates/${crate}`;
const client = new http.HttpClient(
"@actions-rs (https://github.com/actions-rs/)"
);

const resp: any = await client.getJson(url);
if (resp.result == null) {
throw new Error("Unable to fetch latest crate version");
}

return resp.result["crate"]["newest_version"];
}

function buildUrl(crate: string, version: string): string {
const runner = getRunner();

Expand Down
9 changes: 6 additions & 3 deletions src/input.ts
Expand Up @@ -9,19 +9,22 @@ export interface Input {
crate: string;
version: string;
useToolCache: boolean;
useCache: boolean;
primaryKey?: string;
restoreKeys?: string[];
}

export function get(): Input {
const crate = input.getInput("crate", { required: true });
const version = input.getInput("version", { required: true });
const useToolCache = input.getInputBool("use-tool-cache") || false;
const useCache = input.getInputBool("use-cache") || true;
const primaryKey = input.getInput("key") || undefined;
const restoreKeys = input.getInputAsArray("restore-keys") || undefined;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it is a good idea to force users to provide all necessary cache keys, as Action can do that by itself; grab the crate name, version and current runner name (ex. ubuntu-18.04), join them together - we got a cache key.
That also mean that we will need to use resolveVersion if there is no version specifically set in the Action arguments or set to "latest", for example.

Ideally, from the user side I would expect to write

- uses: actions-rs/install@v0.2
  with:
    crate: cargo-make

and it will handle all the stuff by itself.

Copy link
Author

@flip1995 flip1995 Aug 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thought behind adding those keys was that there is currently no way to force-update a cache. If a new cache should be created, the cache has to get a new name. With actions/cache in rust projects, this is for example done by hashing the Cargo.lock files.

But when caching binaries I guess only naming the hash with the version number should do the trick. There should be no need to add a hash to the cache name.

Should I remove the keys completely or should I just make them optional?


The part with resolving the version is already implemented. But I still have to add the runner name.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@svartalf Do you have a suggestion how I might get the name of the runner? I looked at the @actions/github and @actions/core package, but couldn't find any functions with which I'm able to access the runner name.

If we're able to get the runner name $somehow, we can get rid of the key/restoreKeys arguments of the action and go back to useCache. This change has to be implemented in actions-rs/core though.


return {
crate: crate,
version: version,
useToolCache: useToolCache,
useCache: useCache,
primaryKey: primaryKey,
restoreKeys: restoreKeys,
};
}
13 changes: 10 additions & 3 deletions src/main.ts
Expand Up @@ -6,7 +6,8 @@ import * as download from "./download";

interface Options {
useToolCache: boolean;
useCache: boolean;
primaryKey?: string;
restoreKeys?: string[];
}

async function downloadFromToolCache(
Expand Down Expand Up @@ -52,7 +53,12 @@ export async function run(
} catch (error) {
core.info("Falling back to the `cargo install` command");
const cargo = await Cargo.get();
await cargo.installCached(crate, version);
await cargo.installCached(
crate,
version,
options.primaryKey,
options.restoreKeys
);
}
}

Expand All @@ -62,7 +68,8 @@ async function main(): Promise<void> {

await run(actionInput.crate, actionInput.version, {
useToolCache: actionInput.useToolCache,
useCache: actionInput.useCache,
primaryKey: actionInput.primaryKey,
restoreKeys: actionInput.restoreKeys,
});
} catch (error) {
core.setFailed(error.message);
Expand Down