6

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!

Uros
  • 303
  • 1
  • 3
  • 13

2 Answers2

9

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 }}
fabasoad
  • 1,613
  • 18
  • 20
-1

If you have a similar structure in your github repo, docker/build-push-action@v3 file and context properties should be set appropriately in your workflow file.

Project Structure

Workflow file

uqi8
  • 29
  • 7