|
1 | | -name: 'Login to CUE Registry via GitHub OIDC' |
2 | | -description: 'Authenticate to a CUE registry using GitHub OIDC tokens' |
| 1 | +name: Login to CUE Registry via GitHub OIDC |
| 2 | +description: Authenticate to a CUE registry using GitHub OIDC tokens |
3 | 3 | branding: |
4 | | - icon: 'log-in' |
5 | | - color: 'blue' |
6 | | - |
| 4 | + icon: log-in |
| 5 | + color: blue |
7 | 6 | inputs: |
8 | 7 | registry: |
9 | | - description: 'CUE registry' |
| 8 | + description: CUE registry |
10 | 9 | required: false |
11 | | - default: 'registry.cue.works' |
| 10 | + default: registry.cue.works |
12 | 11 | update_logins: |
13 | | - description: 'Whether to update the local CUE logins.json file' |
| 12 | + description: Whether to update the local CUE logins.json file |
14 | 13 | required: false |
15 | | - default: 'true' |
16 | | - |
| 14 | + default: "true" |
17 | 15 | outputs: |
18 | 16 | access_token: |
19 | | - description: 'The access token obtained from the registry' |
| 17 | + description: The access token obtained from the registry |
20 | 18 | value: ${{ steps.oidc.outputs.access_token }} |
21 | | - |
22 | 19 | runs: |
23 | | - using: 'composite' |
| 20 | + using: composite |
24 | 21 | steps: |
25 | 22 | - name: Get OIDC token and login |
26 | 23 | id: oidc |
27 | 24 | uses: actions/github-script@v7 |
28 | 25 | with: |
29 | 26 | script: | |
30 | | - const registry = '${{ inputs.registry }}' |
31 | | - const registryUrl = `https://${registry}` |
32 | | - |
| 27 | + const registry = "${{ inputs.registry }}"; |
| 28 | + const registryUrl = `https://${registry}`; |
| 29 | +
|
33 | 30 | // Get OIDC token with registry as audience |
34 | | - let token = null |
| 31 | + let token = null; |
35 | 32 | try { |
36 | | - token = await core.getIDToken(registryUrl) |
| 33 | + token = await core.getIDToken(registryUrl); |
37 | 34 | } catch (e) { |
38 | | - throw new Error(`Failed to obtain OIDC token. Did you specify the id-token: write permission? ${e.message}`) |
| 35 | + throw new Error( |
| 36 | + `Failed to obtain OIDC token. Did you specify the id-token: write permission? ${e.message}`, |
| 37 | + ); |
39 | 38 | } |
40 | 39 |
|
41 | 40 | // Exchange OIDC token for registry access token |
42 | 41 | const res = await fetch(`${registryUrl}/login/oidc/github`, { |
43 | | - method: 'POST', |
| 42 | + method: "POST", |
44 | 43 | headers: { |
45 | | - 'Content-Type': 'application/json' |
| 44 | + "Content-Type": "application/json", |
46 | 45 | }, |
47 | | - body: JSON.stringify({ id_token: token }) |
48 | | - }) |
| 46 | + body: JSON.stringify({ id_token: token }), |
| 47 | + }); |
49 | 48 | if (!res.ok) { |
50 | 49 | let msg = `HTTP ${res.status} ${res.statusText}`; |
51 | 50 | try { |
52 | | - const data = await res.json() |
53 | | - msg = data.error_description || JSON.stringify(data) |
54 | | - } catch (e) { |
55 | | - } |
56 | | - throw new Error(`${registry}: ${msg}`) |
| 51 | + const data = await res.json(); |
| 52 | + msg = data.error_description || JSON.stringify(data); |
| 53 | + } catch (e) {} |
| 54 | + throw new Error(`${registry}: ${msg}`); |
57 | 55 | } |
58 | | - const data = await res.json() |
59 | | - core.setOutput('access_token', data.access_token) |
| 56 | + const data = await res.json(); |
| 57 | + core.setOutput("access_token", data.access_token); |
60 | 58 |
|
61 | 59 | // Write credentials to cue logins file if update_logins is true |
62 | | - if ('${{ inputs.update_logins }}' === 'true') { |
63 | | - const fs = require('fs') |
64 | | - const path = require('path') |
65 | | - const homeDir = process.env.HOME || process.env.USERPROFILE |
66 | | - const configDir = path.join(homeDir, '.config', 'cue') |
67 | | - const loginsPath = path.join(configDir, 'logins.json') |
68 | | - |
69 | | - fs.mkdirSync(configDir, { recursive: true }) |
70 | | - |
71 | | - let logins = {} |
| 60 | + if ("${{ inputs.update_logins }}" === "true") { |
| 61 | + const fs = require("fs"); |
| 62 | + const path = require("path"); |
| 63 | + const homeDir = process.env.HOME || process.env.USERPROFILE; |
| 64 | + const configDir = path.join(homeDir, ".config", "cue"); |
| 65 | + const loginsPath = path.join(configDir, "logins.json"); |
| 66 | +
|
| 67 | + fs.mkdirSync(configDir, { recursive: true }); |
| 68 | +
|
| 69 | + let logins = {}; |
72 | 70 | if (fs.existsSync(loginsPath)) { |
73 | 71 | try { |
74 | | - const content = fs.readFileSync(loginsPath, 'utf8') |
| 72 | + const content = fs.readFileSync(loginsPath, "utf8"); |
75 | 73 | if (content.trim()) { |
76 | | - logins = JSON.parse(content) |
| 74 | + logins = JSON.parse(content); |
77 | 75 | } |
78 | 76 | } catch (err) { |
79 | | - console.warn('Failed to parse existing logins.json, will overwrite:', err.message) |
| 77 | + console.warn( |
| 78 | + "Failed to parse existing logins.json, will overwrite:", |
| 79 | + err.message, |
| 80 | + ); |
80 | 81 | } |
81 | 82 | } |
82 | | - |
83 | | - logins.registries = logins.registries || {} |
84 | | - |
| 83 | +
|
| 84 | + logins.registries = logins.registries || {}; |
| 85 | +
|
85 | 86 | logins.registries[registry] = { |
86 | 87 | access_token: data.access_token, |
87 | | - token_type: "Bearer" |
88 | | - } |
89 | | - |
90 | | - fs.writeFileSync(loginsPath, JSON.stringify(logins, null, 2) + '\n') |
| 88 | + token_type: "Bearer", |
| 89 | + }; |
| 90 | +
|
| 91 | + fs.writeFileSync(loginsPath, JSON.stringify(logins, null, 2) + "\\n"); |
91 | 92 | } |
0 commit comments