|
| 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