#2: Rewrite as a composite action

This commit is contained in:
mtkennerly 2023-03-28 15:38:58 +08:00
parent 1a3a67c1e5
commit 6708a62e85
No known key found for this signature in database
GPG key ID: E764BE00BE6E6408
9 changed files with 68 additions and 3088 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
__pycache__/
node_modules/ node_modules/
out/ out/
tmp*/ tmp*/

View file

@ -9,11 +9,3 @@ repos:
rev: v1.1.6 rev: v1.1.6
hooks: hooks:
- id: forbid-tabs - id: forbid-tabs
- repo: local
hooks:
- id: build
name: build
language: system
entry: npm run build
pass_filenames: false
files: '(\.(tsx?|jsx?)$|^package.json$|^tsconfig.json$)'

61
action.py Normal file
View file

@ -0,0 +1,61 @@
import os
import subprocess
def run_command(command: str) -> str:
print("Running command: {}".format(command))
result = subprocess.run(command, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return result.stdout.decode("utf-8").strip()
def set_output(name: str, value: str) -> None:
with open(os.environ["GITHUB_OUTPUT"], "a") as file:
file.write(["{}={}\n".format(name, value)])
def export_variable(name: str, value: str) -> None:
print("Setting environment variable: {}".format(name))
with open(os.environ["GITHUB_ENV"], "a") as file:
file.write(["{}={}\n".format(name, value)])
def set_failed(error: Exception) -> None:
print("::error::{}".format(error))
stdout = getattr(error, "stdout", None)
if stdout is not None:
print("stdout: {}".format(stdout))
stderr = getattr(error, "stderr", None)
if stderr is not None:
print("stderr: {}".format(stdout))
def main() -> None:
install = os.environ["INPUT_INSTALL"]
env_var = os.environ["INPUT_ENV_VAR"]
command = os.environ["INPUT_COMMAND"]
args = os.environ["INPUT_ARGS"]
if install == "none":
# No install.
pass
elif install == "latest":
run_command("pip install dunamai")
else:
run_command("pip install dunamai=={}", install)
version = run_command("{} {}".format(command, args))
set_output("version", version)
print("Dynamic version: {}".format(version))
if env_var != "":
export_variable(env_var, version)
if __name__ == "__main__":
try:
main()
except Exception as error:
set_failed(error)
exit(1)

View file

@ -30,6 +30,10 @@ inputs:
outputs: outputs:
version: version:
description: The dynamic version. description: The dynamic version.
value: ${{ steps.main.outputs.version }}
runs: runs:
using: node16 using: composite
main: public/index.js steps:
- id: main
shell: bash
run: python ${{ github.action_path }}/action.py

129
package-lock.json generated
View file

@ -1,129 +0,0 @@
{
"name": "dunamai-action",
"version": "1.2.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "dunamai-action",
"version": "1.2.0",
"license": "MIT",
"dependencies": {
"@actions/core": "^1.10.0"
},
"devDependencies": {
"@types/node": "^16.11.7",
"@vercel/ncc": "^0.34.0",
"typescript": "^3.9.6"
}
},
"node_modules/@actions/core": {
"version": "1.10.0",
"resolved": "https://registry.npmjs.org/@actions/core/-/core-1.10.0.tgz",
"integrity": "sha512-2aZDDa3zrrZbP5ZYg159sNoLRb61nQ7awl5pSvIq5Qpj81vwDzdMRKzkWJGJuwVvWpvZKx7vspJALyvaaIQyug==",
"dependencies": {
"@actions/http-client": "^2.0.1",
"uuid": "^8.3.2"
}
},
"node_modules/@actions/http-client": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/@actions/http-client/-/http-client-2.0.1.tgz",
"integrity": "sha512-PIXiMVtz6VvyaRsGY268qvj57hXQEpsYogYOu2nrQhlf+XCGmZstmuZBbAybUl1nQGnvS1k1eEsQ69ZoD7xlSw==",
"dependencies": {
"tunnel": "^0.0.6"
}
},
"node_modules/@types/node": {
"version": "16.18.3",
"resolved": "https://registry.npmjs.org/@types/node/-/node-16.18.3.tgz",
"integrity": "sha512-jh6m0QUhIRcZpNv7Z/rpN+ZWXOicUUQbSoWks7Htkbb9IjFQj4kzcX/xFCkjstCj5flMsN8FiSvt+q+Tcs4Llg==",
"dev": true
},
"node_modules/@vercel/ncc": {
"version": "0.34.0",
"resolved": "https://registry.npmjs.org/@vercel/ncc/-/ncc-0.34.0.tgz",
"integrity": "sha512-G9h5ZLBJ/V57Ou9vz5hI8pda/YQX5HQszCs3AmIus3XzsmRn/0Ptic5otD3xVST8QLKk7AMk7AqpsyQGN7MZ9A==",
"dev": true,
"bin": {
"ncc": "dist/ncc/cli.js"
}
},
"node_modules/tunnel": {
"version": "0.0.6",
"resolved": "https://registry.npmjs.org/tunnel/-/tunnel-0.0.6.tgz",
"integrity": "sha512-1h/Lnq9yajKY2PEbBadPXj3VxsDDu844OnaAo52UVmIzIvwwtBPIuNvkjuzBlTWpfJyUbG3ez0KSBibQkj4ojg==",
"engines": {
"node": ">=0.6.11 <=0.7.0 || >=0.7.3"
}
},
"node_modules/typescript": {
"version": "3.9.6",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-3.9.6.tgz",
"integrity": "sha512-Pspx3oKAPJtjNwE92YS05HQoY7z2SFyOpHo9MqJor3BXAGNaPUs83CuVp9VISFkSjyRfiTpmKuAYGJB7S7hOxw==",
"dev": true,
"bin": {
"tsc": "bin/tsc",
"tsserver": "bin/tsserver"
},
"engines": {
"node": ">=4.2.0"
}
},
"node_modules/uuid": {
"version": "8.3.2",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz",
"integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==",
"bin": {
"uuid": "dist/bin/uuid"
}
}
},
"dependencies": {
"@actions/core": {
"version": "1.10.0",
"resolved": "https://registry.npmjs.org/@actions/core/-/core-1.10.0.tgz",
"integrity": "sha512-2aZDDa3zrrZbP5ZYg159sNoLRb61nQ7awl5pSvIq5Qpj81vwDzdMRKzkWJGJuwVvWpvZKx7vspJALyvaaIQyug==",
"requires": {
"@actions/http-client": "^2.0.1",
"uuid": "^8.3.2"
}
},
"@actions/http-client": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/@actions/http-client/-/http-client-2.0.1.tgz",
"integrity": "sha512-PIXiMVtz6VvyaRsGY268qvj57hXQEpsYogYOu2nrQhlf+XCGmZstmuZBbAybUl1nQGnvS1k1eEsQ69ZoD7xlSw==",
"requires": {
"tunnel": "^0.0.6"
}
},
"@types/node": {
"version": "16.18.3",
"resolved": "https://registry.npmjs.org/@types/node/-/node-16.18.3.tgz",
"integrity": "sha512-jh6m0QUhIRcZpNv7Z/rpN+ZWXOicUUQbSoWks7Htkbb9IjFQj4kzcX/xFCkjstCj5flMsN8FiSvt+q+Tcs4Llg==",
"dev": true
},
"@vercel/ncc": {
"version": "0.34.0",
"resolved": "https://registry.npmjs.org/@vercel/ncc/-/ncc-0.34.0.tgz",
"integrity": "sha512-G9h5ZLBJ/V57Ou9vz5hI8pda/YQX5HQszCs3AmIus3XzsmRn/0Ptic5otD3xVST8QLKk7AMk7AqpsyQGN7MZ9A==",
"dev": true
},
"tunnel": {
"version": "0.0.6",
"resolved": "https://registry.npmjs.org/tunnel/-/tunnel-0.0.6.tgz",
"integrity": "sha512-1h/Lnq9yajKY2PEbBadPXj3VxsDDu844OnaAo52UVmIzIvwwtBPIuNvkjuzBlTWpfJyUbG3ez0KSBibQkj4ojg=="
},
"typescript": {
"version": "3.9.6",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-3.9.6.tgz",
"integrity": "sha512-Pspx3oKAPJtjNwE92YS05HQoY7z2SFyOpHo9MqJor3BXAGNaPUs83CuVp9VISFkSjyRfiTpmKuAYGJB7S7hOxw==",
"dev": true
},
"uuid": {
"version": "8.3.2",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz",
"integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg=="
}
}
}

