|
| 1 | +/** |
| 2 | + * GitHub action to run other actions in WSL environment |
| 3 | + * @param {string} uses - Name of GitHub action to run (e.g. 'actions/checkout@v4') |
| 4 | + * @param {string} with - Input parameters to pass to the action in key=value format, supporting =, :, and = delimiters |
| 5 | + * @param {string} run - Commands to run in WSL bash shell |
| 6 | + * @return {Promise<void>} Promise that resolves when action completes |
| 7 | + */ |
| 8 | +const core = require('@actions/core'); |
| 9 | +const exec = require('@actions/exec'); |
| 10 | + |
| 11 | +// Define WSL environment variables once at the top level |
| 12 | +const baseWslEnv = [ |
| 13 | + 'GITHUB_WORKSPACE/p', |
| 14 | + 'GITHUB_ACTION', |
| 15 | + 'GITHUB_ACTIONS', |
| 16 | + 'GITHUB_ACTOR', |
| 17 | + 'GITHUB_REPOSITORY', |
| 18 | + 'GITHUB_EVENT_NAME', |
| 19 | + 'GITHUB_EVENT_PATH/p', |
| 20 | + 'GITHUB_SHA', |
| 21 | + 'GITHUB_REF', |
| 22 | + 'GITHUB_TOKEN', |
| 23 | + 'GITHUB_RUN_ID', |
| 24 | + 'GITHUB_RUN_NUMBER', |
| 25 | + 'RUNNER_OS', |
| 26 | + 'RUNNER_TEMP/p', |
| 27 | + 'RUNNER_TOOL_CACHE/p', |
| 28 | + 'CI', |
| 29 | + 'GITHUB_DEBUG', |
| 30 | + 'ACTIONS_RUNNER_DEBUG', |
| 31 | + 'ACTIONS_STEP_DEBUG' |
| 32 | +]; |
| 33 | + |
| 34 | +async function run() { |
| 35 | + try { |
| 36 | + // Get action inputs |
| 37 | + const uses = core.getInput('uses'); |
| 38 | + const withInputs = core.getInput('with'); |
| 39 | + const runCommand = core.getInput('run'); |
| 40 | + |
| 41 | + // Validate inputs |
| 42 | + if (runCommand && (uses || withInputs)) { |
| 43 | + throw new Error('The "run" input cannot be used together with "uses" or "with" inputs'); |
| 44 | + } |
| 45 | + |
| 46 | + if (!runCommand && !uses) { |
| 47 | + throw new Error('Either "run" or "uses" input must be provided'); |
| 48 | + } |
| 49 | + |
| 50 | + // If run command is provided, execute it directly in WSL |
| 51 | + if (runCommand) { |
| 52 | + core.info('Running command in WSL environment'); |
| 53 | + core.debug(`Command: ${runCommand}`); |
| 54 | + |
| 55 | + // Set up basic environment variables for WSL |
| 56 | + const wslEnv = baseWslEnv.join(':'); |
| 57 | + |
| 58 | + process.env.WSLENV = process.env.WSLENV ? `${process.env.WSLENV}:${wslEnv}` : wslEnv; |
| 59 | + |
| 60 | + await exec.exec('wsl.exe', ['bash', '-c', runCommand], { |
| 61 | + env: process.env |
| 62 | + }); |
| 63 | + |
| 64 | + core.info('Command completed successfully'); |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + // Original action execution logic for 'uses' |
| 69 | + core.info(`Running action ${uses} in WSL environment`); |
| 70 | + |
| 71 | + // Parse action name and version |
| 72 | + const [owner, repo, version] = uses.split(/[@/]/g); |
| 73 | + core.debug(`Parsed action: owner=${owner}, repo=${repo}, version=${version}`); |
| 74 | + |
| 75 | + // Set up environment variables from with inputs |
| 76 | + const env = {}; |
| 77 | + if (withInputs) { |
| 78 | + core.debug('Parsing with inputs:'); |
| 79 | + core.debug(withInputs); |
| 80 | + |
| 81 | + // Parse key-value pairs with flexible delimiters |
| 82 | + const inputs = withInputs |
| 83 | + .split(/[\n,]/) |
| 84 | + .map(line => line.trim()) |
| 85 | + .filter(line => line.length > 0) |
| 86 | + .reduce((acc, line) => { |
| 87 | + const match = line.match(/^([^=:]+)(?:=|\s*:\s*|\s+=\s+)(.+)$/); |
| 88 | + if (match) { |
| 89 | + const [, key, value] = match; |
| 90 | + acc[key.trim()] = value.trim(); |
| 91 | + } |
| 92 | + return acc; |
| 93 | + }, {}); |
| 94 | + |
| 95 | + for (const [key, value] of Object.entries(inputs)) { |
| 96 | + const envKey = `INPUT_${key.toUpperCase().replace(/-/g, '_')}`; |
| 97 | + env[envKey] = value; |
| 98 | + core.debug(`Setting env var: ${envKey}=${value}`); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + // Add environment variables to WSLENV |
| 103 | + const wslEnv = [ |
| 104 | + ...baseWslEnv, |
| 105 | + ...Object.keys(env).map(key => key.slice(6)) |
| 106 | + ].join(':'); |
| 107 | + |
| 108 | + process.env.WSLENV = process.env.WSLENV ? `${process.env.WSLENV}:${wslEnv}` : wslEnv; |
| 109 | + core.debug(`Set WSLENV: ${process.env.WSLENV}`); |
| 110 | + |
| 111 | + // Clone and install the action in WSL |
| 112 | + core.info('Cloning and installing action in WSL...'); |
| 113 | + await exec.exec('wsl.exe', ['bash', '-c', ` |
| 114 | + set -e |
| 115 | + mkdir -p ~/actions/${owner}/${repo} |
| 116 | + cd ~/actions/${owner}/${repo} |
| 117 | + git clone --depth 1 --branch ${version} https://github.com/${owner}/${repo}.git . |
| 118 | + # Install dependencies if needed |
| 119 | + if [ -f "package.json" ]; then |
| 120 | + npm install |
| 121 | + npm run build || true |
| 122 | + fi |
| 123 | + # First check for dist/index.js |
| 124 | + if [ -f "dist/index.js" ]; then |
| 125 | + echo "MAIN_FILE=dist/index.js" >> $GITHUB_ENV |
| 126 | + else |
| 127 | + # Fall back to checking action.yml for entry point |
| 128 | + for action_file in action.yml action.yaml; do |
| 129 | + if [ -f "$action_file" ]; then |
| 130 | + MAIN_FILE=$(grep "main:" "$action_file" | sed 's/main:[[:space:]]*//;s/^["'\'']*//;s/["'\''].*$//') |
| 131 | + if [ ! -z "$MAIN_FILE" ]; then |
| 132 | + echo "Entry point from $action_file: $MAIN_FILE" |
| 133 | + echo "MAIN_FILE=$MAIN_FILE" >> $GITHUB_ENV |
| 134 | + break |
| 135 | + fi |
| 136 | + fi |
| 137 | + done |
| 138 | + fi |
| 139 | + `], { |
| 140 | + env: { |
| 141 | + ...process.env, |
| 142 | + ...env |
| 143 | + } |
| 144 | + }); |
| 145 | + |
| 146 | + // Run the action |
| 147 | + core.info('Running action...'); |
| 148 | + const debugScript = process.env.GITHUB_DEBUG === 'true' ? 'env' : ''; |
| 149 | + await exec.exec('wsl.exe', ['bash', '-c', ` |
| 150 | + set -e |
| 151 | + cd ~/actions/${owner}/${repo} |
| 152 | + ${debugScript} |
| 153 | + if [ -f "$MAIN_FILE" ]; then |
| 154 | + node "$MAIN_FILE" |
| 155 | + else |
| 156 | + echo "Could not find entry point at $MAIN_FILE. Contents of directory:" |
| 157 | + ls -R |
| 158 | + exit 1 |
| 159 | + fi |
| 160 | + `], { |
| 161 | + env: { |
| 162 | + ...process.env, |
| 163 | + ...env |
| 164 | + } |
| 165 | + }); |
| 166 | + |
| 167 | + core.info('Action completed successfully'); |
| 168 | + |
| 169 | + } catch (error) { |
| 170 | + core.error('Action failed with error:'); |
| 171 | + core.error(error); |
| 172 | + core.setFailed(error.message); |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +run(); |
0 commit comments