Skip to main content

此版本的 GitHub Enterprise Server 将于以下日期停止服务 2024-09-24. 即使针对重大安全问题,也不会发布补丁。 为了获得更好的性能、更高的安全性和新功能,请升级到最新版本的 GitHub Enterprise。 如需升级帮助,请联系 GitHub Enterprise 支持

添加脚本到工作流程

还可以使用 GitHub Actions 工作流来运行脚本。

可以使用 GitHub Actions 工作流来运行脚本和 shell 命令,然后在指定的运行器上执行。 此示例演示如何使用 run 关键字在运行器上执行 npm install -g bats 命令。

jobs:
  example-job:
    runs-on: ubuntu-latest
    steps:
      - run: npm install -g bats

要使用工作流运行存储在存储库中的脚本,必须先将存储库签出到运行器。 完成此操作后,即可使用 run 关键字在运行器上运行脚本。 以下示例运行两个脚本,每个脚本在单独的作业步骤中运行。 脚本在运行器上的位置通过设置运行命令的默认工作目录加以指定。 有关详细信息,请参阅“设置默认 shell 和工作目录”。

jobs:
  example-job:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: ./scripts
    steps:
      - name: Check out the repository to the runner
        uses: actions/checkout@v4  
      - name: Run a script
        run: ./my-script.sh
      - name: Run another script
        run: ./my-other-script.sh

希望工作流作业运行的任何脚本,都必须是可执行脚本。 可以在工作流中执行此操作:方法一是将脚本作为参数(例如 run: bash script.sh),传递给将运行脚本的解释器;方法二是让文件本身变成可执行文件。 可以通过在本地使用 git update-index --chmod=+x PATH/TO/YOUR/script.sh 命令来授予文件执行权限,然后将文件提交并推送到存储库。 另外,对于在 Linux 和 Mac 运行器上运行的工作流,可以在运行脚本之前添加一个命令,从而在工作流作业中授予文件执行权限:

jobs:
  example-job:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: ./scripts
    steps:
      - name: Check out the repository to the runner
        uses: actions/checkout@v4  
      - name: Make the script files executable
        run: chmod +x my-script.sh my-other-script.sh
      - name: Run the scripts
        run: |
          ./my-script.sh
          ./my-other-script.sh

要详细了解 run 密钥,请参阅“GitHub Actions 的工作流语法”。