diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..8eaa13b --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,2 @@ +github: kishaningithub +custom: https://www.paypal.me/kishansh diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 270bf43..b8226a0 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,30 +1,33 @@ name: Test -on: [ push ] +on: + push: + branches: [main] + pull_request: + branches: [main] jobs: - test_setup_python: + test_python_installation: strategy: + fail-fast: false matrix: # This tests the amazon linux versions receiving standard support # Refer - https://endoflife.date/amazon-linux amazon-linux-version: [ 2, 2023 ] # This tests the lowest supported python version and the latest stable python version # Refer - https://devguide.python.org/versions/#status-of-python-versions - python-version: - - 3.8.18 - - 3.12.0 - cache: - - true - - false + python-version: [ 3.8.18, 3.12.0 ] + cache: [ true, false ] runs-on: ubuntu-latest container: amazonlinux:${{ matrix.amazon-linux-version }} steps: - name: Setup runner run: | - yum install -y git tar gzip sudo which + yum install -y git sudo tar gzip which - - uses: actions/checkout@v3 + - name: Checkout + run: | + git clone --depth 1 -b "${GITHUB_HEAD_REF:-$GITHUB_REF_NAME}" "https://github.com/${GITHUB_REPOSITORY}.git" . - name: Install python uses: ./ @@ -48,44 +51,71 @@ jobs: python --version 2>&1 | grep -F "${{ matrix.python-version }}" test "$(python3 -m pip --version)" = "$(pip --version)" - test_major_version_specification: + test_pip_installs: + strategy: + fail-fast: false + matrix: + amazon-linux-version: [ 2, 2023 ] + python-version: [ "3.8", "3.9", "3.10", "3.11" , "3.12" ] + cache: [ true, false ] runs-on: ubuntu-latest - container: amazonlinux:2023 + container: amazonlinux:${{ matrix.amazon-linux-version }} steps: - name: Setup runner run: | - yum install -y git tar gzip sudo + yum install -y git sudo tar gzip which - - uses: actions/checkout@v3 + - name: Checkout + run: | + git clone --depth 1 -b "${GITHUB_HEAD_REF:-$GITHUB_REF_NAME}" "https://github.com/${GITHUB_REPOSITORY}.git" . - name: Install python uses: ./ with: - python-version: 3 + python-version: "${{ matrix.python-version }}" + cache: ${{ matrix.cache }} - name: Test installation run: | set -x - - python3 --version 2>&1 | grep -F "3.12.0" - test_minor_version_specification: + pip install requests + pip3 install s4cmd + pip3 install boto3 + + test_python_version_file: runs-on: ubuntu-latest container: amazonlinux:2023 steps: - name: Setup runner run: | - yum install -y git tar gzip sudo + yum install -y git sudo tar gzip which + + - name: Checkout + run: | + git clone --depth 1 -b "${GITHUB_HEAD_REF:-$GITHUB_REF_NAME}" "https://github.com/${GITHUB_REPOSITORY}.git" . - - uses: actions/checkout@v3 + - name: Create python version file + run: | + echo '3.11.5' > .python-version - name: Install python uses: ./ with: - python-version: 3.9 + python-version-file: ".python-version" - name: Test installation run: | set -x - - python3 --version 2>&1 | grep -F "3.9.18" + + which python3 + which python + + python3 --version + python --version + + python3 --version 2>&1 | grep -F "3.11.5" + test "$(python3 -m pip --version)" = "$(pip3 --version)" + + python --version 2>&1 | grep -F "3.11.5" + test "$(python3 -m pip --version)" = "$(pip --version)" diff --git a/LICENSE b/LICENSE index 5450814..1076c56 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2023 Kishan B +Copyright (c) Kishan B Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index f19f0a7..03fa0dd 100644 --- a/README.md +++ b/README.md @@ -17,14 +17,32 @@ All you have to do is change "setup-python" to "setup-python-amazon-linux" in yo ```yaml steps: - uses: actions/checkout@v3 -- uses: actions/setup-python-amazon-linux@v1 +- uses: kishaningithub/setup-python-amazon-linux@v1 with: python-version: '3.10' - run: python my_script.py ``` +The `python-version` input is optional. If not supplied, the action will try to resolve the version from the default +`.python-version` file. Highly recommend always setting Python version explicitly using the `python-version` or +`python-version-file` inputs. + +```yaml +steps: +- uses: kishaningithub/setup-python-amazon-linux@v1 + with: + python-version-file: '.python-version' +``` + For more options to the action, kindly refer [action.yml](./action.yml) +## Supported amazon linux flavours + +- Amazon Linux 2 +- Amazon Linux 2023 + +If you need support for more kindly raise an issue or a PR + ## Implementation caveat This action compiles the desired version of python from source for the first time because the glibc version in use in @@ -38,13 +56,6 @@ python: /lib64/libc.so.6: version `GLIBC_2.28' not found Hence, only for the first time this action may like 3 to 4 minutes depending on the runner configuration. Post this only cache will be used and hence will be very fast (4 to 5 seconds) -## Supported amazon linux flavours - -- Amazon Linux 2 -- Amazon Linux 2023 - -If you need support for more kindly raise an issue or a PR - ## Contributing Contributions are most welcome! See [CONTRIBUTING.md](./CONTRIBUTING.md) diff --git a/action.yml b/action.yml index acd59e6..3073d93 100644 --- a/action.yml +++ b/action.yml @@ -2,66 +2,74 @@ name: 'Setup python amazon linux' description: 'setup-python action for amazon linux self hosted runners' inputs: python-version: + description: 'Version of python to be installed. Reads from .python-version if unset.' + python-version-file: description: 'Version of python to be installed' - required: true + default: '.python-version' cache: - description: Used to specify whether caching is needed. Set to true, if you'd like to enable caching. - required: true + description: > + [Deprecated] Used to specify whether caching is needed. Set to true, if you'd like to enable caching. + Current implementation uses uv which pulls in pre-compiled binaries of python thereby eliminating the + need to cache compiled python artifacts. default: 'true' + runs: using: "composite" steps: - - name: Ensure dependencies of python are installed + - name: Ensure system dependencies are installed shell: bash run: | - sudo yum groupinstall -y "Development Tools" - sudo yum install -y gcc openssl-devel bzip2-devel libffi-devel - sudo yum install -y tar gzip wget + sudo yum install -y tar gzip which - - name: Find exact python version - id: find-exact-python-version + - name: Install uv shell: bash run: | - exact_python_version=$(./find-exact-python-version.sh "${{ inputs.python-version }}") - echo "exact_python_version=${exact_python_version}" >> $GITHUB_OUTPUT + installation_directory="${{ github.action_path }}/.setup-python-amazon-linux/uv" + echo "Installing uv.. installation_directory=${installation_directory}" + uv_version="0.8.18" + # HOME is set to foobar till this is resolved https://github.com/astral-sh/uv/issues/6965#issuecomment-2915796022 + curl -LsSf "https://github.com/astral-sh/uv/releases/download/${uv_version}/uv-installer.sh" | HOME="foobar" UV_UNMANAGED_INSTALL="${installation_directory}" bash --login + echo "${installation_directory}" >> "${GITHUB_PATH}" + + - name: Find desired python version + id: find-desired-python-version + shell: bash + run: | + desired_python_version=$(${GITHUB_ACTION_PATH}/find-desired-python-version.sh "${{ inputs.python-version }}" "${{ inputs.python-version-file }}") + echo "desired_python_version=${desired_python_version}" | tee -a "${GITHUB_OUTPUT}" - name: Set installation directory id: set-installation-directory shell: bash run: | - exact_python_version="${{ steps.find-exact-python-version.outputs.exact_python_version }}" - echo "installation_directory=~/setup-python-amazon-linux/.python-versions/${exact_python_version}" >> $GITHUB_OUTPUT - - - name: Cache - id: cache-python - uses: actions/cache@v3 - if: inputs.cache == 'true' - with: - path: | - ${{ steps.set-installation-directory.outputs.installation_directory }} - key: python-${{ steps.find-exact-python-version.outputs.exact_python_version }}-${{ runner.arch }} + desired_python_version="${{ steps.find-desired-python-version.outputs.desired_python_version }}" + echo "installation_directory=${HOME}/.setup-python-amazon-linux/.python-versions/${desired_python_version}" | tee -a "${GITHUB_OUTPUT}" - id: setup-python shell: bash - if: inputs.cache == 'false' || (inputs.cache == 'true' && steps.cache-python.outputs.cache-hit != 'true') run: | - installation_directory=${{ steps.set-installation-directory.outputs.installation_directory }} - mkdir -p "${installation_directory}" - exact_python_version="${{ steps.find-exact-python-version.outputs.exact_python_version }}" - ./install-python.sh "${exact_python_version}" "${installation_directory}" + installation_directory="${{ steps.set-installation-directory.outputs.installation_directory }}" + desired_python_version="${{ steps.find-desired-python-version.outputs.desired_python_version }}" + + uv venv --python "${desired_python_version}" "${installation_directory}" - name: Add python to PATH shell: bash run: | - installation_directory=${{ steps.set-installation-directory.outputs.installation_directory }} + installation_directory="${{ steps.set-installation-directory.outputs.installation_directory }}" echo "${installation_directory}/bin" >> "${GITHUB_PATH}" echo "The following python binaries are now available in the PATH" ls "${installation_directory}/bin" - - echo "Linking python libraries.." - ls "${installation_directory}/lib" - ldconfig "${installation_directory}/lib" + + - name: Install pip + shell: bash + run: | + installation_directory="${{ steps.set-installation-directory.outputs.installation_directory }}" + + python -m ensurepip --upgrade + ln -sf "${installation_directory}/bin/pip3" "${installation_directory}/bin/pip" + pip install --upgrade pip branding: icon: 'code' diff --git a/find-desired-python-version.sh b/find-desired-python-version.sh new file mode 100755 index 0000000..a479b68 --- /dev/null +++ b/find-desired-python-version.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +set -euo pipefail + +specified_version="$1" +specified_version_file="$2" + +desired_python_version="${specified_version}" +if [ -f "${specified_version_file}" ]; then + desired_python_version=$(cat "${specified_version_file}") +fi + +echo "${desired_python_version}" \ No newline at end of file diff --git a/find-exact-python-version.sh b/find-exact-python-version.sh deleted file mode 100755 index 0c65677..0000000 --- a/find-exact-python-version.sh +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/env bash - -set -euo pipefail - -specified_version="$1" - -# This versions map should be kept in sync with -# - https://www.python.org/downloads/ -# - https://devguide.python.org/versions/ -case "${specified_version}" in - "3") - echo "3.12.0" - ;; - "3.12") - echo "3.12.0" - ;; - "3.11") - echo "3.11.6" - ;; - "3.10") - echo "3.10.13" - ;; - "3.9") - echo "3.9.18" - ;; - "3.8") - echo "3.8.18" - ;; - *) - echo "${specified_version}" - ;; -esac diff --git a/install-python.sh b/install-python.sh deleted file mode 100755 index 969704d..0000000 --- a/install-python.sh +++ /dev/null @@ -1,33 +0,0 @@ -#!/usr/bin/env bash - -set -euo pipefail - -# Follows the guidelines from https://realpython.com/installing-python/#how-to-build-python-from-source-code - -function set_aliases() { - ln -sf "${python_installation_dir}/bin/python3" "${python_installation_dir}/bin/python" - ln -sf "${python_installation_dir}/bin/pip3" "${python_installation_dir}/bin/pip" -} - -function setup_python() { - python_version="$1" - python_installation_dir="$2" - - mkdir -p "${python_installation_dir}" - temp_dir=$(mktemp -d) - pushd "${temp_dir}" >/dev/null - wget "https://www.python.org/ftp/python/${python_version}/Python-${python_version}.tgz" - tar -zxf "Python-${python_version}.tgz" - pushd "Python-${python_version}" >/dev/null - # Have not added --enable-optimizations flag because that shoots up the build time by ~5 minutes - ./configure --prefix="${python_installation_dir}" --enable-shared - make -j 8 - make install - popd >/dev/null - popd - rm -rf "${temp_dir}" - - set_aliases -} - -setup_python "$1" "$2"