New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic Node.js Action #9

Open
LandonSchropp opened this Issue Dec 2, 2018 · 12 comments

Comments

Projects
None yet
3 participants
@LandonSchropp
Copy link

LandonSchropp commented Dec 2, 2018

I use Yarn instead of NPM for all my projects. When I run my linter, I do it by running yarn lint. In order to get this to work in GitHub Actions, I overrode the entrypoint.sh script like this:

action "Lint" {
  needs = "Build"
  uses = "actions/npm@master"
  runs = "yarn"
  args = "lint"
}

My above command is using the npm action, but only because it comes with a Node.js environment and Yarn preinstalled. I could make my own action with a Dockerfile for Yarn, but for my simple use case that seems like overkill. I'd also like to avoid having extra Dockerfiles to maintain if possible.

Is the intended design to always have one command per action? Would you expect me to create a yarn action that I could use for this? Or would it make sense to build more generic actions? if there were a generic node action, I could do this:

action "Lint" {
  needs = "Build"
  uses = "actions/node@master"
  runs = "yarn"
  args = "lint"
}
@LandonSchropp

This comment has been minimized.

Copy link

LandonSchropp commented Dec 2, 2018

I think I might understand now. It looks like uses can take a Docker image hosted on DockerHub. I could accomplish the same thing as above (without involving the NPM action) like this:

action "Lint" {
  needs = "Build"
  uses = "docker://node:10"
  runs = "yarn"
  args = "lint"
}

Does that seem like the right way to go?

@johnnyreilly

This comment has been minimized.

Copy link

johnnyreilly commented Jan 1, 2019

Hey @LandonSchropp,

Happy New Year! I'm brand new to GitHub Actions and alas I'm still waiting for someone to click the "give John Reilly access to the beta" button. No doubt that's in the post. I'm planning to use GitHub actions to automate the publishing of a project I work on called ts-loader to npm.

All of which is background. The reason I'm writing this comment is you're at least a month ahead of me on this GitHub Actions thing and I'm hoping I can pick your brains. If you've no time to answer then no sweat; but to quote my father "if you don't ask, you don't get" 😉

Should these questions seem a bit obvious then please forgive me; I'm no expert on docker and I'm certainly no expert on GitHub Actions.

Like you, ts-loader very much requires yarn. To that end I've read a whole bunch of main.workflow files and have written my best guess at what my own main.workflow file may look like:

workflow "build, test and publish on release" {
  on = "release"
  resolves = "publish"
}

# install with yarn
action "install" {
  uses = "actions/npm@master"
  runs = "yarn"
  args = "install"
}

# build with yarn
action "build" {
  needs = "install"
  uses = "actions/npm@master"
  runs = "yarn"
  args = "build"
}

# test with yarn
action "test" {
  needs = "build"
  uses = "actions/npm@master"
  runs = "yarn"
  args = "test"
}

# publish with npm
action "publish" {
  needs = "test"
  uses = "actions/npm@master"
  args = "publish"
  secrets = ["NPM_AUTH_TOKEN"]
}

The above is hand crafted since I don't yet have access to the beta. There are likely some wrong bits but hopefully you get the gist; we install, build and test using yarn and then publish with npm.

All of which should hopefully work based upon what you've said in your first comment. Since yarn is included in node:10-slim which actions/npm depends upon I can just use yarn in the manner you suggest.

Questions

To actions/npm or not to actions/npm?

I was intrigued by what you said in your subsequent comment when you suggested using uses = "docker://node:10" in place of uses = "actions/npm@master".

If you can substitute uses = "docker://node:10" (or in fact any node version) in place of uses = "actions/npm@master" then what value does actions/npm@master actually bring to the table? When would you use uses = "actions/npm@master" as opposed to the docker node container of your choice? I'd love to understand.

It looks to me like the value of actions/npm@master is simply that using this you don't need a runs = "npm" in play as the entryfile.sh handles this. Possibly also if you want to npm publish you'd need actions/npm@master? I'm not certain...

actions/npm is built on node:10-slim. Let's imagine I have a requirement to run my tests on node 11 or something. Can I just swap to use uses = "docker://node:11" and everything just works?

Can I switch uses mid-workflow and keep output?

