Step0:-> Install Nodejs on Windows
https://nodejs.org/en/download/
> Install Nodejs on ubuntu
curl -fsSL https://deb.nodesource.com/setup_17.x | bash -
apt-get install -y nodejs
Step 1:- Install Visual Studio Code on your system.
Step2:- Create a folder say C:\nodeapp and open it in visual studio code
cd c:\nodeapp
code .
Step3:- Create a index.js file and write below code in this file
const express = require('express')
const app = express()
const port = 8080
app.get('/', (req, res) => {
res.send('This is my first Node application!')
})
app.listen(port, () => {
console.log(`Application is listening at http://localhost:${port}`)
})
Step 4:- Create package.json file and write below content
{
"dependencies": {
"express": "^4.16.1"
}
}
Step5: Run below commands in the terminal
npm install
node index.js
Then in GitLab:
upload index.js and package.json into the repository.
edit .gitlab-ci.yml file with these examples:
Example 1: This will result in error:
stages:
- build
- deploy
build:
stage: build
script:
- apt update
- apt install npm -y
- npm install
deploy:
stage: deploy
script:
- apt update -y
- apt install nodejs -y
- node index.js
it will result in error because it didn't find the express edition.
because what is installed in build is not passed down to deploy.
Example2: to remedy the above situation, artifacts need to be added into the line of codes. The artifacts line means it will pass down the paths from build to deploy so when deploy job runs, it will have the resources with it. BUT this will result in endless loop..the job will keep on running.
stages:
- build
- deploy
build:
stage: build
script:
- apt update
- apt install npm -y
- npm install
artifacts:
paths:
- node_modules
- package-lock.json
# expire_in: 1 week
deploy:
stage: deploy
script:
- apt update -y
- apt install nodejs -y
- node index.js
Example3: to make the above to run in the background. And the pipeline is passed.
stages:
- build
- deploy
build:
stage: build
script:
- apt update
- apt install npm -y
- npm install
artifacts:
paths:
- node_modules
- package-lock.json
# expire_in: 1 week
deploy:
stage: deploy
script:
- apt update -y
- apt install nodejs -y
- node index.js > /dev/null 2>&1 & (The ampersand is the solution, it makes the program run in background)
Example 4: Using docker image rather than scripting apt install.
stages:
- build
- deploy
build:
image: node
stage: build
script:
# - apt update -y
# - apt install npm -y
- npm install
artifacts:
paths:
- node_modules
- package-lock.json
# expire_in: 1 week
deploy:
image: node
stage: deploy
script:
# - apt update -y
# - apt install nodejs -y
- node index.js > /dev/null 2>&1 &