@@ -22,10 +22,12 @@ runs:
22
22
- name : Install PowerShell (Linux)
23
23
if : runner.os == 'Linux'
24
24
shell : bash
25
+ working-directory : ${{ github.action_path }}
25
26
env :
26
27
REQUESTED_VERSION : ${{ inputs.Version }}
27
28
GITHUB_TOKEN : ${{ github.token }}
28
29
run : |
30
+ # Install-PowerShell
29
31
set -e
30
32
31
33
echo "Requested version: [$REQUESTED_VERSION]"
@@ -50,121 +52,127 @@ runs:
50
52
esac
51
53
52
54
DETECTED_VERSION=$(pwsh -NoLogo -NoProfile -Command '$PSVersionTable.PSVersion.ToString()' 2>/dev/null || true)
55
+ if [[ -n "$DETECTED_VERSION" ]]; then
56
+ echo "Currently installed PowerShell version: $DETECTED_VERSION"
57
+ else
58
+ echo "PowerShell is not currently installed"
59
+ fi
60
+
53
61
if [[ "$DETECTED_VERSION" == "$REQUESTED_VERSION" ]]; then
54
62
echo "PowerShell $DETECTED_VERSION already installed. Skipping."
55
63
exit 0
56
64
fi
57
65
58
- if command -v apt-get >/dev/null; then
59
- echo "Using APT package manager (Debian/Ubuntu)..."
66
+ # Determine Linux distribution type
67
+ ARCH=$(dpkg --print-architecture 2>/dev/null || rpm --eval '%{_arch}' 2>/dev/null || echo "x86_64")
60
68
61
- if ! grep -q "packages.microsoft.com" /etc/apt/sources.list /etc/apt/sources.list.d/* 2>/dev/null; then
62
- wget -qO- https://packages.microsoft.com/keys/microsoft.asc \
63
- | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc > /dev/null
64
- DIST_CODENAME=$(lsb_release -cs)
65
- sudo add-apt-repository \
66
- "deb [arch=$(dpkg --print-architecture)] https://packages.microsoft.com/ubuntu/$DIST_CODENAME/prod $DIST_CODENAME main"
67
- fi
68
-
69
- sudo apt-get update -y
70
- EXACT_PKG=$(apt-cache madison powershell | awk '{print $3}' \
71
- | grep -E "^${REQUESTED_VERSION}(-|$)" | head -n1 || true)
72
-
73
- if [[ -n "$EXACT_PKG" ]]; then
74
- sudo apt-get install -y powershell=$EXACT_PKG
69
+ if command -v apt-get >/dev/null || command -v dpkg >/dev/null; then
70
+ # Debian/Ubuntu based
71
+ echo "Detected Debian/Ubuntu based system..."
72
+ DEB_NAME="powershell_${REQUESTED_VERSION}-1.deb_${ARCH}.deb"
73
+ URL="https://github.com/PowerShell/PowerShell/releases/download/v${REQUESTED_VERSION}/${DEB_NAME}"
74
+ echo "Downloading from: $URL"
75
+ wget -q "$URL" -O "$DEB_NAME"
76
+ echo "Starting installation of PowerShell [$REQUESTED_VERSION)]..."
77
+ sudo dpkg -i "$DEB_NAME" || sudo apt-get -f install -y
78
+ echo "Installation complete. PowerShell [$REQUESTED_VERSION] is now available."
79
+ elif command -v rpm >/dev/null; then
80
+ # RHEL/Fedora/CentOS based
81
+ echo "Detected RHEL/Fedora/CentOS based system..."
82
+
83
+ if [[ "$ARCH" == "aarch64" ]]; then
84
+ RPM_NAME="powershell-${REQUESTED_VERSION}-1.rh.${ARCH}.rpm"
75
85
else
76
- ARCH=$(dpkg --print-architecture)
77
- DEB_NAME="powershell_${REQUESTED_VERSION}-1.deb_${ARCH}.deb"
78
- URL="https://github.com/PowerShell/PowerShell/releases/download/v${REQUESTED_VERSION}/${DEB_NAME}"
79
- wget -q "$URL" -O "$DEB_NAME"
80
- sudo dpkg -i "$DEB_NAME" || sudo apt-get -f install -y
86
+ RPM_NAME="powershell-${REQUESTED_VERSION}-1.rh.x86_64.rpm"
81
87
fi
88
+
89
+ URL="https://github.com/PowerShell/PowerShell/releases/download/v${REQUESTED_VERSION}/${RPM_NAME}"
90
+ echo "Downloading from: $URL"
91
+ wget -q "$URL" -O "$RPM_NAME"
92
+ echo "Starting installation of PowerShell [$REQUESTED_VERSION)]..."
93
+ sudo rpm -i "$RPM_NAME" || sudo yum install -y "$RPM_NAME"
94
+ echo "Installation complete. PowerShell [$REQUESTED_VERSION] is now available."
82
95
else
83
- echo "Unsupported Linux distribution (no apt-get) ."
96
+ echo "Unsupported Linux distribution. Cannot determine package format ."
84
97
exit 1
85
98
fi
99
+ echo "PowerShell [$REQUESTED_VERSION] installed successfully."
86
100
87
101
- name : Install PowerShell (macOS)
88
102
if : runner.os == 'macOS'
89
103
shell : bash
104
+ working-directory : ${{ github.action_path }}
90
105
env :
91
106
REQUESTED_VERSION : ${{ inputs.Version }}
92
107
GITHUB_TOKEN : ${{ github.token }}
93
108
run : |
109
+ # Install-PowerShell
94
110
set -e
95
111
96
112
echo "Requested version: [$REQUESTED_VERSION]"
97
113
98
- # Convert to lowercase for comparison (macOS-compatible method)
99
- REQ_VER_LOWER=$(echo "$REQUESTED_VERSION" | tr '[:upper:]' '[:lower:]')
100
-
101
- # Only resolve to latest version if explicitly set to 'latest'
102
- if [[ "$REQ_VER_LOWER" == "latest" ]]; then
103
- REQUESTED_VERSION=$(
104
- curl -s -f \
105
- -H "Accept: application/vnd.github+json" \
106
- -H "Authorization: Bearer $GITHUB_TOKEN" \
107
- -H "X-GitHub-Api-Version: 2022-11-28" \
108
- https://api.github.com/repos/PowerShell/PowerShell/releases/latest |
109
- jq -r '.tag_name' | sed 's/^v//'
110
- )
111
- echo "Latest stable PowerShell release detected: $REQUESTED_VERSION"
112
- fi
114
+ # Only resolve to latest version if explicitly set to 'latest' (case-insensitive)
115
+ case "${REQUESTED_VERSION:-}" in
116
+ [Ll][Aa][Tt][Ee][Ss][Tt])
117
+ REQUESTED_VERSION=$(
118
+ curl -s -f \
119
+ -H "Accept: application/vnd.github+json" \
120
+ -H "Authorization: Bearer $GITHUB_TOKEN" \
121
+ -H "X-GitHub-Api-Version: 2022-11-28" \
122
+ https://api.github.com/repos/PowerShell/PowerShell/releases/latest |
123
+ jq -r '.tag_name' | sed 's/^v//'
124
+ )
125
+ echo "Latest stable PowerShell release detected: $REQUESTED_VERSION"
126
+ ;;
127
+ "")
128
+ echo "Error: Version input is required (or use 'latest')"
129
+ exit 1
130
+ ;;
131
+ esac
113
132
114
- # Validate REQUESTED_VERSION is not empty
115
- if [[ -z "${REQUESTED_VERSION}" ]]; then
116
- echo "Error: Could not determine a valid PowerShell version."
117
- exit 1
133
+ DETECTED_VERSION=$(pwsh -NoLogo -NoProfile -Command '$PSVersionTable.PSVersion.ToString()' 2>/dev/null || true)
134
+ if [[ -n "$DETECTED_VERSION" ]]; then
135
+ echo "Currently installed PowerShell version: $DETECTED_VERSION"
136
+ else
137
+ echo "PowerShell is not currently installed"
118
138
fi
119
139
120
- DETECTED_VERSION=$(pwsh -NoLogo -NoProfile -Command '$PSVersionTable.PSVersion.ToString()' 2>/dev/null || true)
121
140
if [[ "$DETECTED_VERSION" == "$REQUESTED_VERSION" ]]; then
122
141
echo "PowerShell $DETECTED_VERSION already installed. Skipping."
123
142
exit 0
124
143
fi
125
144
126
- # Try Homebrew first (simplified approach)
127
- if command -v brew >/dev/null; then
128
- echo "Using Homebrew package manager..."
129
- # Homebrew handles 'latest' automatically when no version is specified
130
- if [[ "$REQ_VER_LOWER" == "latest" ]]; then
131
- brew install --cask powershell
132
- else
133
- # Try specific version formula first
134
- if brew install --cask "powershell@$REQUESTED_VERSION" 2>/dev/null; then
135
- echo "Successfully installed via Homebrew"
136
- else
137
- # Fall back to generic formula if versioned one doesn't exist
138
- echo "Version-specific formula not found, installing latest via Homebrew"
139
- brew install --cask powershell
140
- fi
141
- fi
142
- else
143
- # Fall back to direct download
144
- echo "Homebrew not available, downloading directly..."
145
- ARCH=$(uname -m)
146
- case "$ARCH" in
147
- "arm64") PKG_NAME="powershell-${REQUESTED_VERSION}-osx-arm64.pkg" ;;
148
- *) PKG_NAME="powershell-${REQUESTED_VERSION}-osx-x64.pkg" ;;
149
- esac
150
-
151
- URL="https://github.com/PowerShell/PowerShell/releases/download/v${REQUESTED_VERSION}/${PKG_NAME}"
152
- echo "Downloading from: $URL"
153
- if ! curl -sSL "$URL" -o "$PKG_NAME"; then
154
- echo "Error: Failed to download PowerShell package"
155
- exit 1
156
- fi
157
- sudo installer -pkg "$PKG_NAME" -target /
145
+ # Determine architecture and download appropriate package
146
+ ARCH=$(uname -m)
147
+ case "$ARCH" in
148
+ "arm64") PKG_NAME="powershell-${REQUESTED_VERSION}-osx-arm64.pkg" ;;
149
+ *) PKG_NAME="powershell-${REQUESTED_VERSION}-osx-x64.pkg" ;;
150
+ esac
151
+
152
+ URL="https://github.com/PowerShell/PowerShell/releases/download/v${REQUESTED_VERSION}/${PKG_NAME}"
153
+ echo "Downloading from: $URL"
154
+
155
+ echo "Starting installation of PowerShell [$REQUESTED_VERSION]..."
156
+
157
+ if ! curl -sSL "$URL" -o "$PKG_NAME"; then
158
+ echo "Error: Failed to download PowerShell package"
159
+ exit 1
158
160
fi
161
+ sudo installer -pkg "$PKG_NAME" -target /
162
+
163
+ echo "Installation complete. PowerShell [$REQUESTED_VERSION] is now available."
159
164
160
165
- name : Install PowerShell (Windows)
161
166
if : runner.os == 'Windows'
162
167
shell : powershell
168
+ working-directory : ${{ github.action_path }}
163
169
env :
164
170
REQUESTED_VERSION : ${{ inputs.Version }}
165
171
GITHUB_TOKEN : ${{ github.token }}
166
172
run : |
167
- Write-Host "Requested version: [$REQUESTED_VERSION]"
173
+ # Install-PowerShell
174
+ Write-Host "Requested version: [$env:REQUESTED_VERSION]"
175
+
168
176
# Only resolve to latest version if explicitly set to 'latest' (case-insensitive)
169
177
$req = $env:REQUESTED_VERSION
170
178
if ($req -and $req.Trim().ToLower() -eq 'latest') {
@@ -185,19 +193,27 @@ runs:
185
193
186
194
try {
187
195
$detected = (pwsh -NoLogo -NoProfile -Command '$PSVersionTable.PSVersion.ToString()')
196
+ Write-Host "Currently installed PowerShell version: $detected"
188
197
} catch {
198
+ Write-Host "PowerShell is not currently installed"
189
199
$detected = $null
190
200
}
191
201
192
- Write-Host "Detected PowerShell version: $detected"
193
- Write-Host "Requested PowerShell version: $env:REQUESTED_VERSION"
194
-
195
202
if ($detected -eq $env:REQUESTED_VERSION) {
196
203
Write-Host "PowerShell $detected already installed. Skipping."
197
204
exit 0
198
205
}
199
206
200
207
$msi = "PowerShell-$($env:REQUESTED_VERSION)-win-x64.msi"
201
208
$url = "https://github.com/PowerShell/PowerShell/releases/download/v$($env:REQUESTED_VERSION)/$msi"
202
- Invoke-WebRequest -Uri $url -OutFile $msi -UseBasicParsing
209
+ Write-Host "Downloading from: $url"
210
+
211
+ Write-Host "Starting installation of PowerShell [$($env:REQUESTED_VERSION)]..."
212
+
213
+ if (-not (Invoke-WebRequest -Uri $url -OutFile $msi -UseBasicParsing -PassThru)) {
214
+ Write-Host "Error: Failed to download PowerShell package"
215
+ exit 1
216
+ }
203
217
Start-Process msiexec.exe -ArgumentList '/i', $msi, '/quiet', '/norestart' -Wait
218
+
219
+ Write-Host "Installation complete. PowerShell [$($env:REQUESTED_VERSION)] is now available."
0 commit comments