Make the workflow more straignforward

This commit is contained in:
Joseph Garrone 2021-03-15 00:19:37 +01:00
parent a24a61b3db
commit b2cf360940
4 changed files with 125 additions and 229 deletions

View file

@ -13,14 +13,24 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
if: ${{ !github.event.created && github.repository != 'garronej/ts_ci' }} if: ${{ !github.event.created && github.repository != 'garronej/ts_ci' }}
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2.3.4
- uses: actions/setup-node@v1 - uses: actions/setup-node@v2.1.3
- run: npm ci
- name: Making sure 'npm run lint' and 'npm run format' does not changes anything. - name: Making sure 'npm run lint' and 'npm run format' does not changes anything.
run: | run: |
npm run lint:check PACKAGE_MANAGER=npm
npm run format:check if [ -f "./yarn.lock" ]; then
test_node: PACKAGE_MANAGER=yarn
fi
if [ "$PACKAGE_MANAGER" = "yarn" ]; then
yarn install --frozen-lockfile
else
npm ci
fi
$PACKAGE_MANAGER run lint:check
$PACKAGE_MANAGER run format:check
test:
runs-on: ${{ matrix.os }} runs-on: ${{ matrix.os }}
needs: test_lint needs: test_lint
strategy: strategy:
@ -30,72 +40,120 @@ jobs:
name: Test with Node v${{ matrix.node }} on ${{ matrix.os }} name: Test with Node v${{ matrix.node }} on ${{ matrix.os }}
steps: steps:
- name: Tell if project is using npm or yarn - name: Tell if project is using npm or yarn
id: _1 id: step1
uses: garronej/github_actions_toolkit@v1.11 uses: garronej/github_actions_toolkit@v2.1
with: with:
action_name: tell_if_project_uses_npm_or_yarn action_name: tell_if_project_uses_npm_or_yarn
owner: ${{github.repository_owner}} - uses: actions/checkout@v2.3.4
repo: ${{github.event.repository.name}} - uses: actions/setup-node@v2.1.3
branch: ${{github.ref}}
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with: with:
node-version: ${{ matrix.node }} node-version: ${{ matrix.node }}
- if: steps._1.outputs.npm_or_yarn == 'yarn' - if: steps.step1.outputs.npm_or_yarn == 'yarn'
run: | run: |
yarn install --frozen-lockfile yarn install --frozen-lockfile
yarn run build yarn build
yarn run test yarn test
- if: steps._1.outputs.npm_or_yarn == 'npm' - if: steps.step1.outputs.npm_or_yarn == 'npm'
run: | run: |
npm ci npm ci
npm run build npm run build
npm run test npm test
trigger_publish:
name: Trigger publish.yaml workflow if package.json version updated ( and secrets.PAT is set ). check_if_version_upgraded:
name: Check if version upgrade
if: github.event_name == 'push'
runs-on: ubuntu-latest runs-on: ubuntu-latest
env: needs: test
PAT: ${{secrets.PAT}} outputs:
if: github.event_name == 'push' && github.event.head_commit.author.name != 'ts_ci' from_version: ${{ steps.step1.outputs.from_version }}
needs: test_node to_version: ${{ steps.step1.outputs.to_version }}
is_upgraded_version: ${{steps.step1.outputs.is_upgraded_version }}
steps: steps:
- uses: garronej/github_actions_toolkit@v2.1
- name: Get version on latest id: step1
id: v_latest
uses: garronej/github_actions_toolkit@v1.11
with: with:
action_name: get_package_json_version action_name: is_package_json_version_upgraded
owner: ${{github.repository_owner}}
repo: ${{github.event.repository.name}}
branch: latest
compare_to_version: '0.0.0'
- name: Get version on main update_changelog:
id: v_main runs-on: ubuntu-latest
uses: garronej/github_actions_toolkit@v1.11 needs: check_if_version_upgraded
if: needs.check_if_version_upgraded.outputs.is_upgraded_version == 'true'
steps:
- uses: garronej/github_actions_toolkit@v2.2
with: with:
action_name: get_package_json_version action_name: update_changelog
owner: ${{github.repository_owner}} branch: ${{ github.ref }}
repo: ${{github.event.repository.name}} commit_author_email: ts_ci@github.com
branch: ${{ github.sha }}
compare_to_version: ${{steps.v_latest.outputs.version || '0.0.0'}}
- name: 'Trigger the ''publish'' workflow' create_github_release:
if: ${{ !!env.PAT && steps.v_main.outputs.compare_result == '1' }} runs-on: ubuntu-latest
uses: garronej/github_actions_toolkit@v1.11 needs:
- update_changelog
- check_if_version_upgraded
steps:
- uses: actions/checkout@v2
with:
ref: ${{ github.ref }}
- name: Build GitHub release body
id: step1
run: |
if [ "$FROM_VERSION" = "0.0.0" ]; then
echo "::set-output name=body::🚀"
else
echo "::set-output name=body::📋 [CHANGELOG](https://github.com/$GITHUB_REPOSITORY/blob/v$TO_VERSION/CHANGELOG.md)"
fi
env: env:
GITHUB_TOKEN: ${{ secrets.PAT }} FROM_VERSION: ${{ needs.check_if_version_upgraded.outputs.from_version }}
TO_VERSION: ${{ needs.check_if_version_upgraded.outputs.to_version }}
- uses: garronej/action-gh-release@v0.2.0
with: with:
action_name: dispatch_event name: Release v${{ needs.check_if_version_upgraded.outputs.to_version }}
owner: ${{github.repository_owner}} tag_name: v${{ needs.check_if_version_upgraded.outputs.to_version }}
repo: ${{github.event.repository.name}} target_commitish: ${{ github.ref }}
event_type: publish body: ${{ steps.step1.outputs.body }}
client_payload_json: | draft: false
${{ prerelease: false
format( env:
'{{"from_version":"{0}","to_version":"{1}","repo":"{2}"}}', GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps.v_latest.outputs.version,
steps.v_main.outputs.version, publish:
github.event.repository.name runs-on: ubuntu-latest
) needs:
}} - update_changelog
- check_if_version_upgraded
steps:
- uses: actions/checkout@v2
with:
ref: ${{ github.ref }}
- uses: actions/setup-node@v2.1.3
- run: |
PACKAGE_MANAGER=npm
if [ -f "./yarn.lock" ]; then
PACKAGE_MANAGER=yarn
fi
if [ "$PACKAGE_MANAGER" = "yarn" ]; then
yarn install --frozen-lockfile
else
npm ci
fi
$PACKAGE_MANAGER run build
- run: npx -y -p denoify@0.6.5 denoify_enable_short_npm_import_path
env:
DRY_RUN: "0"
- name: Publishing on NPM
run: |
if [ "$(npm show . version)" = "$VERSION" ]; then
echo "This version is already published"
exit 0
fi
if [ "$NPM_TOKEN" = "" ]; then
echo "Can't publish on NPM, You must first create a secret called NPM_TOKEN that contains your NPM auth token. https://help.github.com/en/actions/automating-your-workflow-with-github-actions/creating-and-using-encrypted-secrets"
false
fi
echo '//registry.npmjs.org/:_authToken=${NPM_TOKEN}' > .npmrc
npm publish
rm .npmrc
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
VERSION: ${{ needs.check_if_version_upgraded.outputs.to_version }}

