how to access a private npm package via github actions? - node.js

I think this is a pretty straightforward question. Still, I'm finding lots of different resources suggesting going down different paths, installing different things, and generally speaking, so far everything seems all over the place.
In my case, I made a private package on npm. I'm trying to do a basic on push to main > run ci and build github action. However, I have my own custom private npm package, part of an org I made on npm. When the pipeline triggers I get the following response:
The current node.js.yml file looks like so:
name: Node.js CI
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
on:
push:
branches: ['main']
pull_request:
branches: ['main']
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- uses: actions/checkout#v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node#v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: echo "Running pipeline."
- run: npm ci
- run: npm run build --if-present
- run: npm test
There's not enough information in it yet, other than a read-only key provide (per npm docs), but I don't know if that's a step in the right direction or if I need to do something entirely different. As I said, I'm not too familiar with this space and any help would be appreciated.

Turns out I needed to add an .npmrc file, with NPM_TOKEN as a github secret.
//registry.npmjs.org/:_authToken=${NPM_TOKEN}

You have to create an .npmrc file with proper authToken.
Add your authToken to your GitHub Environemnt Secrets (for example as: NPM_TOKEN) and then use to create a file using bash script for example:
- name: "Create .npmrc"
run: |
echo "registry.npmjs.org/:_authToken=${NPM_TOKEN}" >> .npmrc
Then you can call npm install.

Related

What's the best way to share the output between Github Actions reusable workflows?

