Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework pacman pkg cache and enable by default #51

Merged
merged 2 commits into from
Jul 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 13 additions & 32 deletions .github/workflows/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,7 @@ on:
- cron: '0 0 * * 3,6'
jobs:

save:
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- name: build action
shell: bash
run: |
npm ci
npm run pkg
rm -rf node_modules
- name: run action
uses: ./
with:
cache: save
- shell: msys2 {0}
run: |
uname -a

cache:
needs: [save]
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
Expand All @@ -39,7 +20,7 @@ jobs:
uses: ./
with:
update: true
cache: true
install: base-devel git
- shell: msys2 {0}
run: |
uname -a
Expand Down Expand Up @@ -71,7 +52,7 @@ jobs:
msys2 ./test.sh MINGW32

cmd:
needs: [powershell]
needs: [cache]
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
Expand Down Expand Up @@ -100,7 +81,7 @@ jobs:
msys2 ./test.sh MINGW32

env:
needs: [cmd]
needs: [cache]
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
Expand All @@ -126,7 +107,7 @@ jobs:
MSYSTEM: MINGW32

shell:
needs: [env]
needs: [cache]
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
Expand All @@ -143,7 +124,7 @@ jobs:
uname -a

msystem:
needs: [shell]
needs: [cache]
strategy:
matrix:
task: [ MSYS, MINGW64, MINGW32 ]
Expand All @@ -163,7 +144,7 @@ jobs:
- run: msys2 ./test.sh ${{ matrix.task }}

update:
needs: [msystem]
needs: [cache]
strategy:
fail-fast: false
max-parallel: 3
Expand All @@ -186,7 +167,7 @@ jobs:
- run: msys2 ./test.sh ${{ matrix.msystem }}

MSYS2_PATH_TYPE:
needs: [update]
needs: [cache]
runs-on: windows-latest
steps:
- uses: actions/setup-go@v1
Expand All @@ -204,7 +185,7 @@ jobs:
MSYS2_PATH_TYPE: inherit

path-type:
needs: [update]
needs: [cache]
runs-on: windows-latest
steps:
- uses: actions/setup-go@v1
Expand All @@ -222,7 +203,7 @@ jobs:
- run: msys2 -c "go env"

install:
needs: [path-type]
needs: [cache]
strategy:
fail-fast: false
max-parallel: 3
Expand All @@ -246,7 +227,7 @@ jobs:
- run: msys2 ./test.sh MINGW64

defaultclean:
needs: [install]
needs: [cache]
runs-on: windows-latest
defaults:
run:
Expand All @@ -271,7 +252,7 @@ jobs:
- run: git describe --dirty --tags

defaultdirty:
needs: [install]
needs: [cache]
runs-on: windows-latest
defaults:
run:
Expand All @@ -294,7 +275,7 @@ jobs:
- run: git describe --dirty --tags

errorhandling:
needs: [defaultdirty]
needs: [cache]
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
Expand All @@ -312,7 +293,7 @@ jobs:
[[ "$-" =~ 'e' ]] || exit 1; # make sure "set -e" is active by default

