1

Context

I'm developing a GitHub Action that allows managing Repositories Issues based on my company policies (like greeting the user, changing the tags based on interactions and sending messages to Ryver's Chat when it is required).

For the sake of simplicity, lets call the used Repositories as:

  • Repo Act: The repository where the action is stored (and releases are made)
  • Repo Use: The repository where the action is being used (and where the issues are created, and so on)

Actions

So far, to test the changes made in the Repo Act, I have to:

  1. Repo Act. Implement the logic and push it
  2. Repo Act. Make a release of the action (e.g: v1.1.3)
  3. Repo Use. Change the Action version, to use the new version of Repo Act (e.g: v1.1.3)
  4. Repo Use. Create an issue and trigger the action

This process takes way to long. I'm looking for a way to test it locally.

So far, I've been able to short some of those steps, like, e.g:

i. Test the NodeJS logic of the action locally (giving manually values to the variables)

ii. Push the code to Repo Act with the tag so the release is made automatically

iii. Use nektos/act to simulate the point 4. "Create an issue and trigger the action".

My results

But still, this requires to manually removing the mocked values, push the new version of Repo Act, change the version on Repo Use and re-testing by creating the Issue.

There is a way to speed up this process?

I'm way too new on GitHub Repository development and maybe I'm taking the long route.

4

2 回答 2

2

Testing the action new version

It's not mandatory to generate a release version to use an action, you can use the branch name directly.

For example: actions/checkout@main instead of actions/checkout@v2.3.4.

Therefore, targeting the main branch instead of generating a new release each time will allow you to go faster in your tests.

Using another repository to test the action

You don't need to create a new repository to test the action, you can add a workflow in the own action to test it.

Therefore, there is no need to use two repos to do the testing, just use a workflow in the action that will always targeting the main branch and trigger when your condition event is met.

于 2021-06-17T18:18:47.433 回答
0

Integration testing

Facing a similar problem I ended up writing a JS package for actions integration testing called github-action-ts-run-api. This package provides JavaScript/TypeScript API for running an individual action locally or at any CI (including GitHub Actions itself). It supports both node and Docker actions.

Using the package, you can write integration tests directly in Repo Act and run them locally, mocking the whole context of Repo Use and any its events on the level of the action or even on the level of individual JS functions (if you need to mock GitHub API calls for example).

Actual code example:

import {RunTarget, RunOptions} from "github-action-ts-run-api";
import dotenv from 'dotenv';

// For GITHUB_TOKEN and GITHUB_REPOSITORY for local tests
dotenv.config({path: 'tests.env'});

describe('git-get-release-action', () => {
    const target = RunTarget.jsFile('lib/index.js', 'action.yml');

    it('should get by id', () => {
        const res = target.run(RunOptions.create()
            .setEnv({
                GITHUB_TOKEN: process.env.GITHUB_TOKEN,
                GITHUB_REPOSITORY: process.env.GITHUB_REPOSITORY
            })
            .setInputs({  releaseId: '56669824' })
        );
        expect(res.isSuccess).toEqual(true);
        expect(res.commands.outputs.tag_name).toEqual('testTag');
    });
});

Special testing workflow

You can also have a special testing workflow in Repo Act that will call the action by a local path (uses: ./) overriding GITHUB_REPOSITORY and other service env variables to emulate the Repo Use context.

As you mentioned, you can execute the testing workflow using nektos/act.

于 2022-01-31T15:34:49.903 回答