github actions can not find package.json

我们两清 提交于 2021-02-10 15:07:36

问题


I'm trying to set-up some basic GitHub action to write comments on the PRs.

Action is published on github and looks like this:

action.yml file:

name: !!name!!
description: !!description!!
author: !!me!!
inputs:
  token:
    description: "Github token"
    required: true
runs:
  using: "node12"
  main: "index.js"

index.js file:

const core = require("@actions/core");
const { execSync } = require("child_process");
const { GitHub, context } = require("@actions/github");

const main = async () => {
  const repoName = context.repo.repo;
  const repoOwner = context.repo.owner;
  const token = core.getInput("token");
  const testCommand = "yarn test --watchAll=false";
  const prNumber = context.payload.number;

  const githubClient = new GitHub(token);

  const reportAsString = execSync(testCommand).toString();

  const commentBody = `<div><h2>Test report</h2>
    <p>
      <pre>${reportAsString}</pre>
    </p>
  </div>`;

  await githubClient.issues.createComment({
    repo: repoName,
    owner: repoOwner,
    body: commentBody,
    issue_number: prNumber,
  });
};

main().catch((err) => core.setFailed(err.message));

In the project, I've added action through github, like this

.github/workflows/main.yml file:

on: [push]

jobs:
  start_tests:
    runs-on: ubuntu-latest
    name: A test job
    steps:
      - name: !!name!!
        uses: !!link to my action on github with version!!
        with:
          token: ${{ secrets.GITHUB_TOKEN }}

However, my PRs actions are failing and this is the reason:

 error Couldn't find a package.json file in "/home/runner/work/!!project_name!!/!!project_name!!"
##[error]Command failed: yarn test --watchAll=false
error Couldn't find a package.json file in "/home/runner/work/!!project_name!!/!!project_name!!"

So, anyone has an idea what am I doing wrong here? I tried to google the solution but erm... unsuccessfully :(

Thanks for your time!


回答1:


package.json file has to be present to run yarn test command. As we can see in error there is no such file just because there is no project folder at all. It means that job doesn't know anything about project to work with. So, you have to do one of the following changes:

in case your action is published to marketplace

on: [push]

jobs:
  start_tests:
    runs-on: ubuntu-latest
    name: A test job
    steps:
      - uses: actions/checkout@v2.1.0
      - name: !!name!!
        uses: !!link to my action on github with version!!
        with:
          token: ${{ secrets.GITHUB_TOKEN }}

in case your action is not published yet or you just want to run it "locally"

on: [push]

jobs:
  start_tests:
    runs-on: ubuntu-latest
    name: A test job
    steps:
      - name: !!name!!
        uses: ./
        with:
          token: ${{ secrets.GITHUB_TOKEN }}


来源:https://stackoverflow.com/questions/61613938/github-actions-can-not-find-package-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!