Vue CLI 3.0 - azure deploy - azure

I want to deploy my app build in vue which use CLI 3.0.
My package.json:
"scripts": {
"serve": "vue-cli-service serve",
"postinstall": "npm run build",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint",
"test:unit": "vue-cli-service test:unit",
"test:e2e": "vue-cli-service test:e2e"
}
I added "#vue/cli": "^3.0.0-rc.3" to devDependencies, but is don't see any changes.
Azure deploy result:
> npm run vue-cli-service build
npm ERR! missing script: vue-cli-service
Do you have any ideas?

I havent used azure yet, but try only with
npm run build
instead of
npm run vue-cli-service build

I assume you have a build pipeline that struggles with the message you have given.
I think what you are missing is a simple
npm install
After the install you are able to run
npm run build
Without the npm install before, threre is no vue-cli-service npm can find to build the application. I build my own vue-cli 3.0 app this way an deploy to azure like this from an Azure DevOps build pipeline.
or an other possibility is that you are missing another dependency. Add "#vue/cli-service": "^3.0.1" to your devDependencies. And as Daniel Gonzalez pointed out in the comments, there is no need for a postinstall script.

I have succeed building Vue with Vue CLI 3 in Azure.
Sharing my build file here
Azure pipeline YAML script
resources:
- repo: self
trigger: ['staging']
pool:
vmImage: 'Ubuntu 16.04'
steps:
- task: NodeTool#0
displayName: 'Use Node 10.x'
inputs:
versionSpec: 10.x
- script: |
npm install
npm run build-staging
displayName: 'npm install and build'
env:
NODE_ENV: staging
- task: ArchiveFiles#2
displayName: Archive
inputs:
rootFolderOrFile: '$(build.sourcesDirectory)/dist'
includeRootFolder: false
archiveFile: '$(Build.ArtifactStagingDirectory)/$(Build.SourceVersion)_$(Build.BuildId).zip'
- task: PublishBuildArtifacts#1
displayName: 'Publish Artifact: build'
inputs:
ArtifactName: build
package.json
...
"scripts": {
"serve": "vue-cli-service serve --port 9001",
"build": "vue-cli-service build",
"build-staging": "NODE_ENV=production vue-cli-service build --mode staging",
"build-production": "NODE_ENV=production vue-cli-service build --mode production",
"lint": "vue-cli-service lint"
},
...

Only way I have found to host Vue-CLI build files in Azure is this:
Create a StorageV2 in Azure. Make this a static website (under settings). Make index.html the index document name.
Run: npm run build
Install Azure Storage explorer
Open the new storage you created in step 1 in Azure Storage Explorer, go to Blob Containers, $web
Copy the build files from the ./dist folder to the $web folder.
Open a web browser, enter the endpoint URL.

Related

CI with google cloud build and unit testing Jest in NodeJS

I'm setting up a CI/CD pipeline using Google cloud build, I'm doing 3 simple steps : install, test and deploy, here is my CloudBuild.yaml file :
steps:
- name: "gcr.io/cloud-builders/npm"
args: ["install"]
- name: "gcr.io/cloud-builders/npm"
args: ["run", "test"]
- name: "gcr.io/cloud-builders/gcloud"
args: ["app", "deploy"]
And here is my package.json where I need to specify the hole path of the jest so it can identify it, otherwise I set it : "test": "jest" the step 2 of the build will fail and it will not recognize it.
"scripts": {
"start": "node index.js",
"test": "sh node_modules/.bin/jest",
"test:watch": "jest --watch"
},
In this case I need to always push the node_modules to the github repo to find the module jest, but it's not a good practise.
Any ideas how can I fix this ?
Thanks

Azure Deployment Pipeline fails at "react-scripts build"

