使用 GitHub 工作流程将可执行文件添加到 Ubuntu 最新路径

使用 GitHub 工作流程将可执行文件添加到 Ubuntu 最新路径

使用下面给出的最少代码可以在任何 GitHub 帐户中重现此问题。

myapp.py下面给出的两个文件.github/workflows/myworkflow.yaml是您在任何 GitHub 帐户中在几分钟内重现此内容所需的全部内容。

下面需要更改哪些具体语法才能使.github/workflows/myworkflow.yaml下面给出的 GitHub 工作流程成功添加myapp到 Ubuntu 最新的 PATH,然后能够使用该myapp命令?

背景:

GitHub 工作流程使用 pyinstaller 编译 python 应用程序,并且需要将该编译的应用程序添加到PATH以便myapp后续作业可以使用该命令。

当前错误:

我们当前收到的错误是该myapp --version步骤在第一行失败,并显示以下消息:

/home/runner/work/_temp/long-alpha-numeric-string.sh: line 1: myapp: command not found
Error: Process completed with exit code 127.  

这个错误表明myapp没有添加到PATH中。

但是,日志显示同一作业中同一步骤中的前述命令已成功将该myapp文件夹添加到/usr/local/bin目录并更改其所有权。

完整的日志:

工作流程的完整日志如下:

Collecting pyinstaller
  Downloading pyinstaller-5.3-py3-none-manylinux2014_x86_64.whl (555 kB)
Requirement already satisfied, skipping upgrade: setuptools in /usr/lib/python3/dist-packages (from pyinstaller) (45.2.0)
Collecting altgraph
  Downloading altgraph-0.17.2-py2.py3-none-any.whl (21 kB)
Collecting pyinstaller-hooks-contrib>=2021.4
  Downloading pyinstaller_hooks_contrib-2022.10-py2.py3-none-any.whl (244 kB)
