首页 > 解决方案 > 复合 github 动作,自动 cd 进入动作目录?

问题描述

我正在尝试设置一个 github 操作机器人,它在操作运行时使用复合来构建。值得注意的是,这不是必需的,我知道 ncc 也可以实现相同的目标,但我很好奇是否有可能以可持续的方式进行这项工作。

在某些情况下,我正在尝试使用 yaml 脚本运行我的alita-moore/EIP-Bot私人操作...

name: "Auto Merge EIP"
description: "A bot that lints EIP edits, finds common errors, and can auto-merge"
inputs:
  GITHUB-TOKEN:
    description: |-
      The Github token to be used by this bot when merging, posting comments, or requesting reviewers
    required: true
  VERSION:
    description: version of the action; this is required because of how actions work
    required: true

runs:
  using: "composite"
  steps:
    - run: cd /home/runner/work/_actions/alita-moore/EIP-Bot/${{ inputs.VERSION }} && npm ci && npm run build && GITHUB_TOKEN=${{ inputs.GITHUB-TOKEN }} node build/index.js
      shell: bash

这目前使用给定 repo 的工作流 yaml 运行

on: [pull_request]

jobs:
  auto_merge_bot:
    runs-on: ubuntu-latest
    name: EIP Auto-Merge Bot
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Setup Node.js Enviornment
        uses: actions/setup-node@v2
        with:
          node-version: '14'
      - name: auto-merge-bot
        uses: alita-moore/EIP-Bot@1.0.8
        id: auto-merge-bot
        with:
          GITHUB-TOKEN: ${{ secrets.TOKEN }} 
          VERSION: 1.0.8

您可能已经注意到,在npm ci && npm run build我第一次必须cd /home/runner/work/_actions/alita-moore/EIP-Bot/${{ inputs.VERSION }}

如果你不这样做,它会抛出以下错误

Run alita-moore/EIP-Bot@1.0.1
npm ERR! code ENOENT
npm ERR! syscall open
npm ERR! path /home/runner/work/EIPs/EIPs/package.json
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, open '/home/runner/work/EIPs/EIPs/package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent 

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/runner/.npm/_logs/2021-03-14T09_02_26_002Z-debug.log
Error: Process completed with exit code 254.

有一个更好的方法吗?

标签: github-actions

解决方案


您可以使用${{github.action_path}}包含操作定义文件的目录的路径action.yaml

[...]
runs:
  using: "composite"
  steps:
    - run: cd ${{github.action_path}} && npm ci && npm run build && GITHUB_TOKEN=${{ inputs.GITHUB-TOKEN }} node build/index.js
      shell: bash

github.action_path| string| 您的操作所在的路径。您可以使用此路径轻松访问与您的操作位于同一存储库中的文件。此属性仅在复合运行步骤操作中受支持。

https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#github-context


推荐阅读