View file

@ -1,116 +0,0 @@
on:
repository_dispatch:
types: publish
jobs:
update_changelog_and_sync_package_lock_version:
name: Update CHANGELOG.md and make sure package.json and package-lock.json versions matches.
runs-on: ubuntu-latest
steps:
- name: Synchronize package.json and package-lock.json version if needed.
uses: garronej/github_actions_toolkit@v1.11
env:
GITHUB_TOKEN: ${{ secrets.PAT }}
with:
action_name: sync_package_and_package_lock_version
owner: ${{github.repository_owner}}
repo: ${{github.event.client_payload.repo}}
branch: main
commit_author_email: ts_ci@github.com
- name: Update CHANGELOG.md
if: ${{ !!github.event.client_payload.from_version }}
uses: garronej/github_actions_toolkit@v1.11
env:
GITHUB_TOKEN: ${{ secrets.PAT }}
with:
action_name: update_changelog
owner: ${{github.repository_owner}}
repo: ${{github.event.client_payload.repo}}
branch_behind: latest
branch_ahead: main
commit_author_email: ts_ci@github.com
exclude_commit_from_author_names_json: '["ts_ci"]'
publish:
runs-on: ubuntu-latest
needs: update_changelog_and_sync_package_lock_version
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
ref: main
- name: Remove .github directory, useless on 'latest' branch
run: rm -r .github
- name: Remove branch 'latest'
continue-on-error: true
run: |
git branch -d latest || true
git push origin :latest
- name: Create the new 'latest' branch
run: |
git branch latest
git checkout latest
- uses: actions/setup-node@v1
- run: |
if [ -f "./yarn.lock" ]; then
yarn install --frozen-lockfile
else
npm ci
fi
- run: |
PACKAGE_MANAGER=npm
if [ -f "./yarn.lock" ]; then
PACKAGE_MANAGER=yarn
fi
$PACKAGE_MANAGER run enable_short_import_path
env:
DRY_RUN: "0"
- name: (DEBUG) Show how the files have been moved to enable short import
run: ls -lR
- name: Publishing on NPM
run: |
if [ "$(npm show . version)" = "$VERSION" ]; then
echo "This version is already published"
exit 0
fi
if [ "$NPM_TOKEN" = "" ]; then
echo "Can't publish on NPM, You must first create a secret called NPM_TOKEN that contains your NPM auth token. https://help.github.com/en/actions/automating-your-workflow-with-github-actions/creating-and-using-encrypted-secrets"
false
fi
echo '//registry.npmjs.org/:_authToken=${NPM_TOKEN}' > .npmrc
npm publish
rm .npmrc
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
VERSION: ${{ github.event.client_payload.to_version }}
- name: Commit changes
run: |
git config --local user.email "ts_ci@github.com"
git config --local user.name "ts_ci"
git add -A
git commit -am "Enabling shorter import paths [automatic]"
- run: git push origin latest
- name: Release body
id: id_rb
run: |
if [ "$FROM_VERSION" = "" ]; then
echo "::set-output name=body::🚀"
else
echo "::set-output name=body::📋 [CHANGELOG](https://github.com/$OWNER/$REPO/blob/$REF/CHANGELOG.md)"
fi
env:
FROM_VERSION: ${{ github.event.client_payload.from_version }}
OWNER: ${{github.repository_owner}}
REPO: ${{github.event.client_payload.repo}}
REF: v${{github.event.client_payload.to_version}}
- name: Create Release
uses: garronej/create-release@master
env:
GITHUB_TOKEN: ${{ secrets.PAT }}
with:
tag_name: v${{ github.event.client_payload.to_version }}
release_name: Release v${{ github.event.client_payload.to_version }}
branch: latest
draft: false
prerelease: false
body: ${{ steps.id_rb.outputs.body }}

