Skip to content

Commit bd94265

Browse files
marcelotrevisanisszzsolt
andcommitted
feat: add Windows support for 1Password CLI for GH runners
Co-authored-by: sszzsolt <sszzsolt@users.noreply.github.com>
1 parent 36d945b commit bd94265

File tree

5 files changed

+180
-6
lines changed

5 files changed

+180
-6
lines changed

.github/workflows/test.yml

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,24 @@ jobs:
66
name: "Test getting latest CLI stable version"
77
strategy:
88
matrix:
9-
os: [ubuntu-latest, macos-latest]
9+
os: [ubuntu-latest, macos-latest, windows-latest]
1010
runs-on: ${{ matrix.os }}
1111
steps:
1212
- uses: actions/checkout@v3
1313
- name: Install 1Password CLI
1414
uses: ./ # 1password/install-cli-action@<version>
1515
- name: Check CLI version
16+
if: matrix.os == 'ubuntu-latest' || matrix.os == 'macos-latest'
1617
run: ./test/assert-version.sh latest
18+
- name: Check CLI version
19+
if: matrix.os == 'windows-latest'
20+
run: .\test\assert-version.ps1 latest
21+
shell: pwsh
1722
use-latest-beta-version:
1823
name: "Test getting latest CLI beta version"
1924
strategy:
2025
matrix:
21-
os: [ubuntu-latest, macos-latest]
26+
os: [ubuntu-latest, macos-latest, windows-latest]
2227
runs-on: ${{ matrix.os }}
2328
steps:
2429
- uses: actions/checkout@v3
@@ -27,12 +32,17 @@ jobs:
2732
with:
2833
version: latest-beta
2934
- name: Check CLI version
35+
if: matrix.os == 'ubuntu-latest' || matrix.os == 'macos-latest'
3036
run: ./test/assert-version.sh latest-beta
37+
- name: Check CLI version
38+
if: matrix.os == 'windows-latest'
39+
run: .\test\assert-version.ps1 latest-beta
40+
shell: pwsh
3141
use-specific-version:
3242
name: "Test getting a specific CLI version"
3343
strategy:
3444
matrix:
35-
os: [ubuntu-latest, macos-latest]
45+
os: [ubuntu-latest, macos-latest, windows-latest]
3646
runs-on: ${{ matrix.os }}
3747
steps:
3848
- uses: actions/checkout@v3
@@ -41,12 +51,17 @@ jobs:
4151
with:
4252
version: 2.18.0
4353
- name: Check CLI version
54+
if: matrix.os == 'ubuntu-latest' || matrix.os == 'macos-latest'
4455
run: ./test/assert-version.sh 2.18.0
56+
- name: Check CLI version
57+
if: matrix.os == 'windows-latest'
58+
run: .\test\assert-version.ps1 2.18.0
59+
shell: pwsh
4560
use-specific-beta-version:
4661
name: "Test getting a specific CLI beta version"
4762
strategy:
4863
matrix:
49-
os: [ubuntu-latest, macos-latest]
64+
os: [ubuntu-latest, macos-latest, windows-latest]
5065
runs-on: ${{ matrix.os }}
5166
steps:
5267
- uses: actions/checkout@v3
@@ -55,4 +70,9 @@ jobs:
5570
with:
5671
version: 2.19.0-beta.01
5772
- name: Check CLI version
73+
if: matrix.os == 'ubuntu-latest' || matrix.os == 'macos-latest'
5874
run: ./test/assert-version.sh 2.19.0-beta.01
75+
- name: Check CLI version
76+
if: matrix.os == 'windows-latest'
77+
run: .\test\assert-version.ps1 2.19.0-beta.01
78+
shell: pwsh

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ To install a specific version of the 1Password CLI:
4141

4242
## ⚙️ Supported Runners
4343

44-
You can perform the action on Linux and macOS runners. Windows is not currently supported.
44+
You can perform the action on Linux, macOS and Windows runners.
4545

4646
## 💙 Community & Support
4747

@@ -53,4 +53,6 @@ You can perform the action on Linux and macOS runners. Windows is not currently
5353

5454
1Password requests you practice responsible disclosure if you discover a vulnerability.
5555

