Skip to content

Commit 69ff91e

Browse files
Refactor PowerShell installation: enhance version resolution and improve error handling for Linux and macOS; update Windows installation process to ensure proper version detection and installation.
1 parent 6db690c commit 69ff91e

File tree

1 file changed

+117
-79
lines changed

1 file changed

+117
-79
lines changed

action.yml

Lines changed: 117 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,138 @@
11
name: Install PowerShell
2-
description: Install a specific version of PowerShell Core on any GitHub runner (Linux, macOS, Windows). Installs only when the requested version is not already present.
2+
description: >-
3+
Install a specific version —or the latest stable version— of PowerShell Core
4+
on any GitHub runner (Linux, macOS, Windows).
5+
Skips the install if the requested version is already present.
36
author: PSModule
47
branding:
58
icon: terminal
69
color: purple
710

811
inputs:
912
Version:
10-
description: Exact PowerShell version to install (e.g. 7.4.1)
11-
required: true
13+
description: >-
14+
PowerShell version to install (`7.4.1`, `7.3.10`, or `latest` for the
15+
newest stable release).
16+
Defaults to **latest**.
17+
required: false
18+
default: latest
1219

1320
runs:
1421
using: composite
1522
steps:
16-
- name: Install PowerShell (Linux)
17-
if: runner.os == 'Linux'
18-
env:
19-
REQUESTED_VERSION: ${{ inputs.Version }}
20-
shell: bash
21-
run: |
22-
set -e
23-
DETECTED_VERSION=$(pwsh -NoLogo -NoProfile -Command '$PSVersionTable.PSVersion.ToString()' 2>/dev/null || true)
24-
if [[ "$DETECTED_VERSION" == "$REQUESTED_VERSION" ]]; then
25-
echo "PowerShell $DETECTED_VERSION already installed. Skipping."
26-
exit 0
23+
- name: Install PowerShell (Linux)
24+
if: runner.os == 'Linux'
25+
shell: bash
26+
env:
27+
REQUESTED_VERSION: ${{ inputs.Version }}
28+
run: |
29+
set -e
30+
31+
# Resolve "latest" to a concrete version number (e.g. 7.4.1)
32+
if [[ -z "$REQUESTED_VERSION" || "$REQUESTED_VERSION" == "latest" ]]; then
33+
REQUESTED_VERSION=$(curl -s https://api.github.com/repos/PowerShell/PowerShell/releases/latest \
34+
| grep -oP '"tag_name":\s*"\K([^"]+)' | sed 's/^v//')
35+
echo "Latest stable PowerShell release detected: $REQUESTED_VERSION"
36+
fi
37+
38+
DETECTED_VERSION=$(pwsh -NoLogo -NoProfile -Command '$PSVersionTable.PSVersion.ToString()' 2>/dev/null || true)
39+
if [[ "$DETECTED_VERSION" == "$REQUESTED_VERSION" ]]; then
40+
echo "PowerShell $DETECTED_VERSION already installed. Skipping."
41+
exit 0
42+
fi
43+
44+
if command -v apt-get >/dev/null; then
45+
echo "Using APT package manager (Debian/Ubuntu)…"
46+
47+
if ! grep -q "packages.microsoft.com" /etc/apt/sources.list /etc/apt/sources.list.d/* 2>/dev/null; then
48+
wget -qO- https://packages.microsoft.com/keys/microsoft.asc \
49+
| sudo tee /etc/apt/trusted.gpg.d/microsoft.asc > /dev/null
50+
DIST_CODENAME=$(lsb_release -cs)
51+
sudo add-apt-repository \
52+
"deb [arch=$(dpkg --print-architecture)] https://packages.microsoft.com/ubuntu/$DIST_CODENAME/prod $DIST_CODENAME main"
2753
fi
2854
29-
if command -v apt-get >/dev/null; then
30-
echo "Using APT package manager (Debian/Ubuntu)..."
31-
32-
if ! grep -q "packages.microsoft.com" /etc/apt/sources.list /etc/apt/sources.list.d/* 2>/dev/null; then
33-
wget -qO- https://packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc > /dev/null
34-
DIST_CODENAME=$(lsb_release -cs)
35-
sudo add-apt-repository "deb [arch=$(dpkg --print-architecture)] https://packages.microsoft.com/ubuntu/$DIST_CODENAME/prod $DIST_CODENAME main"
36-
fi
37-
38-
sudo apt-get update -y
39-
EXACT_PKG=$(apt-cache madison powershell | awk '{print $3}' | grep -E "^${REQUESTED_VERSION}(-|$)" | head -n1 || true)
40-
if [[ -n "$EXACT_PKG" ]]; then
41-
sudo apt-get install -y powershell=$EXACT_PKG
42-
else
43-
ARCH=$(dpkg --print-architecture)
44-
DEB_NAME="powershell_${REQUESTED_VERSION}-1.deb_${ARCH}.deb"
45-
URL="https://github.com/PowerShell/PowerShell/releases/download/v${REQUESTED_VERSION}/${DEB_NAME}"
46-
wget -q "$URL" -O "$DEB_NAME"
47-
sudo dpkg -i "$DEB_NAME" || sudo apt-get -f install -y
48-
fi
55+
sudo apt-get update -y
56+
EXACT_PKG=$(apt-cache madison powershell | awk '{print $3}' \
57+
| grep -E "^${REQUESTED_VERSION}(-|$)" | head -n1 || true)
58+
59+
if [[ -n "$EXACT_PKG" ]]; then
60+
sudo apt-get install -y powershell=$EXACT_PKG
4961
else
50-
echo "Unsupported Linux distribution (no apt-get)."
51-
exit 1
62+
ARCH=$(dpkg --print-architecture)
63+
DEB_NAME="powershell_${REQUESTED_VERSION}-1.deb_${ARCH}.deb"
64+
URL="https://github.com/PowerShell/PowerShell/releases/download/v${REQUESTED_VERSION}/${DEB_NAME}"
65+
wget -q "$URL" -O "$DEB_NAME"
66+
sudo dpkg -i "$DEB_NAME" || sudo apt-get -f install -y
5267
fi
68+
else
69+
echo "Unsupported Linux distribution (no apt-get)."
70+
exit 1
71+
fi
5372
54-
- name: Install PowerShell (macOS)
55-
if: runner.os == 'macOS'
56-
env:
57-
REQUESTED_VERSION: ${{ inputs.Version }}
58-
shell: bash
59-
run: |
60-
set -e
61-
DETECTED_VERSION=$(pwsh -NoLogo -NoProfile -Command '$PSVersionTable.PSVersion.ToString()' 2>/dev/null || true)
62-
if [[ "$DETECTED_VERSION" == "$REQUESTED_VERSION" ]]; then
63-
echo "PowerShell $DETECTED_VERSION already installed. Skipping."
64-
exit 0
65-
fi
73+
- name: Install PowerShell (macOS)
74+
if: runner.os == 'macOS'
75+
shell: bash
76+
env:
77+
REQUESTED_VERSION: ${{ inputs.Version }}
78+
run: |
79+
set -e
6680
67-
if brew info --cask powershell@$REQUESTED_VERSION >/dev/null 2>&1; then
68-
brew install --cask powershell@$REQUESTED_VERSION
81+
# Resolve "latest" to concrete version number
82+
if [[ -z "$REQUESTED_VERSION" || "$REQUESTED_VERSION" == "latest" ]]; then
83+
REQUESTED_VERSION=$(curl -s https://api.github.com/repos/PowerShell/PowerShell/releases/latest \
84+
| grep -oP '"tag_name":\s*"\K([^"]+)' | sed 's/^v//')
85+
echo "Latest stable PowerShell release detected: $REQUESTED_VERSION"
86+
fi
87+
88+
DETECTED_VERSION=$(pwsh -NoLogo -NoProfile -Command '$PSVersionTable.PSVersion.ToString()' 2>/dev/null || true)
89+
if [[ "$DETECTED_VERSION" == "$REQUESTED_VERSION" ]]; then
90+
echo "PowerShell $DETECTED_VERSION already installed. Skipping."
91+
exit 0
92+
fi
93+
94+
if brew info --cask powershell@$REQUESTED_VERSION >/dev/null 2>&1; then
95+
brew install --cask powershell@$REQUESTED_VERSION
96+
else
97+
ARCH=$(uname -m)
98+
if [[ "$ARCH" == "arm64" ]]; then
99+
PKG_NAME="powershell-${REQUESTED_VERSION}-osx-arm64.pkg"
69100
else
70-
ARCH=$(uname -m)
71-
if [[ "$ARCH" == "arm64" ]]; then PKG_NAME="powershell-${REQUESTED_VERSION}-osx-arm64.pkg"; else PKG_NAME="powershell-${REQUESTED_VERSION}-osx-x64.pkg"; fi
72-
URL="https://github.com/PowerShell/PowerShell/releases/download/v${REQUESTED_VERSION}/${PKG_NAME}"
73-
curl -sSL "$URL" -o "$PKG_NAME"
74-
sudo installer -pkg "$PKG_NAME" -target /
101+
PKG_NAME="powershell-${REQUESTED_VERSION}-osx-x64.pkg"
75102
fi
103+
URL="https://github.com/PowerShell/PowerShell/releases/download/v${REQUESTED_VERSION}/${PKG_NAME}"
104+
curl -sSL "$URL" -o "$PKG_NAME"
105+
sudo installer -pkg "$PKG_NAME" -target /
106+
fi
107+
108+
- name: Install PowerShell (Windows)
109+
if: runner.os == 'Windows'
110+
shell: powershell
111+
env:
112+
REQUESTED_VERSION: ${{ inputs.Version }}
113+
run: |
114+
# Resolve "latest" to concrete version number
115+
if (-not $env:REQUESTED_VERSION -or $env:REQUESTED_VERSION -eq 'latest') {
116+
$latest = (Invoke-RestMethod -Uri 'https://api.github.com/repos/PowerShell/PowerShell/releases/latest').tag_name.TrimStart('v')
117+
Write-Host "Latest stable PowerShell release detected: $latest"
118+
$env:REQUESTED_VERSION = $latest
119+
}
120+
121+
try {
122+
$detected = (pwsh -NoLogo -NoProfile -Command '$PSVersionTable.PSVersion.ToString()')
123+
} catch {
124+
$detected = $null
125+
}
126+
127+
Write-Host "Detected PowerShell version: $detected"
128+
Write-Host "Requested PowerShell version: $env:REQUESTED_VERSION"
129+
130+
if ($detected -eq $env:REQUESTED_VERSION) {
131+
Write-Host "PowerShell $detected already installed. Skipping."
132+
exit 0
133+
}
76134
77-
- name: Install PowerShell (Windows)
78-
if: runner.os == 'Windows'
79-
env:
80-
REQUESTED_VERSION: ${{ inputs.Version }}
81-
shell: powershell
82-
run: |
83-
try {
84-
$detected = (pwsh -NoLogo -NoProfile -Command '$PSVersionTable.PSVersion.ToString()')
85-
} catch {
86-
$detected = $null
87-
}
88-
89-
Write-Host "Detected PowerShell version: $detected"
90-
Write-Host "Requested PowerShell version: $env:REQUESTED_VERSION"
91-
92-
if ($detected -eq $env:REQUESTED_VERSION) {
93-
Write-Host "PowerShell $detected already installed. Skipping."
94-
exit 0
95-
}
96-
97-
$msi = "PowerShell-$($env:REQUESTED_VERSION)-win-x64.msi"
98-
$url = "https://github.com/PowerShell/PowerShell/releases/download/v$($env:REQUESTED_VERSION)/$msi"
99-
Invoke-WebRequest -Uri $url -OutFile $msi -UseBasicParsing
100-
Start-Process msiexec.exe -ArgumentList '/i', $msi, '/quiet', '/norestart' -Wait
135+
$msi = "PowerShell-$($env:REQUESTED_VERSION)-win-x64.msi"
136+
$url = "https://github.com/PowerShell/PowerShell/releases/download/v$($env:REQUESTED_VERSION)/$msi"
137+
Invoke-WebRequest -Uri $url -OutFile $msi -UseBasicParsing
138+
Start-Process msiexec.exe -ArgumentList '/i', $msi, '/quiet', '/norestart' -Wait

0 commit comments

Comments
 (0)