I have a private repo that, using Github Actions workflow, I have published as a private npm package on Github Package Registry. I want to consume this package in the scope of another private project of mine. But there is an issue. Upon importing the GPR hosted package as a dependency I get a 'module not found' error.
Github Actions workflow successfully publishes private npm package to GPR.
The published package appears under 'Package' tab at Github user landing.
GPR_ACCESS_TOKEN is a PAT (ensuring that I can consume the package).
IMAGE: the error in question
.npmrc file at root of project consuming private package
#slackermorris:registry=https://npm.pkg.github.com/
//npm.pkg.github.com/:_authToken=XXXX-XXXX-XXXX-XXXX
Github Action responsible for republishing private npm package to Github Registry.
name: Node.js Package
on:
push:
branches:
- master
release:
types: [created]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- uses: actions/setup-node#v1
with:
node-version: 12
- run: npm ci
- run: npm test
env:
CI: true
publish-gpr:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- uses: actions/setup-node#v1
with:
node-version: 12
registry-url: https://npm.pkg.github.com
scope: slackermorris
- run: npm ci
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.GPR_ACCESS_TOKEN}}
package.json of the published npm package.
"name": "#slackermorris/bostock-metaball-animation",
"version": "1.0.3",
"main": "index.js",
"author": "slackermorris",
"license": "MIT",
"publishConfig": {
"registry": "https://npm.pkg.github.com"
} ...
What you are trying should work, but I can confirm it does not. Better than putting your token in .npmrc is storing your Personal Access Token in a repo secret that is exported by the workflow. IE, create a repo secret named GPR_AUTH_TOKEN with the contents of your PAT. Then add this to your workflow:
env:
GPR_AUTH_TOKEN: ${{ secrets.GPR_AUTH_TOKEN }}
Then have your .npmrc load the token via the env variable:
//npm.pkg.github.com/:_authToken=${GPR_AUTH_TOKEN}
That avoids exposing your PAT to everyone with repo access.
That solution works for me on node.js v16 but it doesn't work with node 14.
What works for every LTS version of Node.js is to configure .npmrc with this npm step in your workflow:
- name: Configure NPM
run: npm config set '//npm.pkg.github.com/:_authToken' "${{secrets.GPR_AUTH_TOKEN}}"
After that, the npm install works as expected.
Related
The following workflow.yml works as expected when comitting changes to my main branch for our Flutter web based application.
name: Build and deploy Node.js app to Azure Web App - myApp
on:
push:
branches:
- main
workflow_dispatch:
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: '12.x'
- name: npm install, build, and test
run: |
npm install
npm run build --if-present
npm run test --if-present
- name: 'Deploy to Azure Web App'
id: deploy-to-webapp
uses: azure/webapps-deploy#v2
with:
app-name: 'myApp'
slot-name: 'Production'
publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_MYAPP }}
package: .
But we are challenged to make the workflow to publish to an additional dev and beta branch, so that the website branches could be addressed like this:
main: myapp.azurewebsites.net
beta: beta.myapp.azurewebsites.net
dev: dev.myapp.azurewebsites.net
So how should the workflow.yml be to publish those two additional branches ?
Do I need to do some more in azure setting/enviroment or github to make it work ?
The branches in github are named 'main', 'beta' and 'dev'.
I have a project based on Node/npm and use commitlint, husky and semantic-release. Whenever I push to the protected main branch I want to create a new release.
In Github I added the following workflow
name: Release on push on main
on:
push:
branches:
- main
jobs:
release-on-push-on-main:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
fetch-depth: 0
- name: Setup Node
uses: actions/setup-node#v2
with:
node-version: 16.x
- name: Install dependencies
run: npm install
- name: Release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: npx semantic-release --branches main
which does the job very well. When moving to the releases I see that semantic-release attaches the source code
Running npm run build generates me a dist folder containing all the build files. How can enhance my workflow to add the build to the assets?
Adding the steps
- name: Run build
run: npm run build
- name: Archive build
uses: actions/upload-artifact#v2
with:
name: build
path: dist
before running the Release step seems to work as expected
but how can I add it as an asset to the release?
You need to configure the semantic-release github-plugin
using the configuration file
The CLI uses some default configuration, and if you need a custom config
you will have to create an entire config from scratch (as far as I know).
Create a file called release.config.js in the root project folder:
// make sure you install #semantic-release/github as dev dependency
module.exports = {
"plugins": [
// additional config...
["#semantic-release/github", {
"assets": [
{"path": "dist/asset.min.css", "label": "CSS distribution"},
{"path": "dist/asset.min.js", "label": "JS distribution"}
]
}],
]
}
I can also recommend using an alternative to semantic-release
called atomic-release.
It's an SDK with a strategy to release NPM packages (kind of like semantic-release). Check it out GithubNpmPackageStrategy
Disclaimer: I'm the author of atomic-release.
I want to publish a whole directory (the build directory) on a Github release using semantic-release but unfortunately it releases each build file as a single asset.
For reproduction:
I'm using the Vue CLI to generate a project vue create foo
Install semantic-release as a dev dependency npm install --save-dev semantic-release
Install the Github plugin for semantic-release npm install #semantic-release/github -D
Create a .releaserc.json with the content
.
{
"plugins":[
"#semantic-release/commit-analyzer",
"#semantic-release/release-notes-generator",
[
"#semantic-release/github",
{
"assets":[
{
"path":"dist",
"label":"foo-${nextRelease.gitTag}"
}
]
}
]
]
}
Inside the package.json set the version key to 0.0.0-development
Create a .github/workflows directory with the workflow ci.yml
.
name: CI
on:
push:
branches:
- main
jobs:
ci:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout#v2
with:
fetch-depth: 0
- name: Setup Node
uses: actions/setup-node#v2
with:
node-version: 16.x
- name: Install dependencies
run: npm install
- name: Run build
run: npm run build
- name: Release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: npx semantic-release --branches main
Commit and push it with feat: pushed
The release seems to be fine but unfortunately it didn't publish the dist directory as a single asset.
It simply published each file inside dist as a single
Adding the step
- name: Log
run: ls
shows that the dist directory exists
How can I fix that?
It seems this is not possible. So I have to add this step after building the app
- name: ZIP build
run: zip -r dist.zip dist
and set the assets config to
{
"path":"dist.zip",
"label":"foo-${nextRelease.gitTag}.zip"
}
Hi i created a repository with the same name of my github account called mortezasabihi. and in this repository i pushed my personal website (nuxtjs static) to it and created github action for autodeployment, but I don't know why my github page URL is this:
I want to be https://mortezasabihi.github.io.
this is my gh action:
name: cd
on: [push, pull_request]
jobs:
cd:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest]
node: [14]
steps:
- name: Checkout
uses: actions/checkout#master
- name: Setup node env
uses: actions/setup-node#v2.1.2
with:
node-version: ${{ matrix.node }}
- name: Install dependencies
run: yarn
- name: Generate
run: yarn run generate
- name: Deploy
uses: peaceiris/actions-gh-pages#v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./dist
what's wrong? I didn't understand!
The own action-gh-pages documentation allows you to configure many things inside your workflow file.
You can have one site published to https://<username>.github.io by publishing to the master (or main) branch of a repository named “username.github.io” (substituting your actual username), as explained here.
You can also have an additional site per GitHub project published to https://<username>.github.io/<project> (which seems to be what you did here using your username profile repository).
Project sites will publish whatever you push to the gh-pages branch by default, but you can change the publishing source in the repository settings.
Another description is available in the GitHub Pages documentation, including options for using custom domain names.
I have my personal website on Github pages too, and use this strategy:
I create a branch named development and working on that branch.
Based on Nuxt documentation, I add some script in my package.json file:
"scripts": {
"dev": "nuxt",
"generate": "nuxt generate",
"start": "nuxt start",
"deploy": "push-dir --dir=dist --branch=master --cleanup"
},
and when running these commands, the dist folder automatically will be pushed to master.
npm run generate
npm run deploy
I am new to Gatsby and just tried a GH Actions workflow for my site today. I see this error at the build stage:
error "gatsby-source-github-api" threw an error while running the
sourceNodes lifecycle: token is undefined
I am using this to pull all repos on my Github into the site.
What I have tried so far:
Checked the Github personal access token
the build is working locally
Versions:
"gatsby-source-github-api": "^0.2.1" "gatsby": "^2.26.1"
This is my node.js.yml GH Actions workflow file:
name: Node.js CI
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [13.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- uses: actions/checkout#v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node#v1
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run build
PS: I have used a .env file to hold the value of the token that the plugin 'gatsby-source-github-api' requires. And it is in my .gitignore file. So which means it is not sent to GH and hence can't find it?
So that was it -- the .env file which wasn't being pushed to Github.
So I needed to make the token available to the GH Action build somehow.
I tried the action SpicyPizza/create-envfile#v1. It isn't supported by Github but seemed to do the job. It creates a .env file at build with the keys you provide it.
You could also create a .env file manually. See thread.