I have a repository which represents the code for an Express Web App which serves a React frontend when started. I tried automating the process of building and deploying using Azure pipelines. The build step succeeds, however the deployment one fails. The repo is set up like this:
my_app/
├── client/
│ ├── src/
│ ├── package.json
│ └── index.js
├── package.json
├── server.js
└── azure-pipelines.yml
And here are the configuration files:
client/package.json
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.5.0",
"#testing-library/user-event": "^7.2.1",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-scripts": "3.4.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"proxy": "http://localhost:5001/"
}.
package.json:
{
"name": "my-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "cd client/ && npm install && npm run build && cd ../",
"start": "npm install && node server",
"build": "cd client/ && npm install && npm run build && cd ../",
"dev": "node server"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"dotenv": "^8.2.0",
"express": "^4.17.1"
}
}
azure-pipelines.yml:
trigger:
- master
variables:
# Azure Resource Manager connection created during pipeline creation
azureSubscription: 'my_sub_id'
# Web app name
webAppName: 'my_app_name'
# Environment name
environmentName: 'my_env_name'
# Agent VM image name
vmImageName: 'ubuntu-latest'
stages:
- stage: Build
displayName: Build stage
jobs:
- job: Build
displayName: Build
pool:
vmImage: $(vmImageName)
steps:
- task: NodeTool#0
inputs:
versionSpec: '10.x'
displayName: 'Install Node.js'
- script: |
npm install
npm run build --if-present
npm run test --if-present
displayName: 'npm install, build and test'
- task: ArchiveFiles#2
displayName: 'Archive files'
inputs:
rootFolderOrFile: '$(System.DefaultWorkingDirectory)'
includeRootFolder: false
archiveType: zip
archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
replaceExistingArchive: true
- upload: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
artifact: drop
- stage: Deploy
displayName: Deploy stage
dependsOn: Build
condition: succeeded()
jobs:
- deployment: Deploy
displayName: Deploy
environment: $(environmentName)
pool:
vmImage: $(vmImageName)
strategy:
runOnce:
deploy:
steps:
- task: AzureWebApp#1
displayName: 'Azure Web App Deploy: referee-management-tool'
inputs:
azureSubscription: $(azureSubscription)
appType: webAppLinux
appName: $(webAppName)
runtimeStack: 'NODE|10.10'
package: $(Pipeline.Workspace)/drop/$(Build.BuildId).zip
startUpCommand: 'npm run start'
The error which I get in Azure Pipelines Deploy:
2020-05-25T15:56:53.2620824Z Error: Cannot find module '../scripts/build'
2020-05-25T15:56:53.2621190Z Require stack:
2020-05-25T15:56:53.2626302Z - /tmp/8d800c3a4e8367c/client/node_modules/.bin/react-scripts
2020-05-25T15:56:53.2627240Z at Function.Module._resolveFilename (internal/modules/cjs/loader.js:982:15)
2020-05-25T15:56:53.2627901Z at Function.resolve (internal/modules/cjs/helpers.js:83:19)
2020-05-25T15:56:53.2628870Z at Object.<anonymous> (/tmp/8d800c3a4e8367c/client/node_modules/.bin/react-scripts:31:23)
2020-05-25T15:56:53.2629532Z at Module._compile (internal/modules/cjs/loader.js:1158:30)
2020-05-25T15:56:53.2630113Z at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
2020-05-25T15:56:53.2630662Z at Module.load (internal/modules/cjs/loader.js:1002:32)
2020-05-25T15:56:53.2631232Z at Function.Module._load (internal/modules/cjs/loader.js:901:14)
2020-05-25T15:56:53.2631841Z at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
2020-05-25T15:56:53.2632406Z at internal/main/run_main_module.js:18:47 {
2020-05-25T15:56:53.2633045Z code: 'MODULE_NOT_FOUND',
2020-05-25T15:56:53.2633823Z requireStack: [ '/tmp/8d800c3a4e8367c/client/node_modules/.bin/react-scripts' ]
2020-05-25T15:56:53.2634320Z }
I have already tried SSH-ing into the machine to test the "node server" command by hand, but for some reason the SSH connection was dropped and I could not re-connect. If you need more information here
is the repo
I solved this problem finally: In the azure-pipelines.yml file, in the Stages->Build->Script section, I added cd client/ && npm install && cd ../ because npm was not installing the packages needed by the client and was performing npm install only in the parent folder, parsing the package.json there. After adding this, everything was fine.
You need to build the react app locally and use the build folder in your express webapp project.
The project structure should look like this:
my_app/
├── client/
│ ├── build/
│ ├── reat_built_files
│
├── package.json
├── server.js
└── azure-pipelines.yml
Reference:
https://devblogs.microsoft.com/premier-developer/deploying-react-apps-to-azure-with-azure-devops/