56-
Please file requests by sending an email to bugbounty@agilebits.com.
56+
Please file requests via [**BugCrowd**](https://bugcrowd.com/agilebits).
57+
58+
For information about security practices, please visit our [Security homepage](https://bugcrowd.com/agilebits).

action.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ runs:
1212
using: composite
1313
steps:
1414
- shell: bash
15+
if: ${{ runner.os == 'Linux' || runner.os == 'macOS' }}
1516
env:
1617
OP_CLI_VERSION: ${{ inputs.version }}
1718
run: |
1819
${{ github.action_path }}/install-cli.sh
20+
- shell: pwsh
21+
if: ${{ runner.os == 'Windows' }}
22+
env:
23+
OP_CLI_VERSION: ${{ inputs.version }}
24+
run: |
25+
${{ github.action_path }}\install-cli.ps1

install-cli.ps1

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
$CLI_URL = "https://app-updates.agilebits.com/product_history/CLI2"
2+
3+
# Fetch the latest version of 1Password CLI (Stable or Beta)
4+
function Get-LatestCLIVersion {
5+
param(
6+
[ValidateSet("beta", "non_beta")]
7+
[string]$VersionType
8+
)
9+
10+
try {
11+
$html = Invoke-WebRequest -Uri $CLI_URL -UseBasicParsing
12+
13+
$versions = $html.Content |
14+
Select-String -Pattern '(?ms)<h3>(.*?)<\/h3>' -AllMatches |
15+
ForEach-Object { $_.Matches.Groups[1].Value -replace '\s+', '' -replace '<span.*?>|<\/span>|&nbsp;.*', '' }
16+
17+
# Find the correct version based on the type requested
18+
$latestVersion = $versions | Where-Object { $VersionType -eq "beta" -or ($_ -notmatch "beta") } | Select-Object -First 1
19+
20+
if (-not $latestVersion) {
21+
Write-Error "No suitable version found for type: $VersionType" -ErrorAction Stop
22+
}
23+
24+
return "v$latestVersion"
25+
} catch {
26+
Write-Error "Failed to fetch latest version: $_"
27+
exit 1
28+
}
29+
}
30+
31+
# Install 1Password CLI
32+
function Install-OPCLI {
33+
param(
34+
[string]$OP_CLI_VERSION
35+
)
36+
37+
Write-Host "Installing 1Password CLI version: $OP_CLI_VERSION"
38+
39+
if ($IsWindows) {
40+
# Get system architecture
41+
try {
42+
$WIN32_ARCH = (Get-CimInstance Win32_OperatingSystem).OSArchitecture
43+
} catch {
44+
Write-Error "Failed to detect OS architecture: $_"
45+
exit 1
46+
}
47+
48+
switch ($WIN32_ARCH) {
49+
'64-bit' { $ARCH = 'amd64' }
50+
'32-bit' { $ARCH = '386' }
51+
Default { Write-Error "Unsupported OS architecture: '$WIN32_ARCH'"; exit 1 }
52+
}
53+
54+
$DownloadUrl = "https://cache.agilebits.com/dist/1P/op2/pkg/$OP_CLI_VERSION/op_windows_${ARCH}_$OP_CLI_VERSION.zip"
55+
$InstallDir = Join-Path -Path $env:ProgramFiles -ChildPath '1Password CLI'
56+
57+
try {
58+
Invoke-WebRequest -Uri $DownloadUrl -OutFile op.zip
59+
Expand-Archive -Path op.zip -DestinationPath $InstallDir -Force
60+
Remove-Item -Path op.zip -Force
61+
} catch {
62+
Write-Error "Failed to install 1Password CLI: $_"
63+
exit 1
64+
}
65+
66+
# Add install path to system environment variables
67+
$EnvMachinePath = [System.Environment]::GetEnvironmentVariable('PATH', 'Machine')
68+
if ($EnvMachinePath -split ';' -notcontains $InstallDir) {
69+
[Environment]::SetEnvironmentVariable('PATH', "$EnvMachinePath;$InstallDir", 'Machine')
70+
}
71+
72+
Write-Output $InstallDir | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
73+
Write-Host "Installation completed successfully!"
74+
} else {
75+
Write-Error "This PowerShell script does not support non-Windows installations."
76+
exit 1
77+
}
78+
}
79+
80+
# Main script execution
81+
try {
82+
if ($env:OP_CLI_VERSION -eq "latest") {
83+
$OP_CLI_VERSION = Get-LatestCLIVersion -VersionType "non_beta"
84+
} elseif ($env:OP_CLI_VERSION -eq "latest-beta") {
85+
$OP_CLI_VERSION = Get-LatestCLIVersion -VersionType "beta"
86+
} else {
87+
$OP_CLI_VERSION = "v$env:OP_CLI_VERSION"
88+
}
89+
90+
Install-OPCLI -OP_CLI_VERSION $OP_CLI_VERSION
91+
} catch {
92+
Write-Error "Unexpected error occurred: $_"
93+
exit 1
94+
}

test/assert-version.ps1

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
$OP_CLI_VERSION = $args[0]
2+
$CLI_URL = "https://app-updates.agilebits.com/product_history/CLI2"
3+
4+
# Function to fetch the latest 1Password CLI version
5+
function Get-LatestCLIVersion {
6+
param(
7+
[ValidateSet("beta", "non_beta")]
8+
[string]$VersionType
9+
)
10+
11+
try {
12+
$html = Invoke-WebRequest -Uri $CLI_URL -UseBasicParsing
13+
14+
$versions = $html.Content |
15+
Select-String -Pattern '(?ms)<h3>(.*?)<\/h3>' -AllMatches |
16+
ForEach-Object { $_.Matches.Groups[1].Value -replace '\s+', '' -replace '<span.*?>|<\/span>|&nbsp;.*', '' }
17+
18+
# Find the latest version based on the requested type
19+
$latestVersion = $versions | Where-Object { $VersionType -eq "beta" -or ($_ -notmatch "beta") } | Select-Object -First 1
20+
21+
if (-not $latestVersion) {
22+
Write-Error "No suitable version found for type: $VersionType" -ErrorAction Stop
23+
}
24+
25+
return $latestVersion
26+
} catch {
27+
Write-Error "Failed to fetch latest version: $_"
28+
exit 1
29+
}
30+
}
31+
32+
# Determine the desired 1Password CLI version
33+
if ($OP_CLI_VERSION -eq "latest") {
34+
$OP_CLI_VERSION = Get-LatestCLIVersion -VersionType "non_beta"
35+
} elseif ($OP_CLI_VERSION -eq "latest-beta") {
36+
$OP_CLI_VERSION = Get-LatestCLIVersion -VersionType "beta"
37+
}
38+
39+
# Check if 1Password CLI is installed before running the version check
40+
if (-Not (Get-Command op -ErrorAction SilentlyContinue)) {
41+
Write-Error "1Password CLI ('op') is not installed or not found in PATH."
42+
exit 1
43+
}
44+
45+
# Compare the installed version with the expected version
46+
$installedVersion = op --version
47+
if ($installedVersion -ne $OP_CLI_VERSION) {
48+
Write-Output "Expected CLI version: $OP_CLI_VERSION"
49+
Write-Output "But found installed version: $installedVersion"
50+
exit 1
51+
}

0 commit comments

Comments
 (0)