Skip to content

Commit a89365e

Browse files
authored
restore.ts - update-package-index option (#386)
add update-package-index option
1 parent 04bc35a commit a89365e

File tree

4 files changed

+112
-25
lines changed

4 files changed

+112
-25
lines changed

.github/workflows/tests.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,27 @@ jobs:
263263
uses: ./
264264
with:
265265
evict-old-files: ${{ matrix.evict }}
266+
267+
test_option_update:
268+
runs-on: ubuntu-latest
269+
container: ubuntu:latest
270+
steps:
271+
- uses: actions/checkout@v5
272+
- name: Run ccache-action
273+
uses: ./
274+
with:
275+
update-package-index: true
276+
277+
test_option_update_breaks:
278+
# Needed to ensure `test_option_update` actually does something.
279+
runs-on: ubuntu-latest
280+
container: ubuntu:latest
281+
steps:
282+
- uses: actions/checkout@v5
283+
- name: Run ccache-action
284+
id: ccache
285+
uses: ./
286+
continue-on-error: true
287+
- name: Fail if previous step succeeded without update
288+
if: steps.ccache.outcome == 'success'
289+
run: exit 1

action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ inputs:
4141
followed by the 's' or 'd' suffix respectively. Also supports the special value 'job' which represents the time
4242
since the job started, which evicts all cache files that were not touched during the job run."
4343
default: ''
44+
update-package-index:
45+
description: "Update package manager index before installing ccache. Enabling this might help when running
46+
ccache-action in a container or from `act`. Disabling this might speed up the action execution."
47+
default: false
48+
required: false
4449
runs:
4550
using: "node20"
4651
main: "dist/restore/index.js"

dist/restore/index.js

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66428,6 +66428,11 @@ function cacheDir(ccacheVariant) {
6642866428

6642966429

6643066430
const SELF_CI = external_process_namespaceObject.env["CCACHE_ACTION_CI"] === "true";
66431+
function getPackageManagerError(error) {
66432+
return (`Failed to install ccache via package manager: '${error}'. ` +
66433+
"Perhaps package manager index is not up to date? " +
66434+
"(either update it manually before running ccache-action or set 'update-package-index' option to 'true')");
66435+
}
6643166436
// based on https://cristianadam.eu/20200113/speeding-up-c-plus-plus-github-actions-using-ccache/
6643266437
async function restore(ccacheVariant) {
6643366438
const inputs = {
@@ -66490,22 +66495,45 @@ async function configure(ccacheVariant, platform) {
6649066495
}
6649166496
}
6649266497
async function installCcacheMac() {
66493-
await execShell("brew install ccache");
66498+
if (lib_core.getBooleanInput("update-package-index")) {
66499+
await execShell("brew update");
66500+
}
66501+
try {
66502+
await execShell("brew install ccache");
66503+
}
66504+
catch (error) {
66505+
throw new Error(getPackageManagerError(error));
66506+
}
6649466507
}
6649566508
async function installCcacheLinux() {
66496-
if (await io.which("apt-get")) {
66497-
await execShellSudo("apt-get install -y ccache");
66498-
return;
66499-
}
66500-
else if (await io.which("apk")) {
66501-
await execShell("apk add ccache");
66502-
return;
66509+
const shouldUpdate = lib_core.getBooleanInput("update-package-index");
66510+
try {
66511+
if (await io.which("apt-get")) {
66512+
if (shouldUpdate) {
66513+
await execShellSudo("apt-get update");
66514+
}
66515+
await execShellSudo("apt-get install -y ccache");
66516+
return;
66517+
}
66518+
else if (await io.which("apk")) {
66519+
if (shouldUpdate) {
66520+
await execShell("apk update");
66521+
}
66522+
await execShell("apk add ccache");
66523+
return;
66524+
}
66525+
else if (await io.which("dnf")) {
66526+
if (shouldUpdate) {
66527+
await execShell("dnf check-update");
66528+
}
66529+
// ccache is part of EPEL repo.
66530+
await execShell("dnf install -y epel-release");
66531+
await execShell("dnf install -y ccache");
66532+
return;
66533+
}
6650366534
}
66504-
else if (await io.which("dnf")) {
66505-
// ccache is part of EPEL repo.
66506-
await execShell("dnf install -y epel-release");
66507-
await execShell("dnf install -y ccache");
66508-
return;
66535+
catch (error) {
66536+
throw new Error(getPackageManagerError(error));
6650966537
}
6651066538
throw Error("Can't install ccache automatically under this platform, please install it yourself before using this action.");
6651166539
}

src/restore.ts

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ import { cacheDir } from "./common";
1111

1212
const SELF_CI = process.env["CCACHE_ACTION_CI"] === "true"
1313

14+
function getPackageManagerError(error: Error | unknown) : string {
15+
return (
16+
`Failed to install ccache via package manager: '${error}'. ` +
17+
"Perhaps package manager index is not up to date? " +
18+
"(either update it manually before running ccache-action or set 'update-package-index' option to 'true')"
19+
);
20+
}
21+
1422
// based on https://cristianadam.eu/20200113/speeding-up-c-plus-plus-github-actions-using-ccache/
1523

1624
async function restore(ccacheVariant : string) : Promise<void> {
@@ -79,22 +87,44 @@ async function configure(ccacheVariant : string, platform : string) : Promise<vo
7987
}
8088

8189
async function installCcacheMac() : Promise<void> {
82-
await execShell("brew install ccache");
90+
if (core.getBooleanInput("update-package-index")) {
91+
await execShell("brew update");
92+
}
93+
try {
94+
await execShell("brew install ccache");
95+
} catch (error) {
96+
throw new Error(getPackageManagerError(error));
97+
}
8398
}
8499

85100
async function installCcacheLinux() : Promise<void> {
86-
if (await io.which("apt-get")) {
87-
await execShellSudo("apt-get install -y ccache");
88-
return;
89-
} else if (await io.which("apk")) {
90-
await execShell("apk add ccache");
91-
return;
92-
} else if (await io.which("dnf")) {
93-
// ccache is part of EPEL repo.
94-
await execShell("dnf install -y epel-release");
95-
await execShell("dnf install -y ccache");
96-
return;
101+
const shouldUpdate = core.getBooleanInput("update-package-index");
102+
try {
103+
if (await io.which("apt-get")) {
104+
if (shouldUpdate) {
105+
await execShellSudo("apt-get update");
106+
}
107+
await execShellSudo("apt-get install -y ccache");
108+
return;
109+
} else if (await io.which("apk")) {
110+
if (shouldUpdate) {
111+
await execShell("apk update");
112+
}
113+
await execShell("apk add ccache");
114+
return;
115+
} else if (await io.which("dnf")) {
116+
if (shouldUpdate) {
117+
await execShell("dnf check-update");
118+
}
119+
// ccache is part of EPEL repo.
120+
await execShell("dnf install -y epel-release");
121+
await execShell("dnf install -y ccache");
122+
return;
123+
}
124+
} catch (error) {
125+
throw new Error(getPackageManagerError(error));
97126
}
127+
98128
throw Error("Can't install ccache automatically under this platform, please install it yourself before using this action.");
99129
}
100130

0 commit comments

Comments
 (0)