In this hands-on lab you will learn how to publish npm packages to GitHub using GitHub Actions. The module has the following steps:
-
Create a new repository and select
Node
as your.gitignore
template: -
Clone the repository to your local machine or open it in code spaces.
$ git clone <URL>
-
Create a file
index.js
and add some simple JavaScript code, for example:alert("Hello, World!");
-
If you have
npm
installed on your machine, runnpm init
and initialize your package using the wizard. Leave the defaults for the other values.$ npm init ... package name: @YOUR-USER/YOUR-REPO ... test command: exit 0 ...
If you don't have npm installed, just clone this repository and copy the files
package.json
andpackage-lock.json
to you local repo. Adjust the URLs and package name to include your username and repo. -
Commit and push your files:
$ git add index.js package.json package-lock.json $ git commit -m "Initialize package" $ git push
-
Create a file
.github/workflows/release-package.yml
and copde the following content to it:name: Node.js Package on: release: types: [created] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: node-version: 12 - run: npm ci - run: npm test publish-gpr: needs: build runs-on: ubuntu-latest permissions: packages: write contents: read steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: node-version: 12 registry-url: https://npm.pkg.github.com/ - run: npm ci - run: npm publish env: NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
-
Create a file
.npmrc
in the root of your repository with the following content:@YOUR-USER:registry=https://npm.pkg.github.com
-
Commit and push the new files:
$ git add .github/workflows/release-package.yml .npmrc $ git commit -m "Add workflow to pusblish package" $ git push
-
Create a release. Click on
Releases
underCode
and clickDraft new Release
. Create a new tag and enter a title. If you create the release, the workflow will automatically run. -
If the workflow has completed, you can find you package under
Code
. -
Click on the package to see the details:
Note:
If you want to create new releases, you have to update the version inpackage.json
. Otherwise the build will fail.
-
Automatically set package number
You can automate this step by adding theNPM-Version
action and set it to the tag of the release:- name: 'Change NPM version' uses: reedyuk/[email protected] with: version: ${{github.event.release.tag_name}}
Add this step to you yaml before the
- run: npm ci
step and create a new release. The package version is now set automatically to the tag. -
Calculate the version with GitVersion If you have a more complex workflow you can use
GitVersion
to calculate your version number. You have to make your checkout shallow for that to work:- uses: actions/checkout@v2 with: fetch-depth: 0
Now you can install and execute
GitVersion
and access the version using$GITVERSION_SEMVER
:- name: Install GitVersion uses: gittools/actions/gitversion/[email protected] with: versionSpec: '5.x' - name: Determine Version id: gitversion uses: gittools/actions/gitversion/[email protected] - name: 'Change NPM version' uses: reedyuk/[email protected] with: version: $GITVERSION_SEMVER
Now you could modify your workflow to also trigger on tags or branches to create your package:
on: push: tags: - 'v*' branches: - 'release/*'
In this module you have learned how to create NPM packages and deploy them to GitHub Packes with automatic versioning.
🥇 Congratulations! You have finished all the exercises of the GitHub Bootcamp worshop.