How to deploy Angular Universal 9 app to production

We have an angular 9 app that works great when deployed into IIS server.
We have also a branch of this app where another company integrated Universal.
Now we need to understand how to deploy our app with ssr into production. We can do everything on production server so we have no limits in installing software.
Requisite is only that production webserver must remain IIS. This doesn't mean that we can't use node for SSR.
We don't understand what are the exact steps to make a production build and then, which folder, files put in the root of our iis site, and how to run node (if needed) and which file use to have a complete ssr rendered app.
This is the scripts part of package.json:
"scripts": {
"ng": "node --max_old_space_size=8192 node_modules/#angular/cli/bin/ng",
"start": "npm run ng -- serve",
"serve:server": "node ./dist-server/main.js",
"serve:server:debug": "node --inspect ./dist-server/main.js",
"start:server": "npm run build:server && node ./dist-server/main.js",
"start:server:debug": "npm run build:server && node ./dist-server/main.js --inspect",
"build": "npm run ng -- build",
"build:server": "ng run my-app:server:production",
"ssr:watch": "ng run my-app:serve-ssr:production",
"demo:ssr:watch": "ng run universal-demo:serve-ssr:dev",
"build-all": "npm-run-all build-production build:server-app:prod",
"start:express-server": "ts-node -P ./src/tsconfig.server.json ./server.ts",
"prerender": "ts-node -P ./server.tsconfig.json ./prerender.ts",
"prerender:debug": "ts-node -P ./server.tsconfig.json --inspect ./prerender.ts",
"test": "npm run ng -- test",
"lint": "npm run ng -- lint",
"e2e": "npm run ng -- e2e",
"analyze": "webpack-bundle-analyzer dist/stats.json",
"compodoc": "npx compodoc -p src/tsconfig.app.json",
"build:stats": "ng build --stats-json --prod",
"build-preprod": "ng build --configuration preprod --index=/src/index/preprod/index.html",
"build-production": "ng build --configuration production --index=/src/index/production/index.html",
"build-staging": "ng build --configuration staging --index=/src/index/staging/index.html"
}
I know that we need to generate a production build, but here there are too many command and I don't know which one is the correct one for production. Withour SSR I would use "npm run build-production" to have a prod bundle. A dist folder would be created and I would copy its entire content to root folder of my site in IIS.
I tried some of these commands. They generated 2 folders:
dist
dist-server
The second one contains main.js
Now, what I have to copy to server and what is the folder tree from the root of site in IIS and how to install/configure/run with node server?
Here I found also something about configuring iis with node:
Link
Do I need this?
Thanks for your help
I answer myself, this is what I did:
ng build --configuration production --index=/src/index/production/index.html
then
ng run my-app:server:production
I copied generated folders "dist" and "dist-server" into the root of my site
In webserver I set a rule to proxy everything to nodeserver on port 4200 and I started it with:
node dist-server/main.js
It seems to work even if I don't understand why I see rendered html only when I do a ctrl+f5 in view-source and not the first time I access the page

How do check node js errors in Azure