I have a bunch of Github Actions that require exactly the same init process. I decided to implement a reusable workflow, but it turned out that the data is not shared between workflows.
I managed to solve that issue using actions/cache. I simply cache all the files with path: '**/**', but I have a feeling that it's not an optimal approach. It doesn't feel like actions/cache was meant to be used for sharing the entire build directory. It was rather designed for sharing things like node_modules or other generated files to save processing time.
The solution provided below works, but it feels hacky. I wonder is there a better solution that would not require caching (I know I could use artifacts but it also doesn't feel right).
That's what I ended up with:
# ./.github/workflows/init.yml
name: init
on: workflow_call
jobs:
init:
name: Init
runs-on: ubuntu-20.04
steps:
- name: Cache
uses: actions/cache#v3
with:
path: '**/**' # Cache all files
key: build-files-{{ github.sha }} # makes the key unique for each commit
- name: Checkout code
uses: actions/checkout#v2
with:
token: ${{ secrets.NPM_TOKEN }}
- name: Install Node
uses: actions/setup-node#v2
with:
always-auth: true
node-version: 12.x
cache: 'npm'
registry-url: 'https://npm.pkg.github.com'
scope: '#my-company-namespace'
- name: Install NPM packages
run: npm install
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
- name: Download translations
run: npm run translations
env:
I18N_PROJECT_ID: ${{secrets.I18N_PROJECT_ID}}
I18N_API_KEY: ${{secrets.I18N_API_KEY}}
# ./.github/workflows/linter.yml
name: Linter
on: [push]
jobs:
init:
secrets: inherit
uses: ./.github/workflows/init.yml # re-use the init.yml workflow
linter:
runs-on: ubuntu-20.04
needs: [init]
steps:
- name: Cache
uses: actions/cache#v3
with:
path: '**/**' # restore cached files
key: build-files-{{ github.sha }}
- name: Linter
run: npm run lint

Azure Web application deployment successful, but does not update the web application

Previously I was having an error with the deployment of my React application on Web Service Linux on Azure. This problem was solved in the previous post I did, follow the link:
My Azure Web Application on Linux is not working. The error message on azure logs "react-scripts: not found" and github "npm ERR! code ELIFECYCLE ”.
Now I am having another problem which consists of the following:
After deploying to the Azure platform (I'm using the github option for deployment) and receiving a successful deployment notification, upon entering my github repository, I received the error
"npm ERR! Code ELIFECYCLE" (follow the link to view the entire log: https://mega.nz/folder/eth0WSiL#pGvXl2yShQfUrNELCKD3cA). Upon entering the application and testing it I noticed that the deployment really did not work.
An important point worth mentioning that in the previous problem the solution passed by #JasonPan worked, but when we tested it I still used the Azure classic
deployment center, which was removed a few days ago and after trying to use the current deployment center I came across this error.
I managed to solve the problem. I needed to do two things within my .yml file, they were:
add a CI: false and remove the npm run test
Here is the code:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Set up Node.js version
uses: actions/setup-node#v1
with:
node-version: '14.x'
- name: npm install, build
run: |
npm install
npm run build --if-present
- name: Upload artifact for deployment job
uses: actions/upload-artifact#v2
with:
name: node-app
path: .
deploy:
runs-on: ubuntu-latest
needs: build
environment:
CI: false
name: 'production'
url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
The .yml file before it was changed:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Set up Node.js version
uses: actions/setup-node#v1
with:
node-version: '14.x'
- name: npm install, build, and test
run: |
npm install
npm run build --if-present
npm run test --if-present
- name: Upload artifact for deployment job
uses: actions/upload-artifact#v2
with:
name: node-app
path: .
deploy:
runs-on: ubuntu-latest
needs: build
environment:
name: 'production'
url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}

how to run a node.js github repository as a service in another repository on github actions

So I have a project that needs a "fake" API to do some functional testing on user scenarios; so my idea was creating a simple little node.js project and getting to return some dummy json data depending on a few endpoints to test several use cases of my application.
I have a separate repository containing this fake API and I'm wondering how I should go about adding it into my github actions workflow?
You can use actions/checkout#v2 to pull another repo into your worker.
See the example below:
name: PullExternalRepo
on: workflow_dispatch
jobs:
PullRepo:
runs-on: ubuntu-latest
- name: Install Node
uses: actions/setup-node#v2-beta
with:
node-version: '12'
- name: Install external repo
uses: actions/checkout#v2
with:
repository: your_org/repo_name
path: './place/to/clone/repo/into'
- name: Install deps and run
run: |
cd ./place/to/clone/repo/into
npm install
npm start
Added note about pulling specific branches:
If you are trying to pull a non-default branch, you need to add the ref property to the checkout action, as seen in the example below.
...
- name: Install external repo
uses: actions/checkout#v2
with:
repository: your_org/repo_name
path: './place/to/clone/repo/into'
ref: 'some-other-branch'
...

React deployment to firebase using github actions

on:
push:
branches:
- master
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout#master
- name: Install Dependencies
run: npm install
- name: Build
run: npm run build
- name: Archive Production Artifact
uses: actions/upload-artifact#master
with:
name: build
path: build
deploy:
name: Deploy
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout#master
- name: Download Artifact
uses: actions/download-artifact#master
with:
name: build
- name: Deploy to Firebase
uses: w9jds/firebase-action#master
with:
args: deploy --only hosting
env:
FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
now this is the gtihub actions workflow it is executing build job without errors but in deployment there comes an error
this is the error image
the error its shows is Error: Specified public directory 'build' does not exist, can't deploy hosting to site landing-page-design-1 i have followed the blog from where the workflow is copied i did everything same except some of my project details which is obvious please help me out why is this error occuring and how can i fix it
You're probably unpacking artifact to root directory instead of build/.
I'm guessing article was written for download-artifact#v1 while you are using download-artifact#v2 (as that's where master points currently). Difference between both is discussed here.
I'd verify first what is going on after artifact is downloaded
- name: Display directory structure
run: ls -R
shell: bash
If files are indeed in root directory, adding path should fix that.
- name: Download Artifact
uses: actions/download-artifact#v2
with:
name: build
path: build
PS: Using actions/<name>#master is not recommended, as it can always lead to issues if same action behaves differently between versions... for example actions/download-artifact ;)
You can also try to use firebase-publish-react to simplify your workflow file
This particular action plugin takes care of building the application internally and also can reuse the build directory from previous steps.
- name: Deploy to Firebase
uses: mohammed-atif/firebase-publish-react#v1.0
with:
firebase-token: ${{ secrets.FIREBASE_TOKEN }}

How to import private data with GitHub actions?

I'm working on a Node project involving several API keys. I stored the API keys in a configuration file config.js. Then I added config.js to .gitignore so that the API keys aren't revealed in the public repository. But when I try to npm run build with GitHub actions, there's an import error because config.js isn't in the repository.
Can I "simulate" config.js somehow on GitHub? Or should I setup an action to download config.js from elsewhere? Is there a better approach?
I'm using GitHub's boilerplate nodejs.yml:
name: Node CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [8.x, 10.x, 12.x]
steps:
- uses: actions/checkout#v1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node#v1
with:
node-version: ${{ matrix.node-version }}
- name: npm install, build, and test
run: |
npm install
npm run build --if-present
env:
CI: true
I'm fairly new to CI/CD. Thanks in advance!
UPDATE: I solved this problem using the accepted answer below. I stored config.js in a secret variable config on GitHub. Then I added a step in the workflow that creates config.js before it's needed:
...
- name: create config.js
run: echo '${{ secrets.config }}' > path/to/config.js
- name: npm install, build, and test
...
You could declare your key as a secret in GitHub Actions under the name you want (for instance 'my_secret_key')
See also "Creating and using secrets (encrypted variables)"
Said key can be referenced in your config.js as a variable $my_secret_key.

Resources