|
| 1 | +import crypto from "node:crypto"; |
| 2 | +import process from "node:process"; |
| 3 | +import * as url from "node:url"; |
| 4 | +import core from "@actions/core"; |
| 5 | +import playwright from "playwright"; |
| 6 | + |
| 7 | +export default async function () { |
| 8 | + core.info("Starting 'auth' action"); |
| 9 | + |
| 10 | + let browser: playwright.Browser | undefined; |
| 11 | + let context: playwright.BrowserContext | undefined; |
| 12 | + let page: playwright.Page | undefined; |
| 13 | + try { |
| 14 | + // Get inputs |
| 15 | + const loginUrl = core.getInput("login_url", { required: true }); |
| 16 | + const username = core.getInput("username", { required: true }); |
| 17 | + const password = core.getInput("password", { required: true }); |
| 18 | + core.setSecret(password); |
| 19 | + |
| 20 | + // Determine storage path for authenticated session state |
| 21 | + // Playwright will create missing directories, if needed |
| 22 | + const actionDirectory = `${url.fileURLToPath(new URL(import.meta.url))}/..`; |
| 23 | + const sessionStatePath = `${ |
| 24 | + process.env.RUNNER_TEMP ?? actionDirectory |
| 25 | + }/.auth/${crypto.randomUUID()}/sessionState.json`; |
| 26 | + |
| 27 | + // Launch a headless browser |
| 28 | + browser = await playwright.chromium.launch({ |
| 29 | + headless: true, |
| 30 | + executablePath: process.env.CI ? "/usr/bin/google-chrome" : undefined, |
| 31 | + }); |
| 32 | + context = await browser.newContext(); |
| 33 | + page = await context.newPage(); |
| 34 | + |
| 35 | + // Log in |
| 36 | + core.info("Navigating to login page"); |
| 37 | + await page.goto(loginUrl); |
| 38 | + core.info("Filling username"); |
| 39 | + await page.getByLabel(/username/i).fill(username); |
| 40 | + core.info("Filling password"); |
| 41 | + await page.getByLabel(/password/i).fill(password); |
| 42 | + core.info("Logging in"); |
| 43 | + await page |
| 44 | + .getByLabel(/password/i) |
| 45 | + .locator("xpath=ancestor::form") |
| 46 | + .evaluate((form) => (form as HTMLFormElement).submit()); |
| 47 | + |
| 48 | + // Write authenticated session state to a file and output its path |
| 49 | + await context.storageState({ path: sessionStatePath }); |
| 50 | + core.setOutput("session_state_path", sessionStatePath); |
| 51 | + core.info(`Wrote authenticated session state to ${sessionStatePath}`); |
| 52 | + } catch (error) { |
| 53 | + if (page) { |
| 54 | + core.info(`Errored at page URL: ${page.url()}`); |
| 55 | + } |
| 56 | + core.setFailed(`${error}`); |
| 57 | + process.exit(1); |
| 58 | + } finally { |
| 59 | + // Clean up |
| 60 | + await context?.close(); |
| 61 | + await browser?.close(); |
| 62 | + } |
| 63 | + |
| 64 | + core.info("Finished 'auth' action"); |
| 65 | +} |
0 commit comments