Installing collected packages: altgraph, pyinstaller-hooks-contrib, pyinstaller
Successfully installed altgraph-0.17.2 pyinstaller-5.3 pyinstaller-hooks-contrib-2022.10
49 INFO: PyInstaller: 5.3
49 INFO: Python: 3.8.10
62 INFO: Platform: Linux-5.15.0-1017-azure-x86_64-with-glibc2.29
62 INFO: wrote /home/runner/work/path/path/myapp.spec
300 INFO: UPX is available.
301 INFO: Extending PYTHONPATH with paths
['/home/runner/work/path/path']
502 INFO: checking Analysis
502 INFO: Building Analysis because Analysis-00.toc is non existent
502 INFO: Initializing module dependency graph...
503 INFO: Caching module graph hooks...
512 INFO: Analyzing base_library.zip ...
4127 INFO: Processing pre-find module path hook distutils from '/home/runner/.local/lib/python3.8/site-packages/PyInstaller/hooks/pre_find_module_path/hook-distutils.py'.
4127 INFO: distutils: retargeting to non-venv dir '/usr/lib/python3.8'
7332 INFO: Caching module dependency graph...
7458 INFO: running Analysis Analysis-00.toc
7494 INFO: Analyzing /home/runner/work/path/path/myapp.py
7497 INFO: Processing module hooks...
7497 INFO: Loading module hook 'hook-sysconfig.py' from '/home/runner/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
7507 INFO: Loading module hook 'hook-encodings.py' from '/home/runner/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
7671 INFO: Loading module hook 'hook-xml.py' from '/home/runner/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
7721 INFO: Loading module hook 'hook-lib2to3.py' from '/home/runner/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
7734 INFO: Loading module hook 'hook-heapq.py' from '/home/runner/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
7736 INFO: Loading module hook 'hook-distutils.util.py' from '/home/runner/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
7738 INFO: Loading module hook 'hook-pickle.py' from '/home/runner/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
7740 INFO: Loading module hook 'hook-platform.py' from '/home/runner/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
7741 INFO: Loading module hook 'hook-distutils.py' from '/home/runner/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
7742 INFO: Loading module hook 'hook-difflib.py' from '/home/runner/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
7743 INFO: Loading module hook 'hook-multiprocessing.util.py' from '/home/runner/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
7744 INFO: Loading module hook 'hook-xml.etree.cElementTree.py' from '/home/runner/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
7749 INFO: Looking for ctypes DLLs
7810 INFO: Analyzing run-time hooks ...
7813 INFO: Including run-time hook '/home/runner/.local/lib/python3.8/site-packages/PyInstaller/hooks/rthooks/pyi_rth_subprocess.py'
7817 INFO: Looking for dynamic libraries
8271 INFO: Looking for eggs
8271 INFO: Python library not in binary dependencies. Doing additional searching...
8550 INFO: Using Python library /lib/x86_64-linux-gnu/libpython3.8.so.1.0
8554 INFO: Warnings written to /home/runner/work/path/path/build/myapp/warn-myapp.txt
8570 INFO: Graph cross-reference written to /home/runner/work/path/path/build/myapp/xref-myapp.html
8580 INFO: checking PYZ
8580 INFO: Building PYZ because PYZ-00.toc is non existent
8581 INFO: Building PYZ (ZlibArchive) /home/runner/work/path/path/build/myapp/PYZ-00.pyz
8753 INFO: Building PYZ (ZlibArchive) /home/runner/work/path/path/build/myapp/PYZ-00.pyz completed successfully.
8755 INFO: checking PKG
8755 INFO: Building PKG because PKG-00.toc is non existent
8755 INFO: Building PKG (CArchive) myapp.pkg
8781 INFO: Building PKG (CArchive) myapp.pkg completed successfully.
8782 INFO: Bootloader /home/runner/.local/lib/python3.8/site-packages/PyInstaller/bootloader/Linux-64bit-intel/run
8782 INFO: checking EXE
8782 INFO: Building EXE because EXE-00.toc is non existent
8782 INFO: Building EXE from EXE-00.toc
8782 INFO: Copying bootloader EXE to /home/runner/work/path/path/build/myapp/myapp
8783 INFO: Appending PKG archive to custom ELF section in EXE
8826 INFO: Building EXE from EXE-00.toc completed successfully.
8828 INFO: checking COLLECT
8828 INFO: Building COLLECT because COLLECT-00.toc is non existent
8829 INFO: Building COLLECT COLLECT-00.toc
9418 INFO: Building COLLECT COLLECT-00.toc completed successfully.
........................About to ls -al /usr/local/bin 
total 1079472
drwxrwxrwx  3 root   root        4096 Aug 31 22:28 .
drwxr-xr-x 20 root   root        4096 Aug 28 22:36 ..
-rwxr-xr-x  1 root   root        3472 Aug 28 22:36 activate-global-python-argcomplete
-rwxrwxrwx  1 root   root    20702195 Aug 22 01:56 aliyun
lrwxrwxrwx  1 root   root          37 Aug 28 21:31 aws -> /usr/local/aws-cli/v2/current/bin/aws
lrwxrwxrwx  1 root   root          47 Aug 28 21:31 aws_completer -> /usr/local/aws-cli/v2/current/bin/aws_completer
-rwxrwxrwx  1 runner docker  27838676 Jul 21 16:31 azcopy
lrwxrwxrwx  1 root   root          21 Aug 28 21:22 azcopy10 -> /usr/local/bin/azcopy
lrwxrwxrwx  1 root   root          47 Aug 28 22:03 bazel -> ../lib/node_modules/@bazel/bazelisk/bazelisk.js
lrwxrwxrwx  1 root   root          47 Aug 28 22:03 bazelisk -> ../lib/node_modules/@bazel/bazelisk/bazelisk.js
lrwxrwxrwx  1 root   root          24 Aug 28 22:01 bcp -> /opt/mssql-tools/bin/bcp
-rwxrwxrwx  1 root   root    42040509 Aug 28 21:30 bicep
-rwxr-xr-x  1 root   root         539 Aug 28 22:12 bin-proxy
-rwxrwxrwx  1 root   root    13698488 Aug 17 17:47 ccmake
-rwxrwxrwx  1 root   root    13742840 Aug 17 17:47 cmake
-rwxrwxrwx  1 root   root    29159968 Aug 17 17:47 cmake-gui
-rwxr-xr-x  1 root   root         543 Aug 28 22:11 commander
lrwxrwxrwx  1 root   root          45 Aug 28 22:01 corepack -> ../lib/node_modules/corepack/dist/corepack.js
-rwxrwxrwx  1 root   root    14176408 Aug 17 17:47 cpack
-rwxrwxrwx  1 root   root    15153816 Aug 17 17:47 ctest
-rwxrwxrwx  1 root   root    12737304 Aug 28 21:16 docker-compose
-rwxr-xr-x  1 root   root         525 Aug 28 22:11 dotenv
-rwxr-xr-x  1 root   root         537 Aug 28 22:12 fastlane
lrwxrwxrwx  1 root   root          35 Aug 28 22:03 grunt -> ../lib/node_modules/grunt/bin/grunt
lrwxrwxrwx  1 root   root          36 Aug 28 22:03 gulp -> ../lib/node_modules/gulp/bin/gulp.js
-rwxrwxrwx  1 root   root    46374912 Aug 28 21:56 helm
lrwxrwxrwx  1 root   root          32 Aug 28 21:53 heroku -> /usr/local/lib/heroku/bin/heroku
-rwxr-xr-x  1 root   root         549 Aug 28 22:11 httpclient
-rwxrwxrwx  1 runner docker   9814937 Mar  5  2020 hub
-rwxrwxrwx  1 root   root     6658458 Aug 28 21:56 kind
-rwxrwxrwx  1 root   root    45002752 Aug 28 21:56 kubectl
-rwxrwxrwx  1 root   root    14381056 Aug 28 21:56 kustomize
-rwxrwxrwx  1 root   root       13700 Aug 28 21:56 lein
lrwxrwxrwx  1 root   root          32 Aug 28 22:03 lerna -> ../lib/node_modules/lerna/cli.js
-rwxrwxrwx  1 root   root    75566620 Aug 28 21:56 minikube
drwxr-xr-x  3 runner runner      4096 Aug 31 22:28 myapp
lrwxrwxrwx  1 root   root          27 Aug 28 22:03 n -> ../lib/node_modules/n/bin/n
lrwxrwxrwx  1 root   root          39 Aug 28 22:03 netlify -> ../lib/node_modules/netlify-cli/bin/run
lrwxrwxrwx  1 root   root          40 Aug 28 22:03 newman -> ../lib/node_modules/newman/bin/newman.js
-rwxrwxrwx  1 root   root    82214176 Aug 28 22:01 node
lrwxrwxrwx  1 root   root          21 Aug 28 22:03 now -> /usr/local/bin/vercel
lrwxrwxrwx  1 root   root          38 Aug 28 22:01 npm -> ../lib/node_modules/npm/bin/npm-cli.js
lrwxrwxrwx  1 root   root          38 Aug 28 22:01 npx -> ../lib/node_modules/npm/bin/npx-cli.js
lrwxrwxrwx  1 root   root          39 Aug 28 22:03 ntl -> ../lib/node_modules/netlify-cli/bin/run
-rwxrwxrwx  1 root   root   123873256 Aug 15 05:28 oc
-rwxr-xr-x  1 runner docker   8239688 Aug 22 08:44 oras
-rwxr-xr-x  1 root   root   156344320 Aug  2 10:03 packer
lrwxrwxrwx  1 root   root          37 Aug 28 22:03 parcel -> ../lib/node_modules/parcel/lib/bin.js
lrwxrwxrwx  1 root   root          59 Aug 28 22:04 phantomjs -> /usr/local/share/phantomjs-2.1.1-linux-x86_64/bin/phantomjs
-rwxr-xr-x  1 root   root     3211265 Aug 22 14:00 phpunit
-rwxr-xr-x  1 root   root         206 Aug 28 22:36 pipx
-rwxr-xr-x  1 runner docker  66364321 Aug 16 23:40 pulumi
-rwxr-xr-x  1 runner docker       245 Aug 16 17:01 pulumi-analyzer-policy
-rwxr-xr-x  1 runner docker      1853 Aug 16 17:01 pulumi-analyzer-policy-python
-rwxr-xr-x  1 runner docker  22150761 Aug 16 23:40 pulumi-language-dotnet
-rwxr-xr-x  1 runner docker  23434119 Aug 16 23:40 pulumi-language-go
-rwxr-xr-x  1 runner docker  22319202 Aug 11 21:20 pulumi-language-java
-rwxr-xr-x  1 runner docker  22267922 Aug 16 23:40 pulumi-language-nodejs
-rwxr-xr-x  1 runner docker  22220351 Aug 16 23:40 pulumi-language-python
-rwxr-xr-x  1 runner docker      6453 Aug 16 17:01 pulumi-language-python-exec
-rwxr-xr-x  1 runner docker  21643264 Aug 12 00:21 pulumi-language-yaml
-rwxr-xr-x  1 runner docker       238 Aug 16 17:01 pulumi-resource-pulumi-nodejs
-rwxr-xr-x  1 runner docker      1893 Aug 16 17:01 pulumi-resource-pulumi-python
-rwxr-xr-x  1 root   root        2555 Aug 28 22:36 python-argcomplete-check-easy-install-script
-rwxr-xr-x  1 root   root         383 Aug 28 22:36 python-argcomplete-tcsh
lrwxrwxrwx  1 root   root          36 Aug 28 22:45 pzstd -> /home/linuxbrew/.linuxbrew/bin/pzstd
-rwxrwxrwx  1 root   root      959664 Aug 28 21:41 rebar3
-rwxr-xr-x  1 root   root        1917 Aug 28 22:36 register-python-argcomplete
-rwxr-xr-x  1 root   root         523 Aug 28 22:11 rougify
lrwxrwxrwx  1 root   root          38 Aug 28 21:31 sam -> /usr/local/aws-sam-cli/current/bin/sam
lrwxrwxrwx  1 root   root          58 Aug 28 21:31 session-manager-plugin -> /usr/local/sessionmanagerplugin/bin/session-manager-plugin
lrwxrwxrwx  1 root   root          27 Aug 28 22:01 sqlcmd -> /opt/mssql-tools/bin/sqlcmd
lrwxrwxrwx  1 root   root          32 Aug 28 22:01 sqlpackage -> /usr/local/sqlpackage/sqlpackage
-rwxrwxrwx  1 root   root    79936320 Aug 28 21:53 stack
lrwxrwxrwx  1 root   root          30 Aug 28 21:34 swift -> /usr/share/swift/usr/bin/swift
lrwxrwxrwx  1 root   root          31 Aug 28 21:34 swiftc -> /usr/share/swift/usr/bin/swiftc
-rwxr-xr-x  1 root   root         591 Aug 28 22:11 terminal-notifier
-rwxr-xr-x  1 root   root    62963712 Aug 24 14:18 terraform
lrwxrwxrwx  1 root   root          38 Aug 28 22:03 tsc -> ../lib/node_modules/typescript/bin/tsc
lrwxrwxrwx  1 root   root          43 Aug 28 22:03 tsserver -> ../lib/node_modules/typescript/bin/tsserver
lrwxrwxrwx  1 root   root          37 Aug 28 22:45 unzstd -> /home/linuxbrew/.linuxbrew/bin/unzstd
-rwxr-xr-x  1 root   root         219 Aug 28 22:36 userpath
lrwxrwxrwx  1 root   root          40 Aug 28 22:03 vc -> ../lib/node_modules/vercel/dist/index.js
lrwxrwxrwx  1 root   root          28 Aug 28 22:23 vcpkg -> /usr/local/share/vcpkg/vcpkg
lrwxrwxrwx  1 root   root          40 Aug 28 22:03 vercel -> ../lib/node_modules/vercel/dist/index.js
lrwxrwxrwx  1 root   root          42 Aug 28 22:03 webpack -> ../lib/node_modules/webpack/bin/webpack.js
lrwxrwxrwx  1 root   root          42 Aug 28 22:03 webpack-cli -> ../lib/node_modules/webpack-cli/bin/cli.js
-rwxr-xr-x  1 root   root         529 Aug 28 22:11 ww
-rwxr-xr-x  1 root   root         543 Aug 28 22:11 xcodeproj
-rwxr-xr-x  1 root   root         537 Aug 28 22:11 xcpretty
-rwxr-xr-x  1 root   root         639 Aug 28 22:11 xcpretty-travis-formatter
lrwxrwxrwx  1 root   root          36 Aug 28 22:03 yarn -> ../lib/node_modules/yarn/bin/yarn.js
lrwxrwxrwx  1 root   root          36 Aug 28 22:03 yarnpkg -> ../lib/node_modules/yarn/bin/yarn.js
lrwxrwxrwx  1 root   root          35 Aug 28 22:45 zstd -> /home/linuxbrew/.linuxbrew/bin/zstd
lrwxrwxrwx  1 root   root          38 Aug 28 22:45 zstdcat -> /home/linuxbrew/.linuxbrew/bin/zstdcat
lrwxrwxrwx  1 root   root          39 Aug 28 22:45 zstdgrep -> /home/linuxbrew/.linuxbrew/bin/zstdgrep
lrwxrwxrwx  1 root   root          39 Aug 28 22:45 zstdless -> /home/linuxbrew/.linuxbrew/bin/zstdless
lrwxrwxrwx  1 root   root          37 Aug 28 22:45 zstdmt -> /home/linuxbrew/.linuxbrew/bin/zstdmt
........................About to ls -al /usr/local/bin/myapp 
total 11072
drwxr-xr-x 3 runner runner    4096 Aug 31 22:28 .
drwxrwxrwx 3 root   root      4096 Aug 31 22:28 ..
-rw-r--r-- 1 runner runner 1032366 Aug 31 22:28 base_library.zip
drwxr-xr-x 2 runner runner    4096 Aug 31 22:28 lib-dynload
-rwxr-xr-x 1 runner runner   74848 Aug 31 22:28 libbz2.so.1.0
-rwxr-xr-x 1 runner runner 2954080 Aug 31 22:28 libcrypto.so.1.1
-rwxr-xr-x 1 runner runner  182560 Aug 31 22:28 libexpat.so.1
-rwxr-xr-x 1 runner runner  162264 Aug 31 22:28 liblzma.so.5
-rwxr-xr-x 1 runner runner 5449112 Aug 31 22:28 libpython3.8.so.1.0
-rwxr-xr-x 1 runner runner  598104 Aug 31 22:28 libssl.so.1.1
-rwxr-xr-x 1 runner runner  108936 Aug 31 22:28 libz.so.1
-rwxr-xr-x 1 runner runner  739512 Aug 31 22:28 myapp
++++++++++++++++++++++++About to hash /usr/local/bin/myapp
------------------------About to hash /usr/local/bin/myapp.py
++++++++++++++++++++++++About to hash /usr/local/bin/myapp/myapp
------------------------About to hash /usr/local/bin/myapp/myapp.py
About to check version
/home/runner/work/_temp/484ed69e-d73c-41a7-9114-052661753f10.sh: line 21: myapp: command not found
Error: Process completed with exit code 127.
 

