Continuous Deployment of a NodeJS using GitLab - javascript

I have an API developed in NodeJS and have successfully set up continuous integration via a .gitlab-ci.yml file. The next stage is to set up continuous deployment to Heroku if all tests pass on the master branch.
There are plenty of tutorials covering the deployment of Ruby and Python apps but nothing on NodeJS. Currently my .gitlab-ci.yml file looks like this:
image: node:latest
job1:
script: "ls -l"
test:
script: "npm install;npm test"
production:
type: deploy
script:
- npm install
- npm start
- gem install dpl
- dpl --provider=heroku --app=my-first-nodejs --api-key=XXXXXXXXXX
only:
- master
The Ruby and Python tutorials use the dpl tool to deploy but how can I start the NodeJS script on the server once deployed?
After adding the production section and pushing it the tests run and pass but the deploy stage gets stuck on pending. The console is blank. Has anyone set up a successful CD script for NodeJS?

you could use a much more simple YAML script where you can define the stages for the CI (to run test before production deploy) you can then use a different image at the Heroku deploy stage. So for a node app you define the default image as node:latest. Then for the production deployment using dpl you can use the ruby image.
image: node:latest
stages:
- job1
- test
- production
job1:
stage: job1
script: "ls -l"
test:
stage: test
script:
- npm install
- npm test
artifacts:
paths:
- dist/
production:
type: deploy
stage: production
image: ruby:latest
script:
- apt-get update -qy
- apt-get install -y ruby-dev
- gem install dpl
- dpl --provider=heroku --app=my-first-nodejs --api-key=XXXXXXXXXX
only:
- master

Well, this won't be the longest answer ever, but this may help you.
Here's the content if it ever disappear:
image: node:argon
before_script:
- apt-get -qq update
- apt-get -qq install -y python2.7 python2.7-dev build-essential make gcc g++ libicu-dev
- npm -g install npm --silent
- "echo -e \"export default {CLIENT_ID: '$CLIENT_ID'}\" > app/scripts/settings.js"
- npm set progress=false
- npm install --silent
stages:
- test
- build
- clean_up
run_tests:
stage: test
script:
- npm test
build_and_deploy_prod:
stage: build
script:
- npm run build
- mkdir dist/build
- tar czfC dist/build/latest.tar.gz dist/$CI_BUILD_REF_NAME/ .
- tar czfC dist/build/$CI_BUILD_REF.tar.gz dist/$CI_BUILD_REF_NAME/ .
- apt-get install -yqq ruby ruby-dev
- gem install dpl
- dpl --skip_cleanup --provider=s3 --region=eu-west-1 --access-key-id=$AWS_ACCESS_KEY --secret-access-key=$AWS_SECRET_KEY --bucket=$AWS_BUCKET --local-dir=dist/build/ --upload-dir=$CI_BUILD_REF_NAME
artifacts:
paths:
- dist/$CI_BUILD_REF_NAME/
only:
- master
- develop
clean_up_job:
stage: clean_up
script:
- rm -rf node_modules
- rm -rf ~/.node-gyp
when: on_failure

A well-explained article for Continuous Deployment of a NodeJS using GitLab :
LINK

Related

Run a npm based web page on Github

