mirror of
https://github.com/ivuorinen/actions.git
synced 2026-01-26 03:23:59 +00:00
76 lines
2.5 KiB
YAML
76 lines
2.5 KiB
YAML
# yaml-language-server: $schema=https://json.schemastore.org/github-action.json
|
|
# permissions:
|
|
# - contents: read # Required for reading version files
|
|
---
|
|
name: Python Version Detect
|
|
description: 'Detects Python version from project configuration files or defaults to a specified version.'
|
|
author: 'Ismo Vuorinen'
|
|
|
|
branding:
|
|
icon: code
|
|
color: blue
|
|
|
|
inputs:
|
|
default-version:
|
|
description: 'Default Python version to use if no version is detected.'
|
|
required: false
|
|
default: '3.12'
|
|
token:
|
|
description: 'GitHub token for authentication'
|
|
required: false
|
|
default: ''
|
|
|
|
outputs:
|
|
python-version:
|
|
description: 'Detected or default Python version.'
|
|
value: ${{ steps.parse-version.outputs.detected-version }}
|
|
|
|
runs:
|
|
using: composite
|
|
steps:
|
|
- name: Validate Inputs
|
|
id: validate
|
|
shell: bash
|
|
env:
|
|
DEFAULT_VERSION: ${{ inputs.default-version }}
|
|
run: |
|
|
set -euo pipefail
|
|
|
|
# Validate default-version format
|
|
if ! [[ "$DEFAULT_VERSION" =~ ^[0-9]+\.[0-9]+(\.[0-9]+)?$ ]]; then
|
|
echo "::error::Invalid default-version format: '$DEFAULT_VERSION'. Expected format: X.Y or X.Y.Z (e.g., 3.12, 3.11.5)"
|
|
exit 1
|
|
fi
|
|
|
|
# Check for reasonable version range (prevent malicious inputs)
|
|
major_version=$(echo "$DEFAULT_VERSION" | cut -d'.' -f1)
|
|
if [ "$major_version" -ne 3 ]; then
|
|
echo "::error::Invalid default-version: '$DEFAULT_VERSION'. Python major version should be 3"
|
|
exit 1
|
|
fi
|
|
|
|
# Check minor version range for Python 3
|
|
minor_version=$(echo "$DEFAULT_VERSION" | cut -d'.' -f2)
|
|
if [ "$minor_version" -lt 8 ] || [ "$minor_version" -gt 15 ]; then
|
|
echo "::error::Invalid default-version: '$DEFAULT_VERSION'. Python 3 minor version should be between 8 and 15"
|
|
exit 1
|
|
fi
|
|
|
|
echo "Input validation completed successfully"
|
|
|
|
- name: Checkout Repository
|
|
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
|
with:
|
|
token: ${{ inputs.token || github.token }}
|
|
|
|
- name: Parse Python Version
|
|
id: parse-version
|
|
uses: ivuorinen/actions/version-file-parser@7061aafd35a2f21b57653e34f2b634b2a19334a9
|
|
with:
|
|
language: 'python'
|
|
tool-versions-key: 'python'
|
|
dockerfile-image: 'python'
|
|
version-file: '.python-version'
|
|
validation-regex: '^[0-9]+\.[0-9]+(\.[0-9]+)?$'
|
|
default-version: ${{ inputs.default-version }}
|