How to use Prettier on Bitbucket pipeline? - bitbucket

I am trying to use code formatter, Prettier, on a Bitbucket pipeline but all I get is a "prettier command not found".
This is my pipeline configuration:
mage: node:latest
pipelines:
branches:
master:
- step:
caches:
- node
script:
- npm install prettier
- prettier --check
Is it even possible to run it through pipelines or would have to forcefully do it locally?

I will suggest you follow the documentation https://prettier.io/docs/en/install.html
install prettier in your repo
npm install --save-dev --save-exact prettier
"--save-dev" because the production will not need prettier to serve your application
add your rules in .prettierrc.json file in the root of the repo
in the pipeline use
npx prettier --check .
What is that npx thing? npx ships with npm and lets you run locally installed tools. We’ll leave off the npx part for brevity throughout the rest of this file!
Note: If you forget to install Prettier first, npx will temporarily download the latest version. That’s not a good idea when using Prettier, because we change how code is formatted in each release! It’s important to have a locked down version of Prettier in your package.json. And it’s faster, too.
When you have installed prettier in your repo, you can even add pre-commit hooks to modify the staged files before pushing.

Related

Gitlab CI /CD: Cannot override private project npm package registry URL for dependencies

I was wondering if someone could help me with my CI/CD configuration for a multi-JS project setup.
Project js-core has some core JS libaries, and project vue-core has some reusable vue components. They are both published as npm packages to the "js-core" project's repository (so that we can use a deploy token with npm publish, as opposed to using a group deploy token, where you have to script the package creation and push directly to the API).
vue-core has a dependency of js-core, so it needs to access the npm package registry of the js-core project to download it inside of the Docker CI/CD instance.
However, gitlab does not allow me to override the package registry's URL. According to some google research, setting the values with yarn config set #myorg:registry <gitlab-url> / yarn config set //gitlab.com/api/v4/... <deploy token> or npm config set #myorg:registry <gitlab-url> / npm config set //gitlab.com/api/v4/... <deploy token> should work. However I can see that it is still trying to download the packages from the vue-core package registry, even though it is disabled as a feature there.
This is the part of my .gitlab-ci.yml that runs before it fails:
image: node:17-alpine
stages:
- build
- test
before_script:
- yarn config set #myorg:registry https://gitlab.com/api/v4/projects/${NPM_PACKAGES_PROJECT_ID}/packages/npm/
- yarn config set //gitlab.com/api/v4/projects/${NPM_PACKAGES_PROJECT_ID}/packages/npm/:_authToken ${NPM_PACKAGES_PROJECT_TOKEN}
- yarn install
It fails at yarn install with:
[2/4] Fetching packages...
error An unexpected error occurred: "https://gitlab.com/api/v4/projects/<vue-core_project_id>/packages/npm/#myorg/js-core/-/#myorg/js-core-1.0.0.tgz: Request failed \"404 Not Found\"".
Where the project ID should be the value of <js-core_project_id>.
I have tried writing to all possible .npmrc file paths (~/.npmrc, ./.npmrc, ${CI_BUILD_DIR}/.npmrc, setting the values with npm config set and yarn config set, deactivating the packages feature in the gitlab project itself. I have also not found any predefined environment variables that would override my configs (see https://docs.gitlab.com/ee/ci/variables/predefined_variables.html). #myorg is correct and matches the gitlab url, as the actual value for "myorg" is a single word...
I am pretty much out of ideas at this point, any help is appreciated!
Update 1: I don't think it is a npm / yarn issue, maybe a gitlab caching problem? If I execute npm config ls -l and yarn config list the correct URLs are output in a format that works locally. I will attempt to clear the yarn cache (globally and locally) and pray that that works.

How do I build lambdas in different languages using AWS CDK Pipelines

I'm setting up a CDK project that have some lambdas in Javascript and Python, I'm trying to figure out what's the best way to build these functions as I would normally pass the build command like this:
// Install dependencies, build and run cdk synth
commands: [
'npm ci',
'npm run build',
'npx cdk synth'
]
or
buildCommand: 'npm run build'
The only thing I can think of is to create a build.sh file inside each lambda, for the ones in JS I'd add npm run build and for the ones in Python pip install -r requirements.txt but I don't really know if this is a good practice and if there's a better way to accomplish this.
What you need is a Bundling docker container. You can
either configure the bundling yourself using the bundling option for Code.fromAsset()
or use the PythonFunction and NodejsFunction constructs which provide standard bundling for Python and Node.js, respectively.
This AWS Blog post gives some more examples for bundling.

npm ci in Jenkins parallel pipeline

I've got a monorepo with roughly the following structure (npm modules):
myProject
|-base
|-ui1
|-ui2
In the base module I have all the stuff combined which my ui1 and ui2 projects use commonly. I use npm local paths to add the base module as a dependency to the ui-projects.
...
"dependencies": {
...
"base": "file:../base"
...
}
...
So far so good...
In my CI environment (Jenkins) I use npm ci to install the dependencies. Since these steps are executed parallelly, the node_modules folder of base will be deleted by the npm ci call of ui1 while ui2 is trying to install the dependencies as well. This causes random errors of course...
Now my actual question: Is there a way to tell npm ci not to delete the node_modules of path dependencies? Of course any other hint to solve this kind of problem is appreciated as well... ;)
Thanks a lot for your help!

running protractor test using npm

I am new to protractor, node and all. I have learned to create scripts with protractor. But now I need to run this test scripts along the build.
I have seen people can run it with npm test/ npm test e2e. How can I achieve this.
My project structure,
webapp/test/e2e
In my project they are using webpack and karma.
Sorry, I am really noob at this and don't know how to set it up.
We also need to configure it in Jenkins, so it runs and generates a flag for success or failure.
Any suggestions to set it up would really really be helpful. Please be as elaborate as possible as it is hard for me to understand things as of now.
Thank you so much!
If the package.json file is in the root folder of the project you can add the code below to it and run it using npm test and npm run test:e2e. This assumes you have Protractor and Karma installed locally in your project which would be a best practice (if it is not in the package.json as a dependency it probably is not).
"scripts": {
"postinstall": "node_modules/protractor/bin/webdriver-manager update",
"test": "node_modules/karma/bin/karma start webapp/test/unit/karma.conf.js"
"test:e2e": "node_modules/protractor/bin/protractor webapp/test/e2e/conf.js"
}
postinstall - This will run webdriver-manager update locally automatically after running npm install, you could run into issues with missing drivers when using a CI Server like Jenkins that is continuously checking our your repository (it also saves you a CI step)
test - This is equivalent to karma start karma.conf.js
test:e2e - This is equivalent to protractor conf.js it is just running it locally. To run these tests type npm run test:e2e from the directory with your package.json
It is recommended to not combine Protractor and Karma together (although possible) Karma should be used for Unit and Integration tests and Protractor should be used for e2e tests.
As an extra tip it is possible to pass protractor or karma command line arguments through the npm scripts. Simply append it the way you normally would (e.g.
"webapp/test/e2e/conf.js --baseUrl=https://yourbaseurl.com/")
Your question regarding integrating NPM into Jenkins has already been answered fairly well here how to run npm/grunt command from jenkins but be aware running your tests entirely in Jenkins will cause it to run headlessly which when using Windows will cause it to run in Session 0 where the screen resolution will be smaller which causes some tests to fail, opening the selenium server beforehand in a Terminal/Powershell window will cause Jenkins to route your tests through that so it will visually display on your computer.

Skip first mvn install in travis-ci

I'm having troubles building a project with maven on travis-ci because travis automatically runs
mvn install -DskipTests=true -Dmaven.javadoc.skip=true -B -V
which fails because of a timeout:
No output has been received in the last 10 minutes, this potentially
indicates a stalled build or something wrong with the build itself
According to the documentation I should be able to override it defining a custom script in .travis.yml but it does not work, here my configuration:
sudo: false
language:
- java
script: "travis_wait mvn -T4 -pl quickfixj-codegenerator install"
jdk:
- oraclejdk8
env:
- MAVEN_OPTS="-Xms2048m -Xmx=2048m"
branches:
only:
- travis-ci-build
Is there any way to avoid the automatic mvn install or to tweak it ?
This is mentioned in the documentation:
https://docs.travis-ci.com/user/job-lifecycle/#skipping-the-installation-phase
Skip the installation step entirely by adding the following to your .travis.yml:
install: skip
The install step runs before script step, and with Maven you don't usually need the install step, at least I personally haven't ever found it useful — Maven will download dependencies on script step anyway.
I had the same issue. It was solved after some discussion with the Travis CI support. Here is their response:
That maven command is run as part of the install section of your
build, it's the default.
If you want to skip this step, you can override it by adding this to
your .travis.yml file:
install: /bin/true
I have found it useful to have an install: mvn dependency:resolve step that downloads the build dependencies up front, so that the output of the actual build script is kept clean

Resources