View file

@ -9,6 +9,7 @@
--- ---
✅ NEW: The workflow is now much more straightforward and portable.
✅ NEW: `yarn` support You are now free to use yarn instead of `npm` if you'd like to. ✅ NEW: `yarn` support You are now free to use yarn instead of `npm` if you'd like to.
# Presentation # Presentation
@ -16,20 +17,17 @@
This template automates the boring and tedious tasks of: This template automates the boring and tedious tasks of:
- Filling up the ``package.json`` - Filling up the ``package.json``
- Setting up Typescript. - Setting up Typescript.
- Writing a [README.md](https://github.com/garronej/ts_ci/blob/master/README.template.md) with decent presentation and instructions on how to install/import your module. - Writing a [README.md](https://github.com/garronej/ts_ci/blob/main/README.template.md) with decent presentation and instructions on how to install/import your module.
- Testing on multiple Node version running on Ubuntu and Windows before publishing. - Testing on multiple Node version running on Ubuntu and Windows before publishing.
- Maintaining a CHANGELOG. - Maintaining a CHANGELOG.
- Publishing on NPM and creating corresponding GitHub releases. - Publishing on NPM and creating corresponding GitHub releases.
Besides, good stuff that comes with using this template: Besides, good stuff that comes with using this template:
- No dist files are tracked on the ``master`` branch. - No dist files are tracked on the ``main`` branch.
- Shorter specific file import path. - Shorter specific file import path.
``import {...} from "my_module/theFile"`` instead of the usual ``import {...} from "my_module/theFile"`` instead of the usual
``import {...} from "my_module/dist/theFile"`` ``import {...} from "my_module/dist/theFile"``
- CDN distribution for importing from an ``.html`` file with a ``<script>`` tag. - CDN distribution for importing from an ``.html`` file with a ``<script>`` tag.
- A branch ``latest`` always in sync with the latest release.
- When your users hit *"Go to Definition"* they get redirected to the actual ``.ts`` source file instead of the ``.d.ts``.
( Feature disabled by default, refer to [instructions](#enabling-go-to-definition-to-redirect-to-the-source-ts-file) on how to enable it ).
- ESlint and Prettier are automatically run against files staged for commit. ( You can [disable](#disable-linting-and-formatting) this feature. ) - ESlint and Prettier are automatically run against files staged for commit. ( You can [disable](#disable-linting-and-formatting) this feature. )
If you want your module to support Deno as well checkout [denoify_ci](https://github.com/garronej/denoify_ci). If you want your module to support Deno as well checkout [denoify_ci](https://github.com/garronej/denoify_ci).
@ -44,7 +42,6 @@ If you want your module to support Deno as well checkout [denoify_ci](https://gi
- [Few things you need to be aware of before getting started](#few-things-you-need-to-be-aware-of-before-getting-started) - [Few things you need to be aware of before getting started](#few-things-you-need-to-be-aware-of-before-getting-started)
- [Customization:](#customization) - [Customization:](#customization)
- [Changing the directories structure](#changing-the-directories-structure) - [Changing the directories structure](#changing-the-directories-structure)
- [Enabling "Go to Definition" to redirect to the source ``.ts`` file](#enabling-go-to-definition-to-redirect-to-the-source-ts-file)
- [Swipe the image in the ``README.md``](#swipe-the-image-in-the-readmemd) - [Swipe the image in the ``README.md``](#swipe-the-image-in-the-readmemd)
- [Disable linting and formatting](#disable-linting-and-formatting) - [Disable linting and formatting](#disable-linting-and-formatting)
- [Disable Prettier](#disable-prettier) - [Disable Prettier](#disable-prettier)
@ -80,11 +77,10 @@ Each time you will push changes ``npm test`` will be run on remote docker contai
## Enable automatic publishing ## Enable automatic publishing
Once you are ready to make your package available on NPM you Once you are ready to make your package available on NPM you
will need to provide two tokens so that the workflow can publish on your behalf: will need to provide your NPM token so that the workflow can publish on your behalf:
Go to repository ``Settings`` tab, then ``Secrets`` you will need to add two new secrets: Go to repository ``Settings`` tab, then ``Secrets`` you will need to add a new secret:
- ``NPM_TOKEN``, you NPM authorization token. ``NPM_TOKEN``, you NPM authorization token.
- ``PAT``, GitHub **P**ersonal **A**ccess **T**oken with the **repo** authorization. [link](https://github.com/settings/tokens)
To trigger publishing edit the ``package.json`` ``version`` field ( ``0.0.0``-> ``0.0.1`` for example) then push changes... that's all ! To trigger publishing edit the ``package.json`` ``version`` field ( ``0.0.0``-> ``0.0.1`` for example) then push changes... that's all !
The publishing will actually be performed only if ``npm test`` passes. The publishing will actually be performed only if ``npm test`` passes.
@ -110,45 +106,6 @@ but don't forget to update your ``package.json`` ``main``, ``type`` and ``files`
</details> </details>
## Enabling "Go to Definition" to redirect to the source ``.ts`` file
<details>
<summary>Click to expand</summary>
There is no denying that it is more convenient when clicking "Go To Definition" to get redirected to
a file ``.ts`` file rather than to a ``.d.ts``.
To enable this feature simply point to the ``package.json``'s ``types`` filed to the ``main``'s source
file instead the type definition file ``.d.ts``.
For example you would replace:
```json
{
"main": "dist/index.js",
"types": "dist/index.d.ts",
}
```
by:
```json
{
"main": "dist/index.js",
"types": "src/index.ts",
}
```
Enabling this feature comes at a cost though. Be aware that if you use [optional chaining](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#optional-chaining) or [nullish coalescing](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#nullish-coalescing) for example, your module will only be importable
in projects using typescript 3.7 or newer ( version that introduces theses features ).
It is important to keep your project compatible with older TS version because
- You don't want to force your users to update the typescript version they use in their project,
updating typescript might break some other things in their code.
- In certain environments updating TypeScript is not an option. Take [Stackblitz](https://stackblitz.com)
for example.
</details>
## Swipe the image in the ``README.md`` ## Swipe the image in the ``README.md``
<details> <details>
@ -288,7 +245,6 @@ Dev dependencies that are not required by the template ( you can safely remove t
Must keep: Must keep:
- ``typescript`` - ``typescript``
- ``denoify`` ( for the script that moves dist files to the root before publishing )
- ``simplifyify`` ( for CDN build ) - ``simplifyify`` ( for CDN build )
- ``terser`` ( for CDN build ) - ``terser`` ( for CDN build )

View file

@ -16,7 +16,6 @@
"cdn:esm": "tsc -p tsconfig.esm.json", "cdn:esm": "tsc -p tsconfig.esm.json",
"cdn": "npm run cdn:bundle && npm run cdn:esm", "cdn": "npm run cdn:bundle && npm run cdn:esm",
"build": "tsc && npm run cdn", "build": "tsc && npm run cdn",
"enable_short_import_path": "npm run build && denoify_enable_short_npm_import_path",
"lint:check": "eslint . --ext .ts,.tsx", "lint:check": "eslint . --ext .ts,.tsx",
"lint": "npm run lint:check -- --fix", "lint": "npm run lint:check -- --fix",
"_format": "prettier '**/*.{ts,tsx,json,md}'", "_format": "prettier '**/*.{ts,tsx,json,md}'",
@ -51,7 +50,6 @@
"@types/node": "^10.0.0", "@types/node": "^10.0.0",
"@typescript-eslint/eslint-plugin": "^4.15.1", "@typescript-eslint/eslint-plugin": "^4.15.1",
"@typescript-eslint/parser": "^4.15.1", "@typescript-eslint/parser": "^4.15.1",
"denoify": "^0.6.4",
"eslint": "^7.20.0", "eslint": "^7.20.0",
"eslint-config-prettier": "^7.2.0", "eslint-config-prettier": "^7.2.0",
"evt": "^1.8.11", "evt": "^1.8.11",