我们可以直接在action.yml文件的“run |”部分下编写Python代码吗

Shi*_*iva 9 python github-actions building-github-actions

在GitHub Actions中,我们可以直接run | 在文件的-section下编写python代码吗action.yml?我可以用 Python 编写 GitHub Actions 脚本吗?

riQ*_*iQQ 17

python有一个内置shell关键字。

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

来源: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}
Run Code Online (Sandbox Code Playgroud)