Skip to content

Commit f73f102

Browse files
test type script
Signed-off-by: Nikita Korolev <nikita.korolev@flant.com>
1 parent d7f71d0 commit f73f102

5 files changed

Lines changed: 288 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: 'My Hello TypeScript Action'
2+
description: 'A reusable TypeScript action example'
3+
inputs:
4+
name:
5+
description: 'Name to greet'
6+
required: true
7+
default: 'World'
8+
uppercase:
9+
description: 'Convert greeting to uppercase'
10+
required: false
11+
default: 'false'
12+
outputs:
13+
greeting:
14+
description: 'The greeting message'
15+
greeting-length:
16+
description: 'Length of the greeting'
17+
runs:
18+
using: 'node20'
19+
main: 'dist/index.js'
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "my-hello-action",
3+
"version": "1.0.0",
4+
"type": "module",
5+
"description": "A reusable TypeScript action",
6+
"main": "dist/index.js",
7+
"scripts": {
8+
"build": "tsc",
9+
"bundle": "ncc build src/main.ts -o dist"
10+
},
11+
"dependencies": {
12+
"@actions/core": "^1.10.0",
13+
"@actions/github": "^6.0.0"
14+
},
15+
"devDependencies": {
16+
"@types/node": "^20.0.0",
17+
"@vercel/ncc": "^0.38.0",
18+
"typescript": "^5.0.0"
19+
}
20+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import * as core from '@actions/core';
2+
import * as github from '@actions/github';
3+
4+
async function run(): Promise<void> {
5+
try {
6+
const name = core.getInput('name', { required: true });
7+
const uppercase = core.getInput('uppercase') === 'true';
8+
9+
let greeting = `Hello, ${name}!`;
10+
11+
if (uppercase) {
12+
greeting = greeting.toUpperCase();
13+
}
14+
15+
core.info(greeting);
16+
core.info(`Repository: ${github.context.repo.owner}/${github.context.repo.repo}`);
17+
core.info(`Actor: ${github.context.actor}`);
18+
19+
core.setOutput('greeting', greeting);
20+
core.setOutput('greeting-length', greeting.length.toString());
21+
} catch (error) {
22+
if (error instanceof Error) {
23+
core.setFailed(error.message);
24+
}
25+
}
26+
}
27+
28+
run();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2020",
4+
"module": "NodeNext",
5+
"moduleResolution": "NodeNext",
6+
"lib": ["ES2020"],
7+
"outDir": "./dist",
8+
"rootDir": "./src",
9+
"strict": true,
10+
"esModuleInterop": true,
11+
"skipLibCheck": true,
12+
"declaration": true
13+
},
14+
"include": ["src/**/*"]
15+
}
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
name: TypeScript Examples
2+
3+
on:
4+
workflow_dispatch:
5+
6+
jobs:
7+
# Example 1: Simple inline script with actions/github-script
8+
# This approach runs TypeScript/JavaScript directly in the YAML workflow
9+
inline-script-example:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout
13+
uses: actions/checkout@v4
14+
15+
- name: Run inline TypeScript script
16+
uses: actions/github-script@v9
17+
with:
18+
script: |
19+
// This runs in Node.js context with github, context, core, etc.
20+
const { data: refData } = await github.rest.git.getRef({
21+
owner: context.repo.owner,
22+
repo: context.repo.repo,
23+
ref: 'heads/main'
24+
});
25+
console.log('Main branch SHA:', refData.object.sha);
26+
console.log('Triggered by:', context.actor);
27+
28+
// Use core for output
29+
core.setOutput('main-sha', refData.object.sha);
30+
31+
# Example 2: Import a file using actions/github-script with require
32+
# This allows running code from a separate .js/.ts file
33+
import-file-example:
34+
runs-on: ubuntu-latest
35+
steps:
36+
- name: Checkout
37+
uses: actions/checkout@v4
38+
39+
- name: Create script file to import
40+
run: |
41+
cat > my-script.js << 'EOF'
42+
// This file is imported and executed
43+
const fs = require('fs');
44+
const path = require('path');
45+
46+
module.exports = async ({ github, context, core }) => {
47+
const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8'));
48+
core.info(`Package name: ${packageJson.name}`);
49+
core.info(`Package version: ${packageJson.version}`);
50+
core.setOutput('package-name', packageJson.name);
51+
};
52+
EOF
53+
54+
- name: Run imported script
55+
uses: actions/github-script@v9
56+
with:
57+
result-encoding: string
58+
script: |
59+
const myScript = require('./my-script.js');
60+
await myScript({ github, context, core });
61+
62+
# Example 3: Using actions/typescript-action with local action
63+
# This requires: action.yml + dist/ (compiled TypeScript)
64+
local-typescript-action:
65+
runs-on: ubuntu-latest
66+
steps:
67+
- name: Checkout
68+
uses: actions/checkout@v4
69+
70+
- name: Setup Node.js
71+
uses: actions/setup-node@v4
72+
with:
73+
node-version: '20'
74+
75+
- name: Install dependencies
76+
run: |
77+
cat > package.json << 'EOF'
78+
{
79+
"name": "my-typescript-action",
80+
"version": "1.0.0",
81+
"type": "module",
82+
"dependencies": {
83+
"@actions/core": "^1.10.0",
84+
"@actions/github": "^6.0.0"
85+
},
86+
"devDependencies": {
87+
"@vercel/ncc": "^0.38.0",
88+
"typescript": "^5.0.0"
89+
}
90+
}
91+
EOF
92+
npm install
93+
94+
- name: Create TypeScript action source
95+
run: |
96+
mkdir -p src
97+
cat > src/main.ts << 'EOF'
98+
import * as core from '@actions/core';
99+
import * as github from '@actions/github';
100+
101+
async function run(): Promise<void> {
102+
try {
103+
const name = core.getInput('name', { required: true });
104+
const token = core.getInput('token');
105+
106+
core.info(`Hello, ${name}!`);
107+
core.info(`Repository: ${github.context.repo.owner}/${github.context.repo.repo}`);
108+
109+
if (token) {
110+
const octokit = github.getOctokit(token);
111+
const { data } = await octokit.rest.repos.get({
112+
owner: github.context.repo.owner,
113+
repo: github.context.repo.repo,
114+
});
115+
core.info(`Default branch: ${data.default_branch}`);
116+
core.setOutput('default-branch', data.default_branch);
117+
}
118+
119+
core.setOutput('greeting', `Hello, ${name}!`);
120+
} catch (error) {
121+
if (error instanceof Error) {
122+
core.setFailed(error.message);
123+
}
124+
}
125+
}
126+
127+
run();
128+
EOF
129+
130+
cat > tsconfig.json << 'EOF'
131+
{
132+
"compilerOptions": {
133+
"target": "ES2020",
134+
"module": "NodeNext",
135+
"moduleResolution": "NodeNext",
136+
"lib": ["ES2020"],
137+
"outDir": "./dist",
138+
"rootDir": "./src",
139+
"strict": true,
140+
"esModuleInterop": true,
141+
"skipLibCheck": true
142+
},
143+
"include": ["src/**/*"]
144+
}
145+
EOF
146+
147+
- name: Build TypeScript
148+
run: npx tsc
149+
150+
- name: Create action.yml
151+
run: |
152+
cat > action.yml << 'EOF'
153+
name: 'My TypeScript Action'
154+
description: 'A demo TypeScript action'
155+
inputs:
156+
name:
157+
description: 'Name to greet'
158+
required: true
159+
token:
160+
description: 'GitHub token for API calls'
161+
required: false
162+
outputs:
163+
greeting:
164+
description: 'The greeting message'
165+
default-branch:
166+
description: 'Default branch of the repo'
167+
runs:
168+
using: 'node20'
169+
main: 'dist/main.js'
170+
EOF
171+
172+
- name: Run local TypeScript action
173+
uses: ./
174+
with:
175+
name: World
176+
177+
# Example 4: Using a reusable TypeScript action from .github/actions/
178+
# This demonstrates the proper actions/typescript-action pattern
179+
reusable-typescript-action:
180+
runs-on: ubuntu-latest
181+
steps:
182+
- name: Checkout
183+
uses: actions/checkout@v4
184+
185+
- name: Setup Node.js
186+
uses: actions/setup-node@v4
187+
with:
188+
node-version: '20'
189+
190+
- name: Install and build reusable action
191+
working-directory: ./.github/actions/my-hello-action
192+
run: |
193+
npm install
194+
npm run build
195+
196+
- id: hello
197+
name: Run reusable TypeScript action
198+
uses: ./.github/actions/my-hello-action
199+
with:
200+
name: Claude
201+
uppercase: true
202+
203+
- name: Show action outputs
204+
run: |
205+
echo "Greeting: ${{ steps.hello.outputs.greeting }}"
206+
echo "Length: ${{ steps.hello.outputs.greeting-length }}"

0 commit comments

Comments
 (0)