首页  >  文章  >  开发工具  >  如何在 github actions 中运行 shell 脚本

如何在 github actions 中运行 shell 脚本

Susan Sarandon
Susan Sarandon原创
2024-10-10 12:44:20808浏览

This article provides a guide on executing shell scripts within GitHub Actions workflows. It covers triggering workflows based on shell script changes, passing variables from scripts to jobs, and executing multiple shell scripts within a single workf

如何在 github actions 中运行 shell 脚本

如何在 GitHub Actions 工作流程中执行 shell 脚本?

要在 GitHub Actions 工作流程中执行 shell 脚本,您可以使用 run 命令。 run 命令采用 shell 脚本作为输入并在运行器上执行它。

例如,以下 GitHub Actions 工作流程执行 hello.sh shell 脚本:

<code>name: Execute a shell script

on: [push, pull_request]

jobs:
  execute-shell-script:
    runs-on: ubuntu-latest
    steps:
      - name: Execute shell script
        run: ./hello.sh</code>

我可以根据 shell 脚本的更改触发 GitHub Action 吗?

是的,您可以根据 shell 脚本的更改触发 GitHub Action。为此,您可以在 GitHub Actions 工作流程文件中使用 on 关键字。 on 关键字指定将触发工作流程的事件。

例如,以下 GitHub Actions 工作流程将在 execute-shell-script shell 脚本发生更改时触发 hello.sh 作业:

<code>name: Trigger GitHub Action on shell script changes

on:
  push:
    paths:
      - hello.sh

jobs:
  execute-shell-script:
    runs-on: ubuntu-latest
    steps:
      - name: Execute shell script
        run: ./hello.sh</code>

如何将变量从 shell 脚本传递到 GitHub Action 作业?

您可以使用 env GitHub Actions 工作流程文件中的关键字。 env 关键字指定作业可用的环境变量。

例如,以下 GitHub Actions 工作流程将 FOO 变量从 hello.sh shell 脚本传递到 execute-shell-script 作业:

<code>name: Pass variables from shell script to GitHub Action

on: [push, pull_request]

jobs:
  execute-shell-script:
    runs-on: ubuntu-latest
    env:
      FOO: 'bar' # Value of FOO variable is defined here
    steps:
      - name: Execute shell script
        run: echo $FOO</code>

以上是如何在 github actions 中运行 shell 脚本的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn