setup-nextflow/src/main.ts

177 lines
4.6 KiB
TypeScript
Raw Normal View History

2022-11-13 04:07:59 +00:00
import * as core from '@actions/core'
import * as exec from '@actions/exec'
import * as fs from 'fs'
import * as github from '@actions/github'
import * as tc from '@actions/tool-cache'
import retry = require('async-retry')
import semver = require('semver')
2022-06-13 18:29:56 +00:00
2022-11-13 04:07:59 +00:00
const NEXTFLOW_REPO = {owner: 'nextflow-io', repo: 'nextflow'}
2022-06-13 18:29:56 +00:00
async function all_nf_releases(ok) {
return await ok.paginate(
ok.rest.repos.listReleases,
NEXTFLOW_REPO,
2022-11-13 04:07:59 +00:00
response => response.data
)
2022-06-13 18:29:56 +00:00
}
async function latest_stable_release_data(ok) {
2022-11-13 04:07:59 +00:00
const {data: stable_release} = await ok.rest.repos.getLatestRelease(
2022-06-13 18:29:56 +00:00
NEXTFLOW_REPO
2022-11-13 04:07:59 +00:00
)
2022-06-13 18:29:56 +00:00
2022-11-13 04:07:59 +00:00
return stable_release
2022-06-13 18:29:56 +00:00
}
async function release_data(version, ok) {
// Setup tag-based filtering
2022-11-13 04:07:59 +00:00
let filter = r => {
return semver.satisfies(r.tag_name, version, true)
}
2022-06-13 18:29:56 +00:00
// Check if the user passed a 'latest*' tag, and override filtering
// accordingly
2022-11-13 04:07:59 +00:00
if (version.includes('latest')) {
if (version.includes('-everything')) {
2022-06-13 18:29:56 +00:00
// No filtering
2022-11-13 04:07:59 +00:00
filter = r => {
return true
}
} else if (version.includes('-edge')) {
filter = r => {
return r.tag_name.endsWith('-edge')
}
2022-06-13 18:29:56 +00:00
} else {
// This is special: passing 'latest' or 'latest-stable' allows us to use
// the latest stable GitHub release direct from the API
2022-11-13 04:07:59 +00:00
const stable_release = await latest_stable_release_data(ok)
return stable_release
2022-06-13 18:29:56 +00:00
}
}
// Get all the releases
2022-11-13 04:07:59 +00:00
const all_releases = await all_nf_releases(ok)
2022-06-13 18:29:56 +00:00
2022-11-13 04:07:59 +00:00
let matching_releases = all_releases.filter(filter)
2022-06-13 18:29:56 +00:00
matching_releases.sort(function (x, y) {
2022-11-13 04:07:59 +00:00
semver.compare(x.tag_name, y.tag_name, true)
})
2022-06-13 18:29:56 +00:00
2022-11-13 04:07:59 +00:00
return matching_releases[0]
2022-06-13 18:29:56 +00:00
}
function nextflow_bin_url(release, get_all) {
2022-11-13 04:07:59 +00:00
const release_assets = release.assets
const all_asset = release_assets.filter(a => {
return a.browser_download_url.endsWith('-all')
})[0]
const regular_asset = release_assets.filter(a => {
return a.name == 'nextflow'
})[0]
2022-06-13 18:29:56 +00:00
2022-11-13 04:07:59 +00:00
const dl_asset = get_all ? all_asset : regular_asset
2022-06-13 18:29:56 +00:00
2022-11-13 04:07:59 +00:00
return dl_asset.browser_download_url
2022-06-13 18:29:56 +00:00
}
2022-06-13 20:07:53 +00:00
async function install_nextflow(url, version) {
2022-11-13 04:07:59 +00:00
core.debug(`Downloading Nextflow from ${url}`)
2022-06-13 20:07:53 +00:00
const nf_dl_path = await retry(
2022-11-13 04:07:59 +00:00
async bail => {
return await tc.downloadTool(url)
2022-06-13 20:07:53 +00:00
},
{
2022-11-13 04:07:59 +00:00
onRetry: err => {
core.debug(`Download of ${url} failed, trying again. Error ${err}`)
}
2022-06-13 20:07:53 +00:00
}
2022-11-13 04:07:59 +00:00
)
2022-06-13 20:07:53 +00:00
2022-11-13 04:07:59 +00:00
const temp_install_dir = fs.mkdtempSync(`nxf-${version}`)
const nf_path = `${temp_install_dir}/nextflow`
2022-06-13 20:07:53 +00:00
2022-11-13 04:07:59 +00:00
fs.renameSync(nf_dl_path, nf_path)
fs.chmodSync(nf_path, '0711')
2022-06-13 20:07:53 +00:00
2022-11-13 04:07:59 +00:00
return temp_install_dir
2022-06-13 20:07:53 +00:00
}
2022-06-13 18:29:56 +00:00
async function run() {
2022-07-13 21:09:19 +00:00
// Set environment variables
2022-11-13 04:07:59 +00:00
core.exportVariable('CAPSULE_LOG', 'none')
2022-07-13 21:09:19 +00:00
2022-06-13 20:07:53 +00:00
// Read in the arguments
2022-11-13 04:07:59 +00:00
const token = core.getInput('token')
const version = core.getInput('version')
const get_all = core.getBooleanInput('all')
2022-06-13 20:07:53 +00:00
2022-11-13 04:07:59 +00:00
let resolved_version = ''
2022-06-13 20:34:37 +00:00
2022-06-13 18:29:56 +00:00
// Setup the API
2022-11-13 04:07:59 +00:00
let octokit = {}
2022-06-13 18:29:56 +00:00
try {
2022-11-13 04:07:59 +00:00
octokit = github.getOctokit(token)
} catch (e: any) {
2022-06-13 18:29:56 +00:00
core.setFailed(
`Could not authenticate to GitHub Releases API with provided token\n${e.message}`
2022-11-13 04:07:59 +00:00
)
2022-06-13 18:29:56 +00:00
}
// Get the release info for the desired release
2022-11-13 04:07:59 +00:00
let release: any = {}
2022-06-13 18:29:56 +00:00
try {
2022-11-13 04:07:59 +00:00
release = await release_data(version, octokit)
resolved_version = release.tag_name
core.info(`Input version '${version}' resolved to Nextflow ${release.name}`)
} catch (e: any) {
2022-06-13 18:29:56 +00:00
core.setFailed(
`Could not retrieve Nextflow release matching ${version}.\n${e.message}`
2022-11-13 04:07:59 +00:00
)
2022-06-13 18:29:56 +00:00
}
// Get the download url for the desired release
2022-11-13 04:07:59 +00:00
let url = ''
2022-06-13 18:29:56 +00:00
try {
2022-11-13 04:07:59 +00:00
url = nextflow_bin_url(release, get_all)
core.info(`Preparing to download from ${url}`)
} catch (e: any) {
2022-11-13 04:07:59 +00:00
core.setFailed(`Could not parse the download URL\n${e.message}`)
2022-06-13 18:29:56 +00:00
}
try {
2022-06-13 20:07:53 +00:00
// Download Nextflow and add it to path
2022-11-13 04:07:59 +00:00
let nf_path = ''
nf_path = tc.find('nextflow', resolved_version)
2022-06-13 20:07:53 +00:00
if (!nf_path) {
2022-11-13 04:07:59 +00:00
core.debug(`Could not find Nextflow ${resolved_version} in cache`)
const nf_install_path = await install_nextflow(url, resolved_version)
2022-06-13 20:34:37 +00:00
2022-11-13 04:07:59 +00:00
nf_path = await tc.cacheDir(nf_install_path, 'nextflow', resolved_version)
core.debug(`Added Nextflow to cache: ${nf_path}`)
2022-06-13 20:07:53 +00:00
2022-11-13 04:07:59 +00:00
fs.rmdirSync(nf_install_path, {recursive: true})
2022-06-13 20:07:53 +00:00
} else {
2022-11-13 04:07:59 +00:00
core.debug(`Using cached version of Nextflow: ${nf_path}`)
2022-06-13 20:07:53 +00:00
}
2022-11-13 04:07:59 +00:00
core.addPath(nf_path)
2022-06-13 18:29:56 +00:00
2022-11-13 04:07:59 +00:00
core.info(`Downloaded \`nextflow\` to ${nf_path} and added to PATH`)
} catch (e: any) {
2022-11-13 04:07:59 +00:00
core.setFailed(e.message)
2022-06-13 18:29:56 +00:00
}
2022-06-13 21:38:31 +00:00
// Run Nextflow so it downloads its dependencies
try {
2022-11-13 04:07:59 +00:00
const nf_exit_code = await exec.exec('nextflow', ['help'])
} catch (e: any) {
2022-11-13 04:07:59 +00:00
core.warning(
'Nextflow appears to have installed correctly, but an error was thrown while running it.'
)
2022-06-13 21:38:31 +00:00
}
2022-06-13 18:29:56 +00:00
}
2022-11-13 04:07:59 +00:00
run()