Created
August 16, 2023 11:20
-
-
Save zlocate/15275513c8cd5c6652da29866472deb0 to your computer and use it in GitHub Desktop.
Gitlab CI build, test, deploy NPM package (with global node modules cache)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # .gitlab-ci.yml | |
| stages: | |
| - build | |
| - test | |
| - deploy | |
| # global cache settings for all jobs | |
| # Ensure compatibility with the install job | |
| # goal: the install job loads the cache and | |
| # all other jobs can only use it | |
| cache: | |
| # most npm libraries will only have 1 entry for the base project deps | |
| - key: &global_cache_node_mods | |
| files: | |
| - package-lock.json | |
| paths: | |
| - node_modules/ | |
| policy: pull # prevent subsequent jobs from modifying cache | |
| # # ATTN mono-repo users: with only additional node_modules, | |
| # # add up to 2 additional cache entries. | |
| # # See limitations in #10. | |
| # - key: | |
| # files: | |
| # - core/pkg1/package-lock.json | |
| # paths: | |
| # - core/pkg1/node_modules/ | |
| # policy: pull # prevent jobs from modifying cache | |
| install: | |
| image: ... | |
| stage: .pre # always first, no matter if it is listed in stages | |
| cache: | |
| # store npm cache for all branches (stores download pkg.tar.gz's) | |
| # will not be necessary for any other job | |
| - key: ${CI_JOB_NAME} | |
| # must be inside $CI_PROJECT_DIR for gitlab-runner caching (#3) | |
| paths: | |
| - .npm/ | |
| when: on_success | |
| policy: pull-push | |
| # Mimic &global_cache_node_mods config but override policy | |
| # to allow this job to update the cache at the end of the job | |
| # and only update if it was a successful job | |
| # NOTE: I would use yaml anchors here but overriding the policy | |
| # in a yaml list is not as easy as a dictionary entry (#5) | |
| - key: | |
| files: | |
| - package-lock.json | |
| paths: | |
| - node_modules/ | |
| when: on_success | |
| policy: pull-push | |
| # # ATTN Monorepo Users: add additional key entries from | |
| # # the global cache and override the policy as above but | |
| # # realize the limitations (read #10). | |
| # - key: | |
| # files: | |
| # - core/pkg1/package-lock.json | |
| # paths: | |
| # - core/client/node_modules/ | |
| # when: on_success | |
| # policy: pull-push | |
| # before_script: | |
| # - ... | |
| script: | |
| # define cache dir & use it npm! | |
| - npm ci --cache .npm --prefer-offline | |
| # # monorepo users: run secondary install actions | |
| # - npx lerna bootstrap -- --cache .npm/ --prefer-offline | |
| build: | |
| stage: build | |
| # global cache settings are inherited to grab `node_modules` | |
| script: | |
| - npm run build | |
| artifacts: | |
| paths: | |
| - dist/ # where ever your build results are stored | |
| test: | |
| stage: test | |
| # global cache settings are inherited to grab `node_modules` | |
| needs: | |
| # install job is not "needed" unless it creates artifacts | |
| # install job also occurs in the previous stage `.pre` so it | |
| # is implicitly required since `when: on_success` is the default | |
| # for subsequent jobs in subsequent stages | |
| - job: build | |
| artifacts: true # grabs built files | |
| # dependencies: could also be used instead of needs | |
| script: | |
| - npm test | |
| deploy: | |
| stage: deploy | |
| when: on_success # only if previous stages' jobs all succeeded | |
| # override inherited cache settings since node_modules is not needed | |
| cache: {} | |
| needs: | |
| - job: build | |
| artifacts: true # grabs dist/ | |
| script: | |
| - npm publish |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment