Githubアクションを使ってNPMJSに自動配備するJavaScriptライブラリ
24790 ワード
概要
これは、簡素化し、GMTHUBアクションを使用してNPMJSで私たちのJSライブラリのリリースを自動化する時間です.
プロジェクトの初期設定
javascript-library-autodeploy
.mkdir javascript-library-autodeloy && cd javascript-library-autodeploy
package.json
そして、我々のライブラリのためのライブラリのカップルを追加します.yarn init
yarn add -D [email protected] [email protected] [email protected] [email protected]
はい、この例ではJavaScript Linterを使用します.ESLint を実行します.Jest . 私たちは皆、テストを書きます.package.json
.{
"name": "@alexeykhr/javascript-library-autodeploy",
"version": "1.0.0",
"license": "MIT",
"scripts": {
"dev": "webpack --mode=development",
"build": "webpack --mode=production",
"test": "jest",
"lint": "eslint src"
},
"devDependencies": {
"eslint": "^7.32.0",
"jest": "^27.1.0",
"webpack": "^5.51.1",
"webpack-cli": "^4.8.0"
}
}
// src/index.js
function sum(a, b) {
return a + b;
}
module.exports = { sum };
// tests/sum.test.js
const { sum } = require('../src/index');
test('1 + 2 = 3', () => {
expect(sum(1, 2)).toBe(3);
});
Githubアクションによる自動化
このステップでは、2を作成しますGithub Workflows .
最初の1つは、githubリポジトリ内の各コードの変更後に実行されます.
# .github/workflows/library.yml
name: Library
on: push
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '14.x'
- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"
- uses: actions/cache@v2
id: yarn-cache
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- run: yarn
- run: yarn lint
- run: yarn test
- run: yarn build
ワークフローは次の通りです.push
イベントyarn.lock
ファイルの変更Workflow
, これはビルドを解放します.# .github/workflows/release.yml
name: Release
on:
release:
types: [published]
jobs:
library:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '14.x'
registry-url: 'https://registry.npmjs.org'
- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"
- uses: actions/cache@v2
id: yarn-cache
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- run: yarn
- run: npm publish --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- uses: actions/setup-node@v2
with:
registry-url: 'https://npm.pkg.github.com'
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
このワークフローのロジックは、前のワークフローと非常に似ています.唯一の違いは、それがrelease
イベント、および代わりにプリンタとテストを実行する.npm publish
npmjsとgithubパッケージに対して呼び出されます.しかし、正しく動作するように、我々は追加する必要がありますGithub Secrets このリポジトリに
NPM_TOKEN
.を追加する必要はありません
GITHUB_TOKEN
, デフォルトで有効になっています. About the GITHUB_TOKEN secret
Generate New Token
Automation
NPM_TOKEN
. 結果の確定
用意!現在、我々はGithubリポジトリに我々のコードをアップロードすることができて、すぐにそれを見ることができます
Workflow
が起動されます.さあ、新しいリリースを作ろうとしましょう.これを行うには、右サイドバーで、リリースをクリックし、新しいリリースを作成します.
The
release
イベントがトリガされ、今我々はどのように我々のプロジェクトを構築することができますし、NPMJSリソースにアップロードすることができます.完了しました、ここに図書館があります.@alexeykhr/javascript-library-autodeploy
また、ライブラリには
Packages
サイドバーブロック.ボーナス:vuepressドキュメンテーション
そしてもちろん、どのようにドキュメントなしですることができます、私は手に来た最初のものを取ったVuepress .
ドキュメントは現在のリポジトリに住んでいます.もちろん、ビルドプロセスはGithubによって行われます.
docs
フォルダと初期化package.json
.mkdir docs && cd docs && yarn init
yarn add -D [email protected]
はい、我々は別々になりますpackage.json
ライブラリバージョンが互いに競合しないようにする(例えば、異なるバージョンのWebpackなど).このようにしてライブラリはコアライブラリに影響を与えません.{
"license": "MIT",
"scripts": {
"dev": "vuepress dev .",
"build": "vuepress build ."
},
"devDependencies": {
"vuepress": "^1.8.2"
}
}
docs/README.md
ファイルの内容を表示するファイル.# VuePress
<<< @/../package.json
Vuepress
.// docs/.vuepress/config.js
const { version, name } = require('../../package');
const parts = name.split('/');
module.exports = {
title: `Version ${version}`,
base: `/${parts[parts.length - 1]}/`
}
私のライブラリ名はGithubリポジトリの名前と同じですので、ベースURLはパッケージから取得されます.JSONこれは何かを構築するのに十分です、そして、ビルドの明示的な表示のために、我々はドキュメンテーションに図書館のバージョンを発表しています.
on:
push:
paths:
- '*/**'
- '!docs/**'
で.ワークフロー/リリース.別のジョブを追加します.docs:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./docs
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '14.x'
- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"
- uses: actions/cache@v2
id: yarn-cache
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-docs-${{ hashFiles('yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-docs-
- run: yarn
- run: yarn build
- name: Commit changes
working-directory: ./docs/.vuepress/dist
run: |
git config --global user.name "github-actions"
git config --global user.email "[email protected]"
git init
git add -A
git commit -m "deploy"
- name: Push changes
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: gh-pages
force: true
directory: ./docs/.vuepress/dist
論理は以下の通りです.yarn install
インサイドdocs
フォルダgh-pages
枝gh-pages
ブランチGithub Pages ドキュメントを表示するには結論
おめでとう、我々は多くの仕事を自動化しました、現在、巨大なスクリプトを書く必要はありません.NPMJSでコードを展開することを忘れてしまうこともあります.
しかし、あなたはまた、例えば、あなたがConventional Commits , また、タグの作成を自動化することもできますstandard-version 図書館.
そして、あらゆる種類のツールを使用するactions/labeler .
リファレンス
Reference
この問題について(Githubアクションを使ってNPMJSに自動配備するJavaScriptライブラリ), 我々は、より多くの情報をここで見つけました https://dev.to/oleksiikhr/autodeploy-javascript-library-to-npmjs-via-github-actions-3bfcテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol