Deploy react application in Git pages using Azure DevOps - reactjs

I'm trying to deploy my react application (Just a very basic app) in git pages using Azure DevOps. This is to get my hands wet with Azure Devops.
First of all I configured my packag.json as below.
{
"name": "gitpages",
"version": "0.1.0",
"homepage": "https://senal.github.io/gitpage-react",
"private": true,
"dependencies": {
"gh-pages": "^2.0.1",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-scripts": "3.0.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"predeploy": "npm run build",
"deploy": "gh-pages -d build"
},
"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"
]
}
}
Please pay attention to "homepage" and "deploy" script.
In my Azure DevOps, I have modify the yml file as below.
# Node.js with React
# Build a Node.js project that uses React.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/javascript
trigger:
- master
pool:
vmImage: 'ubuntu-latest'
steps:
- task: NodeTool#0
inputs:
versionSpec: '10.x'
displayName: 'Install Node.js'
- script: |
git config --global user.email "xxxx.yyyy#mymail.com"
git config --global user.name "RSF"
npm install
npm run build
npm run deploy
displayName: 'npm install and build then deploy'
However, when I run the build in DevOps I get the following error:
fatal: could not read Username for 'https://github.com': terminal prompts disabled
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! gitpages#0.1.0 deploy: `gh-pages -d build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the gitpages#0.1.0 deploy script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/vsts/.npm/_logs/2019-06-13T12_03_34_094Z-debug.log
##[error]Bash exited with code '1'.
Please advice what't with my steps/scripts.
Feel free to contact me should you require more info.
UPDATE: I was able to create the page by running
npm run deploy
from my terminal.
What I would like to know is, why we can't do the same thing in DevOps?
Cheers,
RSF

I had the same issue and fixed it by supplying the Github token in the --repo option. Replace npm run deploy with,
npx gh-pages --repo https://$(GITHUB_TOKEN)#github.com/org/repo.git -d dist
and add your token as a pipeline secret.

Related

What build configuration for React on Azure Static Web Apps?

I have seemingly tried everything and see conflicting instructions on deploying a React app (without API) to Azure Static Web Apps even from Microsoft. Has anyone done this successfully?
The latest doc I've used is:
https://learn.microsoft.com/en-gb/azure/static-web-apps/get-started-portal?tabs=react&pivots=github
which is for creating the SWA in the portal though I've used others as well with problems.
I've now arrived at this error:
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! percentage-calculator#0.1.0 build: `react-scripts build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the percentage-calculator#0.1.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /github/home/.npm/_logs/2022-07-12T19_38_33_349Z-debug.log
---End of Oryx build logs---
Oryx has failed to build the solution.
Here is my file structure if that matters:
react-app ->src ->components ->index.js,App.js etc..
->public ->index.html etc..
->build (build is gitignored)
->node_modules etc..
My azurestaticwebapps... yml file contains:
app_location: "/"
api_location: ""
output_location: "build"
https://github.com/markhardy/percentage-calculator/runs/7308960687?check_suite_focus=true
Can someone explain what I'm doing wrong? I'm at a loss at this point especially with conflicting instructions from Microsoft.
Seems you have warnings in your code, so the compiler doesn't like it.
Simply change the yml file as below:
under build_and_deploy_job, add:
env:
CI: false
This will stop treating warnings as errors.
Has anyone done this successfully?
We have followed the same document which you have provided and able to build and deploy React static web App from portal without any issues.
Build and Deploy Job:
package.json file:
{
"name": "hello",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.3.2",
"#testing-library/user-event": "^7.1.2",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-scripts": "3.4.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"playwright_test": "playwright 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"
]
},
"devDependencies": {
"#playwright/test": "^1.22.2"
}
}
Try to add devDependencies in your package.json file and check once.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents#2.3.2 (node_modules/fsevents):
As per this Document, we can add optionalDependencies in your package.json file or can run npm install -f.
Please refer Troubleshooting deployment and runtime errors for Static Web Apps for more information.

npm install error while running docker container

I am learning docker and I am getting this error when I run
npm install
npm ERR! code ENOENT
npm ERR! syscall open
npm ERR! path D:\Docker Practice\section4-react-app/package.json
npm ERR! errno -4058
npm ERR! enoent ENOENT: no such file or directory, open 'D:\Docker Practice\section4-react-
app\package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\SIBA\AppData\Local\npm-cache\_logs\2022-04-08T10_01_09_620Z-debug-0.log
This is my package.json file showing all the dependencies
{
"name": "react-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.11.4",
"#testing-library/react": "^11.1.0",
"#testing-library/user-event": "^12.1.10",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-scripts": "4.0.3",
"web-vitals": "^1.0.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
I am following mosh tutorial to learn docker.
My task is to run npm install so it automatically installs all the dependencies listed in this project but I am getting above error.
Can someone help me out in this.
I know the versions may be old but it's completly fine as docker will run all the code according to the given dependencies.
So, instead of updated version, I need my application to be build with this version of software and then deploy it to docker container.
Looking forward to hear from you.
Source code is present here
Regards,
Ali
You must run the npm install in the directory where package.json is located, the error clearly states that npm cannot find the package.json:
npm ERR! enoent ENOENT: no such file or directory, open 'D:\Docker
Practice\section4-react- app\package.json' npm ERR!
enoent This is related to npm not being able to find a file.
So it must be some issue with the path.
Though to debug this error, the provided information is not enough:
Open up your terminal or cmd and go to the directory where package.json is located (using the cd command)
run the ls command, it will show all the files present in the directory
take the screenshot of your terminal now and post it here
then run npm install and again take a screenshot of your terminal and post it here.

Publish React app to Github Pages using github Actions

I made a simple Reactjs app and now I want to publish it on gh-pages.
I followed this Facebook tutorial for deploying it and it is also getting deployed from my PC. But now instead of every time manually deploying it, I thought of using Github Actions to deploy on every push to the master branch. so I wrote the below Action.
name: gh pages publish
on:
push:
branches: master
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- uses: actions/setup-node#v1
with:
node-version: 12
registry-url: https://registry.npmjs.org/
- name: publish package
run: |
yarn
npm run deploy
But this action fails because it requires the user to input the username and password.
> gh-pages -d build
fatal: could not read Username for 'https://github.com': No such device or address
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! timetablemanager#0.1.0 deploy: `gh-pages -d build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the timetablemanager#0.1.0 deploy script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/runner/.npm/_logs/2020-06-21T07_07_19_700Z-debug.log
##[error]Process completed with exit code 1.
How can I fix this?
Edit:
Here is the package.json file
{
"homepage": "http://itissandeep98.github.io/TimeTableManager/",
"name": "timetablemanager",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.3.2",
"#testing-library/user-event": "^7.1.2",
"bootstrap": "^4.5.0",
"bootstrap-social": "^5.1.1",
"font-awesome": "^4.7.0",
"gh-pages": "^3.1.0",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-scripts": "3.4.1"
},
"scripts": {
"predeploy": "npm run build",
"deploy": "gh-pages -d build",
"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"
]
}
}
According to the create react app github pages troubleshoot here, you need to do the following.
Create a new Personal Access Token
git remote set-url origin https://<user>:<token>#github.com/<user>/<repo>.
Try npm run deploy again
Also make sure everything in your package.json is correct, but this definitely looks like an authentication issue.
Putting this here for those looking to do the same as the OG:
I created and added a personal token, and also faced some more issues.
Here are the changes I ended up making on the action file:
- name: setup git authentication
run: |
git config --global user.email "<me>#<gmail>.com"
git config --global user.name "<my_name>"
git remote set-url origin https://<user>:<token>#github.com/<user>/<repo>
- name: publish
run: |
yarn
npm run deploy
for anyone else who ends up here, you can use the secrets.GITHUB_TOKEN that is available on every action, without setting it directly in the settings for your repo.
use Joey Baruch's answer above, just replace <token> with:
${{ secrets.GITHUB_TOKEN }}
P.s. stackoverflow wont let me comment yet so I had to post this as a separate answer.

"eslint: Permission denied" when deploying React app on Firebase through GitLab

I'm currently trying to setup CI through GitLab for my React app hosted on Firebase. I'm struggling to get past this point. There were some other post suggesting the use of sudo but the console told me the command was not found.
Any help would be greatly appreciated. Thanks you.
Here are my current configurations:
gitlab-ci.yml configuration file
image: node:10.15.3
cache:
paths:
- node_modules/
stages:
- build
- deploy
deploy_dev:
stage: deploy
script:
- echo "Deploying to staging environment"
- npm install -g firebase-tools
- firebase deploy --token $FIREBASE_DEPLOY_KEY --project $CI_ENVIRONMENT_NAME
environment:
name: dev
only:
- master
Package.json
{
"name": "react-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"bootstrap": "^4.4.1",
"firebase": "^6.6.2",
"firebase-functions": "^3.3.0",
"moment": "^2.24.0",
"node-sass": "^4.13.1",
"react": "^16.12.0",
"react-beautiful-dnd": "^11.0.5",
"react-dates": "^20.3.0",
"react-dom": "^16.12.0",
"react-moment": "^0.9.7",
"react-perfect-scrollbar": "^1.5.3",
"react-router-dom": "^5.1.2",
"react-scripts": "^3.3.0",
"reactstrap": "^8.2.0",
"recompose": "^0.30.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
The console error indicating permission error
$ firebase deploy -m "Pipeline $CI_PIPELINE_ID, build $CI_BUILD_ID" --non-interactive --token $FIREBASE_DEPLOY_KEY --project $CI_ENVIRONMENT_NAME
⚠ functions: package.json indicates an outdated version of firebase-functions.
Please upgrade using npm install --save firebase-functions#latest in your functions directory.
=== Deploying to 'cmd-dev-bbdc4'...
i deploying functions, hosting
Running command: npm --prefix "$RESOURCE_DIR" run lint
> functions# lint /builds/cmdc/cmd/functions
> eslint .
sh: 1: eslint: Permission denied
npm ERR! code ELIFECYCLE
npm ERR! errno 126
npm ERR! functions# lint: `eslint .`
npm ERR! Exit status 126
npm ERR!
npm ERR! Failed at the functions# lint script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2020-02-03T01_51_09_788Z-debug.log
Error: functions predeploy error: Command terminated with non-zero exit code126
ERROR: Job failed: exit code 1
So through some experimentation I was able to determine that I had to cd into the 'functions' directory and also run NPM install. I guess this was a due to a fundamental misunderstanding of how Firebase projects are structured and of node packages.
I would love to learn more so if anyone share some reading about this, it would be appreciated.
Ended up with a script that look like the following.
deploy_dev:
stage: deploy
script:
- echo "Deploying to staging environment"
- npm install -g firebase-tools #--allow-root
- npm ci #--allow-root
- cd functions # required or would throw the "eslint: not found" error
- npm ci
- cd ..
- firebase use --token $FIREBASE_DEPLOY_KEY $CI_ENVIRONMENT_NAME
- firebase deploy -m "Pipeline $CI_PIPELINE_ID, build $CI_BUILD_ID" --non-interactive --token $FIREBASE_DEPLOY_KEY
environment:
name: dev

after creating a react app npm start doesnt work. No error message given

Im new to react and am having some trouble setting up a new app. I have followed the tutorial here.
https://reactjs.org/docs/create-a-new-react-app.html. However when i go to start npm it doesnt start and I get no error. Just put back into the console.
I had created a few apps before this started happening with no problem now when i go to start these i have the same issue.
I have updated and installed to most recent builds
Is there an error log file anywhere I can access? or can I log one to a .txt file as I run npm start
I get nothing unusual while creating.
$ npx create-react-app seatracker
Creating a new React app in C:\react_tutorial_app\nnew\seatracker.
Installing packages. This might take a couple of minutes.
Installing react, react-dom, and react-scripts...
+ react#16.12.0
+ react-dom#16.12.0
+ react-scripts#3.2.0
added 1480 packages from 694 contributors and audited 904937 packages in 89.688s
found 0 vulnerabilities
Initialized a git repository.
Success! Created seatracker at C:\react_tutorial_app\nnew\seatracker
Inside that directory, you can run several commands:
npm start
Starts the development server.
npm run build
Bundles the app into static files for production.
npm test
Starts the test runner.
npm run eject
Removes this tool and copies build dependencies, configuration files
and scripts into the app directory. If you do this, you can’t go back!
We suggest that you begin by typing:
cd seatracker
npm start
Happy hacking!
and when starting npm no indication of what may be wrong
R#DESKTOP-ILPL80A MINGW64 /c/react_tutorial_app/nnew/seatracker (master)
$ npm start
R#DESKTOP-ILPL80A MINGW64 /c/react_tutorial_app/nnew/seatracker (master)
nothing i can see unusual in the json file
{
"name": "seatracker",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-scripts": "3.2.0"
},
"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"
]
}
}
One of the causes for this problem might be disabling npm installation scripts (for security reason) => check if they are disabled:
npm config get ignore-scripts
Current version of create-react-app needs to execute some stuff, so if the above is true, I would recommend:
npm config set ignore-scripts false
npx create-react-app seatracker
npm config set ignore-scripts true
cd seatracker
npm start
I haven't used npx in a while. Try with npm i create-react-app and create app with create-react-app myapp and after that run app with npm start. Tell if that works.
Try cd seatracker first and npm start after.
Believe me.

Resources