I've deployed an app (node js backned and react on frontend) via a bitbucket pipeline to Azure. It executes without any errors.
pipelines:
branches:
master:
- step:
name: Install, build, test & deploy
caches:
- node
script: # Modify the commands below to build your repository.
- apt-get update || true && apt-get install zip
- cd backend && npm ci && npm run build && cd ../frontend && npm ci && npm run build && cd ../
- mv frontend/build backend/dist/
- cd backend && zip -r build-$BITBUCKET_BUILD_NUMBER.zip *
- curl -v -X POST -u $DEPLOY_USER:$DEPLOY_PASSWORD https://$DEPLOY_URL.scm.azurewebsites.net/api/zipdeploy -T "build-$BITBUCKET_BUILD_NUMBER.zip"
my package json
"scripts": {
"build": "npm run build:app",
"build:app": "tsc",
"dev": "NODE_ENV=development tsnd --respawn --transpileOnly ./index.ts",
"lint": "tslint -c tslint.json './**/*.ts'",
"lint:fix": "tslint -c tslint.json './**/*.ts' --fix",
"start": "NODE_ENV=production node dist/server.js",
"start:staging": "NODE_ENV=staging ts-node ./index.ts",
"test": "echo \"Error: no test specified\" && exit 1"
},
The site gives a 403 with "You do not have permission to view this directory or page."
I've downloaded the logs (diagnostic dump) from https://xxx.scm.azurewebsites.net/ but I don't find any errors.
Im running this on windows in Azure and Mac locally. Probably something wrong there. Can I somehow see the output from when then node app starts? Or how can I debug this?
Starting the web app:
403 generally shows up in Appservice when ever the application has not started. To fix this, in your app service click on Advanced Tools under Development Tools
This opens up a window with an option on top of the header - Bash. Click on that.
By default it will be in home directory. Now, using cd navigate to site -> wwwroot
here, you should be able to see all your files if properly deployed. Now issue the command npm start (or any custom command)
Monitoring Logs:
In your webapp under monitoring, you have Log Stream just click on that and you should be able to see the live logs.
You can give a restart on web app once these changes are done.
Lastly, under configuration check if you have provided a start up command for the app service in General Settings. If its node, hope you have given process.env.PORT to get the available port. Hope this helps!

Setting node env variable in gitlab runner

Im trying to use gitlab runner to test and build my node server but I've run into a small issue when trying to automate tests. In my package.json I have scripts
"scripts": {
"start": "node app.js",
"test-init": "node ./node_modules/jasmine/bin/jasmine.js init",
"test": "set NODE_ENV=Dev&& node ./node_modules/jasmine/bin/jasmine.js"
},
So NODE_ENV=Dev will load a different settings file. One that uses the mongodb url "mongodb://mongo/DBName" and when I run npm test on localhost the server crashes(as its supposed to) because it cant connect to mongo using the Dev setttings file. But when I run the project in GitLab on a runner it wont connect to the db as it uses the non-dev settings file which has a url. Is there any reason in the GitLab-ci why the NODE_ENV is not being set?
Below is my GitLab-ci.yml
image: node:latest
stages:
- build
- test
cache:
paths:
- node_modules/
services:
- mongo
install_dependencies:
stage: build
script:
- npm install
artifacts:
paths:
- node_modules/
test_with_lab:
stage: test
script:
- npm run test-init
- npm test
This is because the docker images run on gitlab are linux based and therefore the set command won't work.
There are two solutions.
Solution 1
Use cross-env npm module as documented here by
doing the following:
Install cross-env like so:
npm install --save-dev cross-env
Then edit your package.json to this:
"scripts": {
"start": "node app.js",
"test-init": "node ./node_modules/jasmine/bin/jasmine.js init",
"test": "cross-env NODE_ENV=Dev node ./node_modules/jasmine/bin/jasmine.js"
},
Solution 2
Just modify the script for linux instead, much quicker and simpler. Here is how it should look. Note npm run test won't work on windows anymore. To avoid this use the first solution above.
"scripts": {
"start": "node app.js",
"test-init": "node ./node_modules/jasmine/bin/jasmine.js init",
"test": "NODE_ENV=Dev node ./node_modules/jasmine/bin/jasmine.js"
},
Note: solution 1 is better in the long run while solution 2 is quick but dirty

Resources