View file

@ -1,20 +0,0 @@
{
"name": "dunamai-action",
"version": "1.2.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "ncc build src/index.ts --out public"
},
"keywords": [],
"author": "",
"license": "MIT",
"dependencies": {
"@actions/core": "^1.10.0"
},
"devDependencies": {
"@types/node": "^16.11.7",
"@vercel/ncc": "^0.34.0",
"typescript": "^3.9.6"
}
}

File diff suppressed because it is too large Load diff

View file

@ -1,40 +0,0 @@
import * as core from "@actions/core";
import { execSync } from "child_process";
function runCommand(command: string): Buffer {
console.log(`Running command: ${command}`);
return execSync(command);
}
function main(): void {
const install = core.getInput("install");
const envVar = core.getInput("env-var");
const command = core.getInput("command");
const args = core.getInput("args");
if (install === "none") {
// No install.
} else if (install === "latest") {
runCommand("pip install dunamai");
} else {
runCommand(`pip install dunamai==${install}`);
}
const version = runCommand(`${command} ${args}`).toString().trim();
core.setOutput("version", version);
console.log(`Dynamic version: ${version}`);
if (envVar !== "") {
console.log(`Setting environment variable: ${envVar}`);
core.exportVariable(envVar, version);
}
}
try {
main();
} catch (error) {
core.setFailed(
// @ts-ignore
error.message
);
}

View file

@ -1,16 +0,0 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"outDir": "out",
"lib": [
"es5"
],
"rootDir": "src",
"strict": true,
"noFallthroughCasesInSwitch": true
},
"include": [
"src/*.ts"
]
}