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

fix: support concurrent agent runs by checking if Trivy is installed #17

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
50 changes: 34 additions & 16 deletions trivy-task/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,24 +151,35 @@ async function installTrivy(version: string): Promise<string> {
}
if (os.platform() != "linux") {
throw new Error("Only Linux is currently supported")
}

let url = await getArtifactURL(version)

let bin = "trivy"

let localPath = tmpPath + bin;
task.rmRF(localPath);
}

console.log("Downloading Trivy...")
let downloadPath = await tool.downloadTool(url, localPath);
const bin = "trivy"

console.log("Extracting Trivy...")
await tool.extractTar(downloadPath, tmpPath)
const localPath = tmpPath + bin

const binPath = tmpPath + bin

console.log("Setting permissions...")
await task.exec('chmod', ["+x", binPath]);
const artifact = getArtifactName(version)

const installedAlready = task.exist(localPath + "/" + artifact)

if (installedAlready) {
console.log("Trivy Already Installed.")
} else {
const url = getArtifactURL(version, artifact)

task.rmRF(localPath);

console.log("Downloading Trivy...")
const downloadPath = await tool.downloadTool(url, localPath);

console.log("Extracting Trivy...")
await tool.extractTar(downloadPath, tmpPath)

console.log("Setting permissions...")
await task.exec('chmod', ["+x", binPath]);
}

return binPath
}

Expand All @@ -179,7 +190,14 @@ function stripV(version: string): string {
return version
}

async function getArtifactURL(version: string): Promise<string> {
function getArtifactURL(version: string, artifact: string): string {
if(version === "latest") {
version = latestTrivyVersion
}
return util.format("https://github.com/aquasecurity/trivy/releases/download/%s/%s", version, artifact);
}

function getArtifactName(version: string): string {
if(version === "latest") {
version = latestTrivyVersion
}
Expand All @@ -203,7 +221,7 @@ async function getArtifactURL(version: string): Promise<string> {
}
// e.g. trivy_0.29.1_Linux-ARM.tar.gz
let artifact: string = util.format("trivy_%s_Linux-%s.tar.gz", stripV(version), arch);
return util.format("https://github.com/aquasecurity/trivy/releases/download/%s/%s", version, artifact);
return artifact;
}

run().catch((err: Error) => {
Expand Down