Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

Commit

Permalink
Added Nomad binary file caching (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarampampam authored May 12, 2022
1 parent 437f600 commit bdba280
Show file tree
Hide file tree
Showing 7 changed files with 573 additions and 26 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ on:
pull_request:
paths-ignore: ['**.md']

concurrency:
group: ${{ github.ref }}
cancel-in-progress: true

jobs:
gitleaks:
name: Gitleaks
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this package will be documented in this file.

The format is based on [Keep a Changelog][keepachangelog] and this project adheres to [Semantic Versioning][semver].

## v1.1.0

### Added

- Nomad binary file caching

## v1.0.0

### Added
Expand Down
198 changes: 196 additions & 2 deletions dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"homepage": "https://github.com/gacts/install-nomad#readme",
"dependencies": {
"@actions/cache": "^2.0.2",
"@actions/core": "^1.2.5",
"@actions/exec": "^1.1.1",
"@actions/github": "^5.0.1",
Expand Down
89 changes: 71 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,94 @@ const core = require('@actions/core') // docs: https://github.com/actions/toolki
const tc = require('@actions/tool-cache') // docs: https://github.com/actions/toolkit/tree/main/packages/tool-cache
const github = require('@actions/github') // docs: https://github.com/actions/toolkit/tree/main/packages/github
const io = require('@actions/io') // docs: https://github.com/actions/toolkit/tree/main/packages/io
const cache = require('@actions/cache') // docs: https://github.com/actions/toolkit/tree/main/packages/cache
const exec = require('@actions/exec') // docs: https://github.com/actions/toolkit/tree/main/packages/exec
const path = require('path')
const os = require('os')

// read action inputs
const input = {
version: core.getInput('version', {required: true}),
version: core.getInput('version', {required: true}).replace(/^v/, ''), // strip the 'v' prefix
githubToken: core.getInput('github-token'),
}

// main action entrypoint
async function run() {
let versionToInstall
async function runAction() {
let version

if (input.version.toLowerCase() === 'latest') {
core.debug('Requesting latest nomad version...')
versionToInstall = await getLatestNomadVersion(input.githubToken)
version = await getLatestNomadVersion(input.githubToken)
} else {
versionToInstall = input.version
version = input.version
}

core.startGroup('💾 Install Nomad')
core.info(`Nomad version to install: ${versionToInstall}`)
const nomadZipPath = await tc.downloadTool(getNomadURI(process.platform, process.arch, versionToInstall))
const extractedPath = await tc.extractZip(nomadZipPath, nomadZipPath + '-dist')
core.debug(`Add ${extractedPath} to the $PATH`)
core.addPath(extractedPath)
await doInstall(version)
core.endGroup()

core.startGroup('🧪 Installation check')
const nomadPath = await io.which('nomad', true)
core.info(`Nomad installed: ${nomadPath}`)
core.setOutput('nomad-bin', nomadPath)
await exec.exec(`"${nomadPath}"`, ['version'], {silent: true})
await doCheck()
core.endGroup()
}

/**
* @param {string} version
*
* @returns {Promise<void>}
*
* @throws
*/
async function doInstall(version) {
const pathToInstall = path.join(os.tmpdir(), `nomad-${version}`)
const cacheKey = `nomad-cache-${version}-${process.platform}-${process.arch}`

core.info(`Version to install: ${version} (target directory: ${pathToInstall})`)

let restoredFromCache = undefined

try {
restoredFromCache = await cache.restoreCache([pathToInstall], cacheKey)
} catch (e) {
core.warning(e)
}

if (restoredFromCache !== undefined) { // cache HIT
core.info(`👌 Nomad restored from cache`)
} else { // cache MISS
const nomadZipPath = await tc.downloadTool(
getNomadURI(process.platform, process.arch, version), path.join(os.tmpdir(), `nomad.tmp`),
)
await tc.extractZip(nomadZipPath, pathToInstall)

try {
await cache.saveCache([pathToInstall], cacheKey)
} catch (e) {
core.warning(e)
}
}

core.addPath(pathToInstall)
}

/**
* @returns {Promise<void>}
*
* @throws
*/
async function doCheck() {
const nomadBinPath = await io.which('nomad', true)

if (nomadBinPath === "") {
throw new Error('nomad binary file not found in $PATH')
}

core.info(`Nomad installed: ${nomadBinPath}`)
core.setOutput('nomad-bin', nomadBinPath)

await exec.exec('nomad', ['version'], {silent: true})
}

/**
* @param {string} githubAuthToken
* @returns {Promise<string>}
Expand Down Expand Up @@ -109,8 +162,8 @@ function getNomadURI(platform, arch, version) {
}

// run the action
try {
run()
} catch (error) {
(async () => {
await runAction()
})().catch(error => {
core.setFailed(error.message)
}
})
Loading

0 comments on commit bdba280

Please sign in to comment.