Skip to content

Commit

Permalink
Add immutable option to yarn install (#210)
Browse files Browse the repository at this point in the history
* Add immutable option to yarn install

* Update yarn lib README.md

* Add test for yarnInstall immutable
  • Loading branch information
lunchbreakdev authored Jun 12, 2023
1 parent 11a6531 commit bab02a2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
4 changes: 2 additions & 2 deletions libraries/yarn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ libraries {
| `yarn_version` | Yarn version to use | `latest` |
| `<step name>.stageName` | stage name displayed in the Jenkins dashboard | N/A |
| `<step name>.script` | Yarn script ran by the step | N/A |
| `<step name>.artifacts` | array of glob patterns for artifacts that should be archived |
| `<step name>.yarnInstall` | Yarn install command to run; Yarn install can be skipped with value "skip" | `frozen-lockfile` |
| `<step name>.artifacts` | array of glob patterns for artifacts that should be archived | |
| `<step name>.yarnInstall` | Yarn install command to run; Yarn install can be skipped with value `skip`; Also supports `immutable` for Yarn 3 setups | `frozen-lockfile` |
| `<step name>.env` | environment variables to make available to the Yarn process; can include key/value pairs and secrets | `[]` |
| `<step name>.env.secrets` | text or username/password credentials to make available to the Yarn process; must be present and available in Jenkins credential store | `[]` |
| `<step name>.useEslintPlugin` | if the Jenkins ESLint Plugin is installed, will run the `recordIssues` step to send lint results to the plugin dashboard | `false` |
Expand Down
8 changes: 5 additions & 3 deletions libraries/yarn/steps/yarn_invoke.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,15 @@ void setEnvVars(libStepConfig, appStepConfig, config, app_env) {
libStepConfig?.yarnInstall ?:
"frozen-lockfile"

if (!["install", "frozen-lockfile", "skip"].contains(yarnInstall)) {
error("yarnInstall must be one of \"install\", \"frozen-lockfile\" or \"skip\"; got \"$yarnInstall\"")
if (!["install", "frozen-lockfile", "skip", "immutable"].contains(yarnInstall)) {
error("yarnInstall must be one of \"install\", \"frozen-lockfile\" \"skip\" or \"immutable\"; got \"$yarnInstall\"")
}

env.yarnInstall = (yarnInstall == "frozen-lockfile")
? "install --frozen-lockfile"
: yarnInstall
: (yarnInstall == "immutable")
? "install --immutable"
: yarnInstall

env.scriptCommand = appStepConfig?.script ?:
libStepConfig?.script ?:
Expand Down
10 changes: 10 additions & 0 deletions libraries/yarn/test/YarnInvokeSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,16 @@ public class YarnInvokeSpec extends JTEPipelineSpecification {
1 * getPipelineMock("sh")(shellCommandWithoutYarnInstall)
}

def "Uses Yarn install with 'immutable' when yarnInstall is set to \"immutable\"; runs yarn install step" () {
setup:
YarnInvoke.getBinding().setVariable("config", [unit_test: [stageName: "Yarn Unit Tests", script: "test", yarnInstall: "immutable"]])
when:
YarnInvoke()
then:
YarnInvoke.getBinding().variables.env.yarnInstall == "install --immutable"
1 * getPipelineMock("sh")(shellCommandWithYarnInstall)
}

def "Archives artifacts correctly" () {
setup:
YarnInvoke.getBinding().setVariable("config", [
Expand Down

0 comments on commit bab02a2

Please sign in to comment.