My second question (and I'm pretty sure the answer to this is "no") is this:

If, like in my example, you want to perform several actions in a workflow; installing, building, testing and releasing in turn is it important all share the same uses? Would it be valid to have uses = "docker://node:10" in the install action and then uses = "actions/npm@master" in the build action? I'm assuming the answer is "no, they'd be in different docker images - you can't share the output" but as I'm no docker / GitHub Actions expert I thought it worth asking.

My thinking here being: could I have uses = "docker://node:11" for all my actions apart from the publish one? i.e. only have uses = "actions/npm@master" for the publish action? This isn't actually a need of mine, but the answer will clarify my understanding of how GitHub Actions share output / state.

@mcolyer

This comment has been minimized.

Copy link

mcolyer commented Jan 2, 2019

Is the intended design to always have one command per action? Would you expect me to create a yarn action that I could use for this? Or would it make sense to build more generic actions? if there were a generic node action, I could do this:

It kind of depends on what you want to accomplish. Smaller images will boot faster but that means you'll need to maintain more images to accomplish your goals. We've focused on creating images around single binaries but feel free to create images that include more than one.

Does that seem like the right way to go?

Yep 👍. Don't feel restricted to only use Actions that GitHub has produced. We hope (and expect) that people will author many of their own for their own specific needs.

There are likely some wrong bits but hopefully you get the gist; we install, build and test using yarn and then publish with npm.

It looks right to me. The biggest suggestion I would make is to use a specific version ie @1.0.0 rather than @master, otherwise changes in behavior of the npm Action could break your workflows.

If you can substitute uses = "docker://node:10" (or in fact any node version) in place of uses = "actions/npm@master" then what value does actions/npm@master actually bring to the table? When would you use uses = "actions/npm@master" as opposed to the docker node container of your choice? I'd love to understand.

If your intent is to run an action with Node I'd actually say that the npm action doesn't offer you anything specifically and you should feel free to use existing docker images.

Let's imagine I have a requirement to run my tests on node 11 or something. Can I just swap to use uses = "docker://node:11" and everything just works?

Yep, exactly.

Can I switch uses mid-workflow and keep output?

Suprisingly, the answer is actually yes. You can use whatever docker container or action you would like for each step of the process.

@LandonSchropp

This comment has been minimized.

Copy link

LandonSchropp commented Jan 2, 2019

@johnnyreilly I'm happy to help, but I'm sure @mcolyer's answers are better than mine would be. 😄

@LandonSchropp

This comment has been minimized.

Copy link

LandonSchropp commented Jan 2, 2019

Can I switch uses mid-workflow and keep output?

I think this might be my favorite feature of Actions. Coming from CodeShip, this was unexpected, but it's really useful.

@johnnyreilly

This comment has been minimized.

Copy link

johnnyreilly commented Jan 3, 2019

Can I switch uses mid-workflow and keep output?

Suprisingly, the answer is actually yes. You can use whatever docker container or action you would like for each step of the process.

Wow! That's amazing 😄

Thanks for your answers @mcolyer (and for showing willing @LandonSchropp !)

@mcolyer would you be able to get myself / TypeStrong access to the beta? I've applied on behalf of both myself and TypeStrong but not heard anything yet... (ts-loader lives under TypeStrong: https://github.com/TypeStrong/ts-loader )

@mcolyer

This comment has been minimized.

Copy link

mcolyer commented Jan 4, 2019

@mcolyer would you be able to get myself / TypeStrong access to the beta? I've applied on behalf of both myself and TypeStrong but not heard anything yet... (ts-loader lives under TypeStrong: https://github.com/TypeStrong/ts-loader )

We're adding people as quickly as we can at the moment.

@johnnyreilly

This comment has been minimized.

Copy link

johnnyreilly commented Jan 4, 2019

Great!

@johnnyreilly

This comment has been minimized.

Copy link

johnnyreilly commented Jan 4, 2019

I've just received the invite to johnnyreilly which is 💯😁

However I don't have an invite for TypeStrong (the organisation where ts-loader lives).

Is it possible to get that invitation please? For: https://github.com/TypeStrong/ts-loader

@johnnyreilly

This comment has been minimized.

Copy link

johnnyreilly commented Jan 5, 2019

I have it - thanks!

@johnnyreilly

This comment has been minimized.

Copy link

johnnyreilly commented Jan 5, 2019

Happy to say I am now up and running with GitHub Actions!

Some teething troubles but that's to be expected. You can see what I'm up to here:

TypeStrong/ts-loader#893

@johnnyreilly

This comment has been minimized.

Copy link

johnnyreilly commented Jan 5, 2019

Thanks for the help - I've blogged about my learnings so far: https://blog.johnnyreilly.com/2019/01/github-actions-and-yarn.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment