LoginSignup
2
0

GitHub Actions で .python-version に従った Python を利用する

Last updated at Posted at 2023-06-02

pyenv とかで .python-version ファイルを利用して Python のバージョンを固定している場合に、わざわざ GitHub Actions で使っている Python のバージョン設定を手作業で同期したくないというか、もし仮にそういう作業が大好きでも設定し忘れてて CI が落ちたりします。

という訳で自動的に .python-version を読み込むようにしました。

同じ手法は actions/setup-node@v3 & .node-version でも使えます。
(ruby/setup-ruby@v1 は多分デフォルトで .ruby-version を読みそうです。)

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3

    - uses: actions/setup-python@v4
      with:
        python-version-file: '.python-version'

python-version に替えて python-version-file を使うのがポイントです。

今時は Docker を使ったりするのでやる人も多くないのかも知れませんが、取り回しが良くて手軽ですね。

↑の手法に気づく前に示していた手法

頑張って書いたので、愚直に $GITHUB_OUTPUT でやる場合も残しておきます。

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3

    - id: get-python-version
      run: echo "python-version=$(cat .python-version)" >> "$GITHUB_OUTPUT"

    - uses: actions/setup-python@v4
      with:
        # !!! 上に示した手法の方が効率的です !!!
        python-version: ${{ steps.get-python-version.outputs.python-version }}

$GITHUB_OUTPUT に書き込むことで出力パラメータを設定することができる GitHub Actions の仕様を利用しています。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0