当前工作流程代码:

.github/workflows/myworkflow.yaml除了下面的后续部分中的 3 行 python 程序之外,您只需使用以下名为的工作流程文件即可重现此错误。

name: add-to-path-ubuntu
on:
  push:
    branches:
      - main
jobs:
  create-exe-and-add-to-path:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - shell: bash
        name: Create exe and add exe to PATH
        run: |
          pip install -U pyinstaller
          pyinstaller myapp.py
          sudo cp -r dist/myapp /usr/local/bin
          sudo chown -R runner:runner /usr/local/bin/myapp
          echo "........................About to ls -al /usr/local/bin "
          ls -al /usr/local/bin
          echo "........................About to ls -al /usr/local/bin/myapp "
          ls -al /usr/local/bin/myapp
          echo "++++++++++++++++++++++++About to hash /usr/local/bin/myapp"
          hash /usr/local/bin/myapp
          echo "------------------------About to hash /usr/local/bin/myapp.py"
          hash /usr/local/bin/myapp.py
          echo "++++++++++++++++++++++++About to hash /usr/local/bin/myapp/myapp"
          hash /usr/local/bin/myapp/myapp
          echo "------------------------About to hash /usr/local/bin/myapp/myapp.py"
          hash /usr/local/bin/myapp/myapp.py
          sudo mkdir /home/runner/testdir
          sudo chown runner:runner /home/runner/testdir
          cd /home/runner/testdir
          echo 'About to check version'
          myapp --version

最少的应用程序代码:

您可以使用以下名为myapp.py并包含以下 3 行的 python 文件重现此问题:

#!/usr/bin/env python
import sys
if sys.argv[1] == '--version':
  print("You are using version 1.0.")

@muru 的建议:

这是基于 @muru 下面的评论运行的代码,但我们对此有一些担忧,我们将在下面阐明:

name: add-to-path-ubuntu
on:
  push:
    branches:
      - main
jobs:
  create-exe-and-add-to-path:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - shell: bash
        name: Create exe and add exe to PATH
        run: |
          pip install -U pyinstaller
          pyinstaller myapp.py
          cd dist/myapp
          sudo cp -R . /usr/local/bin/. 
          sudo chown -R runner:runner /usr/local/bin
          sudo mkdir /home/runner/testdir
          sudo chown runner:runner /home/runner/testdir
          cd /home/runner/testdir
          myapp --version

我们对这种方法的主要担忧是:

  1. /usr/local/bin因所有依赖关系而变得混乱myapp混乱必须包含在同一目录中,myapp以便myapp正常运行。
  2. 现在全部/usr/local/bin归用户所有runner

由于这是一个临时容器,其生命周期以来自 GitHub 工作流程的调用开始和结束,这一事实在一定程度上缓解了这些担忧。但仍然看起来很混乱。

你能提出一个更清晰的答案吗?

答案1

关于@muru 评论后的最后一个问题,我的建议是遵循问题目录列表中的/usr/local/bin/aws和命令中可见的模式。/usr/local/bin/heroku这些命令是指向文件的符号链接,这些文件及其依赖项安装在/usr/local/bin.对于该aws命令,文件+依赖项安装在 下/usr/local/aws-cli/,对于heroku它们安装在/usr/local/lib/heroku/.

遵循该模式将涉及为应用程序的命令+依赖项选择一个目录名称,例如/usr/local/lib/myapp,将文件复制到那里,并创建指向/usr/local/bin该目录中可执行脚本的符号链接。您的应用程序的可执行文件和依赖项可以一起存在于同一目录中,但它们不会混乱/usr/local/bin

相关内容