繁体   English   中英

能不能直接把Python代码写在action.yml文件的run | section下

[英]Can we directly write Python code under "run | " section in action.yml file

在GitHub Actions中,我们能不能直接在run | action.yml文件中的部分? 我可以在 Python 中编写 GitHub Actions 脚本吗?

python 有一个内置的shell关键字

steps:
  - name: Display the path
    run: |
      import os
      print(os.environ['PATH'])
    shell: python

来源: https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#example-running-a-python-script


您还可以使用自定义 shell GitHub Actions 将run的值写入临时文件,并通过将{0}替换为临时脚本的文件名,将其传递给指定的 shell。

steps:
  - name: Display the path
    run: |
      import os
      print(os.environ['PATH'])
    shell: python {0}

对的,这是可能的。 如果需要,您甚至可以使用 select python 版本。

以下是一些参考示例:

例子:

name: Python package

on: [push]

jobs:
  build:

    runs-on: ubuntu-latest
    strategy:
      # You can use PyPy versions in python-version.
      # For example, pypy2 and pypy3
      matrix:
        python-version: [2.7, 3.5, 3.6, 3.7, 3.8]

    steps:
    - uses: actions/checkout@v2
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v2
      with:
        python-version: ${{ matrix.python-version }}
    # You can test your matrix by printing the current Python version
    - name: Display Python version
      run: python -c "import sys; print(sys.version)"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM