How to execute package.json scripts from buildkite plugin lifecycle hooks

Hi,
I am creating a buildkite plugin to run the E2E test.(This plugin is a git repository containing only shared E2E tests ). I want to use this plugin in my application’s pipeline and run the test. My question is how can I execute the test script residing in package.json of buildkite plugin from lifecycle hook programatically or any better way?
Any help is appreciated

Hello @Zakk !

We’re taking a look at this for you and will get back to you as soon as possible.

Thank you!
Michelle

Hey @Zakk just following up on your question.

To do this make sure that the test script in the plugin’s package.json is correctly defined.

package.json

"scripts": {
  "test:e2e": "your-e2e-test-command"
},

Then in the pipeline configuration, specify a step that executes the test script in the plugin’s package.json. You can use the command attribute to run the desired script.
yaml

steps:
  - label: ":test_tube: Run E2E tests"
    command: npm run test:e2e

Here are links About how to add hooks and the hook locations for plugins

Hope this helps!
Cheers!

Hi @stephanie.atte ,
Thanks for replying. You mean command should be in the pipeline configuration of the application where I am using this plugin. For example:

Application:

steps:
  - label: ":test_tube: Run E2E tests"
    command: npm run test:e2e
    plugins:
      - a-github-user/shared-test#v1.0.0:
          

shared-test-buildkite-plugin:
package.json

"scripts": {
  "test:e2e": "your-e2e-test-command"
},

If my understanding is correct then command in my application’s pipeline will execute the script in package.json of buildkite plugin?

Yes @Zakk , let’s try this out and see how it goes.

@stephanieat I tried but doesn’t seems to be working and nothing shows up in log related to this. Is it possible to execute the package json script from pre-exit hook of the plugin?

@stephanie.atte My plugin.yml file looks like below:
I have defined one property which will receive the baseUrl from application to run the cypress test (E2E test) in my plugin. I am not sure if I am missing out anything?
In this example I am trying to execute a simple script to log instead of running my e2e test for the sake of simplicity and better understanding

plugin.yml

name: Shareable Test Buildkite Plugin
description: Assists in running e2e test in Buildkite
author: https://github.com/zakk
requirements: []
configuration:
  properties:
    applicationUrl:
      type: string
  additionalProperties: false

package.json of buildkite plugin:

"scripts": {
    "print-console": "sh scripts/script.sh"
  },

script.sh is just logging some text

echo "TestConsole======"

Application pipeline using this plugin from feature branch:

 label: ':Sharable Test'
 command: npm run print-console
 env:
      BUILDKITE_PLUGINS_ALWAYS_CLONE_FRESH: "true"
 plugins:
      - ssh://git@github.com/user/shared-test-buildkite-plugin#test-dev:
             applicationUrl: 'base url of the application'

Note: I am able to access the property ‘applicationUrl’ in pre-exit hook but not able to execute any script of package.json available in plugin’s package.json

Hey @Zakk ,

Thanks for the follow up

Since the package.json is in the plugin. You need to have the npm run print-console in the command hook of the plugin as well.

Cheers!

Thanks for your reply and appreciate your patience. I tried npm command but it shows error 'node command not found’
I already have node Image in my applications pipeline where we are using buildkite plugin

Application’s pipeline.yml

docker-node: &docker-node
  agents:
    queue: docker
  plugins:
    - ssh://git@github.com/docker-buildkite-plugin#vXXX:
        image: node Image.xx.xx.xx
        propagate-environment: true
        mount-ssh-agent: true
        copy-checkout: false
        user: 'root'

Command Hook of buildkite plugin

npm run print-console

Hey Zach

Thanks for the information.

The yml file you sent now is a bit different from the previous one. Can I understand what exactly the use case is. Are you using your own plugin or a Buildkite Plugin, which hooks are being used and where, can you also send the package.json aswell?

docker-node: &docker-node is not a valid syntax and also image: node Image.xx.xx.xx

Please can you forward this to support@buildkite.com
You also send me a build link and any other relevant information in the email. So I can take a closer look

Cheers!

Sorry for the confusion, actually that is the different step in pipeline using another plugin for node Image. I will try to phrase my question again .
My usecase: To invoke package.json script of buildkite plugin from command hook of buildkite plugin.
Attempt: written npm run print-console in command hook
Result: Getting error stating npm command not found when the plugin is invoked by pipeline step of my application(different repository)

What could be the potential reason?

Thanks

Hey there @Zakk!

Ben here :wave: Just jumping in for Stephanie due to timezones.

The reason that you’re getting the npm command not found error will be because your instance AMI doesn’t have Node installed on it by default. If you want to have Node be on your machines, and thus be able to run npm commands then your best bet is to create a custom AMI based off of the official Buildkite release, tools such as Packer can help with this and the Packer file might look something like this:

packer {
  required_plugins {
    amazon = {
      version = ">= 0.0.2"
      source  = "github.com/hashicorp/amazon"
    }
  }
}

source "amazon-ebs" "buildkite_agent" {
  region = "<REGION>"
  source_ami = "<SOURCE AMI>"
  instance_type = "<INSTANCE TYPE>"
  ssh_username = "<SSH_USER>"
  ami_name = "<AMI NAME>"
}

build {
  sources = ["source.amazon-ebs.buildkite_agent"]

  provisioner "shell" {
    inline = [
      "sudo yum update -y",
      "sudo apt install nodejs",
      "bazel version"
    ]
  }
}

The other option you have is to run this command within a Docker container. Assuming you have a step which you use to build your image, you could also run tests against the same image earlier on in the pipeline, only pushing the image to something such as ECR if the testing step passes.

In order for us to have a more granular view of your specific setup, it would be helpful if you could email us at support@buildkite.com. Given sharing the pipeline on a public forum wouldn’t be advised.

Cheers!