Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 55 additions & 76 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,32 +55,33 @@ runs:
exit 0
fi

if command -v apt-get >/dev/null; then
echo "Using APT package manager (Debian/Ubuntu)..."

if ! grep -q "packages.microsoft.com" /etc/apt/sources.list /etc/apt/sources.list.d/* 2>/dev/null; then
wget -qO- https://packages.microsoft.com/keys/microsoft.asc \
| sudo tee /etc/apt/trusted.gpg.d/microsoft.asc > /dev/null
DIST_CODENAME=$(lsb_release -cs)
sudo add-apt-repository \
"deb [arch=$(dpkg --print-architecture)] https://packages.microsoft.com/ubuntu/$DIST_CODENAME/prod $DIST_CODENAME main"
fi

sudo apt-get update -y
EXACT_PKG=$(apt-cache madison powershell | awk '{print $3}' \
| grep -E "^${REQUESTED_VERSION}(-|$)" | head -n1 || true)

if [[ -n "$EXACT_PKG" ]]; then
sudo apt-get install -y powershell=$EXACT_PKG
# Determine Linux distribution type
ARCH=$(dpkg --print-architecture 2>/dev/null || rpm --eval '%{_arch}' 2>/dev/null || echo "x86_64")

if command -v apt-get >/dev/null || command -v dpkg >/dev/null; then
# Debian/Ubuntu based
echo "Detected Debian/Ubuntu based system..."
DEB_NAME="powershell_${REQUESTED_VERSION}-1.deb_${ARCH}.deb"
URL="https://github.com/PowerShell/PowerShell/releases/download/v${REQUESTED_VERSION}/${DEB_NAME}"
echo "Downloading from: $URL"
wget -q "$URL" -O "$DEB_NAME"
sudo dpkg -i "$DEB_NAME" || sudo apt-get -f install -y
elif command -v rpm >/dev/null; then
# RHEL/Fedora/CentOS based
echo "Detected RHEL/Fedora/CentOS based system..."

if [[ "$ARCH" == "aarch64" ]]; then
RPM_NAME="powershell-${REQUESTED_VERSION}-1.rh.${ARCH}.rpm"
else
ARCH=$(dpkg --print-architecture)
DEB_NAME="powershell_${REQUESTED_VERSION}-1.deb_${ARCH}.deb"
URL="https://github.com/PowerShell/PowerShell/releases/download/v${REQUESTED_VERSION}/${DEB_NAME}"
wget -q "$URL" -O "$DEB_NAME"
sudo dpkg -i "$DEB_NAME" || sudo apt-get -f install -y
RPM_NAME="powershell-${REQUESTED_VERSION}-1.rh.x86_64.rpm"
fi

URL="https://github.com/PowerShell/PowerShell/releases/download/v${REQUESTED_VERSION}/${RPM_NAME}"
echo "Downloading from: $URL"
wget -q "$URL" -O "$RPM_NAME"
sudo rpm -i "$RPM_NAME" || sudo yum install -y "$RPM_NAME"
Comment thread
MariusStorhaug marked this conversation as resolved.
else
echo "Unsupported Linux distribution (no apt-get)."
echo "Unsupported Linux distribution. Cannot determine package format."
exit 1
fi

Expand All @@ -95,67 +96,45 @@ runs:

echo "Requested version: [$REQUESTED_VERSION]"

# Convert to lowercase for comparison (macOS-compatible method)
REQ_VER_LOWER=$(echo "$REQUESTED_VERSION" | tr '[:upper:]' '[:lower:]')

# Only resolve to latest version if explicitly set to 'latest'
if [[ "$REQ_VER_LOWER" == "latest" ]]; then
REQUESTED_VERSION=$(
curl -s -f \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/PowerShell/PowerShell/releases/latest |
jq -r '.tag_name' | sed 's/^v//'
)
echo "Latest stable PowerShell release detected: $REQUESTED_VERSION"
fi

# Validate REQUESTED_VERSION is not empty
if [[ -z "${REQUESTED_VERSION}" ]]; then
echo "Error: Could not determine a valid PowerShell version."
exit 1
fi
# Only resolve to latest version if explicitly set to 'latest' (case-insensitive)
case "${REQUESTED_VERSION:-}" in
[Ll][Aa][Tt][Ee][Ss][Tt])
REQUESTED_VERSION=$(
curl -s -f \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/PowerShell/PowerShell/releases/latest |
jq -r '.tag_name' | sed 's/^v//'
)
echo "Latest stable PowerShell release detected: $REQUESTED_VERSION"
;;
"")
echo "Error: Version input is required (or use 'latest')"
exit 1
;;
esac

DETECTED_VERSION=$(pwsh -NoLogo -NoProfile -Command '$PSVersionTable.PSVersion.ToString()' 2>/dev/null || true)
if [[ "$DETECTED_VERSION" == "$REQUESTED_VERSION" ]]; then
echo "PowerShell $DETECTED_VERSION already installed. Skipping."
exit 0
fi

# Try Homebrew first (simplified approach)
if command -v brew >/dev/null; then
echo "Using Homebrew package manager..."
# Homebrew handles 'latest' automatically when no version is specified
if [[ "$REQ_VER_LOWER" == "latest" ]]; then
brew install --cask powershell
else
# Try specific version formula first
if brew install --cask "powershell@$REQUESTED_VERSION" 2>/dev/null; then
echo "Successfully installed via Homebrew"
else
# Fall back to generic formula if versioned one doesn't exist
echo "Version-specific formula not found, installing latest via Homebrew"
brew install --cask powershell
fi
fi
else
# Fall back to direct download
echo "Homebrew not available, downloading directly..."
ARCH=$(uname -m)
case "$ARCH" in
"arm64") PKG_NAME="powershell-${REQUESTED_VERSION}-osx-arm64.pkg" ;;
*) PKG_NAME="powershell-${REQUESTED_VERSION}-osx-x64.pkg" ;;
esac

URL="https://github.com/PowerShell/PowerShell/releases/download/v${REQUESTED_VERSION}/${PKG_NAME}"
echo "Downloading from: $URL"
if ! curl -sSL "$URL" -o "$PKG_NAME"; then
echo "Error: Failed to download PowerShell package"
exit 1
fi
sudo installer -pkg "$PKG_NAME" -target /
# Determine architecture and download appropriate package
ARCH=$(uname -m)
case "$ARCH" in
"arm64") PKG_NAME="powershell-${REQUESTED_VERSION}-osx-arm64.pkg" ;;
*) PKG_NAME="powershell-${REQUESTED_VERSION}-osx-x64.pkg" ;;
esac

URL="https://github.com/PowerShell/PowerShell/releases/download/v${REQUESTED_VERSION}/${PKG_NAME}"
echo "Downloading from: $URL"
if ! curl -sSL "$URL" -o "$PKG_NAME"; then
echo "Error: Failed to download PowerShell package"
exit 1
fi
sudo installer -pkg "$PKG_NAME" -target /

- name: Install PowerShell (Windows)
if: runner.os == 'Windows'
Expand Down