workingdir:
needs: [errorhandling]
needs: [cache]
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
Expand Down
18 changes: 0 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,24 +146,6 @@ Installing additional packages after updating the system is supported through op
install: 'git base-devel'
```

#### cache

If set to `true`, directory `/var/cache/pacman/pkg` is restored/cached in order to speed up future updates:

```yaml
- uses: msys2/setup-msys2@v1
with:
cache: true
```

If set to `save`, the same directory is cached, but it is not restored. This can be used to force a save of a clean state.

```yaml
- uses: msys2/setup-msys2@v1
with:
cache: save
```

## Development

The steps to publish a new release are the following:
Expand Down
4 changes: 0 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ inputs:
description: 'Install packages after installation through pacman'
required: false
default: false
cache:
description: 'Cache /var/cache/pacman/pkg to speed up future updates'
required: false
default: false
runs:
using: 'node12'
main: 'index.js'
50 changes: 38 additions & 12 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const exec = require('@actions/exec');
const tc = require('@actions/tool-cache');
const path = require('path');
const fs = require('fs');
const crypto = require('crypto');
const { hashElement } = require('folder-hash');

const inst_url = 'https://github.com/msys2/msys2-installer/releases/download/2020-07-19/msys2-base-x86_64-20200719.sfx.exe';
Expand Down Expand Up @@ -36,7 +37,6 @@ async function run() {
const p_pathtype = core.getInput('path-type');
const p_msystem = core.getInput('msystem');
const p_install = core.getInput('install');
const p_cache = core.getInput('cache');

let base = 'C:';

Expand Down Expand Up @@ -75,7 +75,7 @@ async function run() {
fs.writeFileSync(cmd, wrap);

core.addPath(dest);
const c_paths = [(p_release ? dest : 'C:') + `\\msys64\\var\\cache\\pacman\\pkg\\`];
const pkgCachePath = (p_release ? dest : 'C:') + `\\msys64\\var\\cache\\pacman\\pkg\\`;

const msystem_allowed = ['MSYS', 'MINGW32', 'MINGW64'];
if (!msystem_allowed.includes(p_msystem.toUpperCase())) {
Expand All @@ -84,11 +84,21 @@ async function run() {
}
core.exportVariable('MSYSTEM', p_msystem.toUpperCase());

if (p_cache === 'true') {
core.startGroup('Restoring cache...');
console.log('Cache ID:', await cache.restoreCache(c_paths, 'msys2', ['msys2-']));
core.endGroup();
}
// We want a cache key that is ideally always the same for the same kind of job.
// So that mingw32 and ming64 jobs, and jobs with different install packages have different caches.
let shasum = crypto.createHash('sha1');
shasum.update([p_release, p_update, p_pathtype, p_msystem, p_install].toString());
// We include "update" in the fallback key so that a job run with update=false never fetches
// a cache created with update=true. Because this would mean a newer version than needed is in the cache
// which would never be used but also would win during cache prunging because it is newer.
const baseCacheKey = 'msys2-pkgs-upd:' + p_update.toString();
const jobCacheKey = baseCacheKey + '-conf:' + shasum.digest('hex').slice(0, 8);

core.startGroup('Restoring cache...');
// We ideally want a cache matching our configuration, but every cache is OK since we prune it later anyway
const restoreKey = await cache.restoreCache([pkgCachePath], jobCacheKey, [jobCacheKey, baseCacheKey]);
console.log(`Cache restore for ${jobCacheKey}, got ${restoreKey}`);
core.endGroup();

async function run(args, opts) {
await exec.exec('cmd', ['/D', '/S', '/C', cmd].concat(['-c', args.join(' ')]), opts);
Expand Down Expand Up @@ -121,12 +131,28 @@ async function run() {
core.endGroup();
}

if (p_cache === 'true' || p_cache === 'save') {
core.startGroup('Saving cache...');
const key = (await hashElement(c_paths[0]))['hash'].toString() + (new Date()).getTime().toString();
console.log('Cache ID:', await cache.saveCache(c_paths, 'msys2-' + key), '[' + key + ']');
core.endGroup();
core.startGroup('Prune cache...');
// Remove all uninstalled packages
await run(['paccache', '-r', '-f', '-u', '-k0']);
// Keep the newest for all other packages
await run(['paccache', '-r', '-f', '-k1']);
core.endGroup();

core.startGroup('Saving cache...');
const saveKey = jobCacheKey + '-files:' + (await hashElement(pkgCachePath))['hash'].toString();
if (restoreKey === saveKey) {
console.log(`Cache unchanged, skipping save for ${saveKey}`);
} else {
try {
const cacheId = await cache.saveCache([pkgCachePath], saveKey);
console.log(`Cache saved as ID ${cacheId} using key ${saveKey}`);
} catch (error) {
// In case something created the same cache since we restored we'll get an error here,
// but that's OK with us, so ignore.
console.log(error.message);
}
}
core.endGroup();
}
catch (error) {
core.setFailed(error.message);
Expand Down