I'm trying to run a web page based on npm on my Gituhb repository (paul.github.io) . In it's Readme.md we can find :
# Three.js Journey
## Setup
Download [Node.js](https://nodejs.org/en/download/).
Run this followed commands:
``` bash
# Install dependencies (only the first time)
npm install
# Run the local server at localhost:8080
npm run dev
# Build for production in the dist/ directory
npm run build
```
On my laptop (windowsĂ  I succeed on installing nodejs and npm. However, I didn't find how to install these on github. Any Idea please?
In the light of the comments, I fixed the issue by building the projet with "npm run build", and I saved the output which has been made in dist/ folder in my Github repository. Thanks again to all contributors.

How to pass Angular environment variables to Gitlab CI/CD Pipeline

# For the testing stage you must include all those lines so the tests run and finish. If ng test doesnt have the flags that come after it, it will hang on that stage.
image: node:latest
before_script:
- apt-get update -qy
- apt-get install -y ruby-dev
- gem install dpl
stages:
- production
production:
type: deploy
stage: production
image: ruby:latest
script:
- dpl --provider=heroku --app=$HEROKU_APP_PRODUCTION --api-key=$HEROKU_API_KEY
only:
- master
Good Evening,
I am trying to use my environment key that is set up in the environment file in Angular in my pipeline, but Gitlab does not know how to use it. process.env.(insert variable name) does not work. I have set the env variable in my gitlab pipeline dashboard. I am just not sure how to use that in my app. Any guidance is appreciated.
Thats quite a long answer, you need to list your config or job so we can trouble shoot. In short here are the main components to setup
First setup pipeline jobs like so, you should see your image in your dashboard
myTestjob:
stage: stage_a
image: node:12-alpine
tags:
- docker
script:
- node --version
Second, setup your dependencies
stages:
- install
install_dependencies:
stage: install
image: node:12-alpine
tags:
- docker
script:
- yarn install
- yarn ngcc --properties es2015 --create-ivy-entry-points
cache:
key:
files:
- yarn.lock
paths:
- node_modules
only:
refs:
- merge_requests
- master
changes:
- yarn.lock
remember that your cache key is invalidated whenever you make changes to yarn.lock
cache:
key:
files:
- yarn.lock
paths:
- node_modules
policy: pull
In your angular,json build your application job
{
"projects": {
"angular-app-example": {
"architect": {
"build": {
"builder": "#angular-devkit/build-angular:browser",
"options": {
"outputPath": "artifacts/app"
}
}
}
}
}
}
Setup your env variable, gitlab has many
variables:
PROJECT_PATH: "$CI_PROJECT_DIR"
APP_OUTPUT_PATH: "$CI_PROJECT_DIR/artifacts/app"
build_app:
stage: build_and_test
image: node:12-alpine
tags:
- docker
script:
- yarn ng build --prod
after_script:
- cp $PROJECT_PATH/Dockerfile $APP_OUTPUT_PATH
artifacts:
name: "angular-app-pipeline"
paths:
- $APP_OUTPUT_PATH
cache:
key:
files:
- yarn.lock
paths:
- node_modules
policy: pull
here is a nice ref. on how to setup in more detail
UPDATE after user posted YML file to setup staging and production
cache:
paths:
- node_modules/
deploy_stage:
stage: deploy
environment: Stage
only:
- master
script:
- rm ./package-lock.json
- npm install
- ./node_modules/#angular/cli/bin/ng test --progress false --single-run=true --watch=false
- ./node_modules/#angular/cli/bin/ng e2e --progress false --watch=false
- ./node_modules/#angular/cli/bin/ng build --progress false --prod --base-href tykt-stage.surge.sh
- ./node_modules/.bin/surge -p dist/ --domain tykt-stage.surge.sh
deploy_production:
stage: deploy
environment: Production
only:
- tags
script:
- rm ./package-lock.json
- npm install
- ./node_modules/#angular/cli/bin/ng test --progress false --single-run=true --watch=false
- ./node_modules/#angular/cli/bin/ng e2e --progress false --watch=false
- ./node_modules/#angular/cli/bin/ng build --progress false --prod --base-href tykt.surge.sh
- ./node_modules/.bin/surge -p dist/ --domain tykt.surge.sh
You can use either section and update your domain it should build and push the CI CD

Gitlab CI Failed: NPM command not found

I have been playing around Gitlabl CI but for some reason I can't get my tests to "passed". It always says npm: command not found
My Gitlab CI config looks like this:
image: node:latest
# This folder is cached between builds
# http://docs.gitlab.com/ce/ci/yaml/README.html#cache
cache:
paths:
- node_modules/
before_script:
- npm install
- npm install eslint -g
- npm install eslint-plugin-react -g
- npm install babel-eslint -g
test:lint:
script:
- eslint src/*
I keep getting the error below and I have No Idea why:
By the way, Im NOT using the gitlab shared runner. Not sure if that contributes to the problem but just to make sure, the machine that has my gitlab runner has all the necessary packages to run nodejs.
Your help is greatly appreciated
Best regards,
The image tag specifies a docker image, hence you must specify the executor of your runner to be docker. Did you perhaps set it to be something else, like shell, when you created the runner?
You can use like below:-
stages:
- build
- deploy
deploy-prod:
image: node:12.13.0-alpine
stage: deploy
script:
- npm i -g firebase-tools
I have same problem.
Gitlab-runner use user of 'gitlab-runner' default when it starts. So the user have not root access.
ps aux|grep gitlab-runner
copy the shell of running
change user:
run /usr/bin/gitlab-runner run --working-directory /home/gitlab-runner --config /etc/gitlab-runner/config.toml --service gitlab-runner --syslog --user root in bash.
gitlab-ci runner test
pass!
kill old pid
nohup /usr/bin/gitlab-runner run --working-directory /home/gitlab-runner --config /etc/gitlab-runner/config.toml --service gitlab-runner --syslog --user root
ps: gitlab-runner -u root also change user.
Your cli is taking ssh executer by default. You probably need docker. Try adding tag in your yml script.
tags: [ <your docker image name> ]
This helped me:
n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; sudo cp -r $n/{bin,lib,share} /usr/local
In my case, there are two gitlab-runner. There is no node enviroment in the specific Runner.Then I stopped the wrong one

GitLab CI with JS Linting

I have 0 experience with GitLab Continuous Integration and I need to setup a job to run ESLint on .js files.
I've already read the GitLab CI and Pipeline documentations, along with some Git Hooks, but I still have no idea how to setup this, so any detailed and starting from the very beginning answer is appreciated.
First you need to setup your CI and have some runners available so they can run your continuous integration jobs. The easiest way for this is to use gitlab-ci-multi-runner (project is here along with documentation) along with the docker executor that will run your CI jobs in docker containers. Once you have configured some runners, add them to your Gitlab project so they're available to run jobs.
Once that's taken care of, you need to add a .gitlab-ci.yml file to your project. This file is used to describe the jobs that need to run during continuous integration etc. Here is an example (assuming you install eslint using npm)
image: node:latest
stages:
- lint
eslint:
stage: lint
script:
# Install ESLint in this docker container
- npm install -g eslint
# Configure ESLint (will read your .eslintrc file)
- eslint --init
# Run ESLint
- eslint <your_js_file>
Add your .gitlab-ci.yml file, commit and push the changes. The CI pipeline should start and run the above steps.
If you'd like to have comments to your PRs here is an example with eslint and pronto. (we have ruby app so we check ruby code style too )
image: 'circleci/ruby:2.5.1-node-browsers'
codestyle:
script:
- sudo apt -y install cmake
# install eslint dependencies
- sudo npm install -g eslint
- sudo npm install -g eslint-plugin-babel
- sudo npm install -g eslint-plugin-react
- sudo npm install -g eslint-plugin-import
- sudo npm install -g babel-eslint
- sudo npm install -g eslint-config-airbnb
- sudo npm install -g eslint-plugin-jsx-a11y
# install pronto runners
- gem install pronto --no-ri
- gem install pronto-rubocop --no-ri
- gem install rubocop-rspec --no-ri
- gem install pronto-eslint_npm --no-ri
# run linters
- vendor/ruby/bin/pronto run -f gitlab -c origin/dev --exit-code
You can also run linters separately:
- vendor/ruby/bin/pronto run -r eslint_npm -f gitlab -c origin/dev --exit-code
This piece -f gitlab -c origin/dev tells linters to check only changed lines of code.
Also if you use pronto-eslint_npm and want to check files in specific folder add
.pronto_eslint_npm.yml that will contain needed regex. (in my case it has next line)
files_to_lint: app\/frontend\/\S*(\.js|\.es6|\.jsx)$

How to include "build" directory when travis-ci builds and deploys npm module

Currently I have Typescript module with ./src dir in it
And travic-ci
language: node_js
node_js:
- 5.1.0
install:
- npm install
- npm install -g mocha
- npm install -g gulp
- npm install -g tsd
- tsd rate
- tsd install
script: gulp
after_success: npm test
deploy:
provider: npm
email: secret
api_key:
secure: secret
Gulp task generates files in to "./build" dir
The problem is that when it tries to deploy
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# build/
#
nothing added to commit but untracked files present (use "git add" to track)
So it force me to build project locally and add it to github, and makes using of travis-ci as useless step
Just resolved this problem with this steps:
1: Add ./build dir to .gitignore file
build
typings
test/**/*.js
test/**/*.map
2: Create .npmignore to omit .gitignore rules (and I also put .src dir in it)
src
test
typings

Categories