diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 6f89582..e47eb51 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -13,14 +13,24 @@ jobs: runs-on: ubuntu-latest if: ${{ !github.event.created && github.repository != 'garronej/ts_ci' }} steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v1 - - run: npm ci + - uses: actions/checkout@v2.3.4 + - uses: actions/setup-node@v2.1.3 - name: Making sure 'npm run lint' and 'npm run format' does not changes anything. run: | - npm run lint:check - npm run format:check - test_node: + 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 lint:check + $PACKAGE_MANAGER run format:check + + test: runs-on: ${{ matrix.os }} needs: test_lint strategy: @@ -30,72 +40,120 @@ jobs: name: Test with Node v${{ matrix.node }} on ${{ matrix.os }} steps: - name: Tell if project is using npm or yarn - id: _1 - uses: garronej/github_actions_toolkit@v1.11 + id: step1 + uses: garronej/github_actions_toolkit@v2.1 with: action_name: tell_if_project_uses_npm_or_yarn - owner: ${{github.repository_owner}} - repo: ${{github.event.repository.name}} - branch: ${{github.ref}} - - uses: actions/checkout@v2 - - uses: actions/setup-node@v1 + - uses: actions/checkout@v2.3.4 + - uses: actions/setup-node@v2.1.3 with: node-version: ${{ matrix.node }} - - if: steps._1.outputs.npm_or_yarn == 'yarn' + - if: steps.step1.outputs.npm_or_yarn == 'yarn' run: | yarn install --frozen-lockfile - yarn run build - yarn run test - - if: steps._1.outputs.npm_or_yarn == 'npm' + yarn build + yarn test + - if: steps.step1.outputs.npm_or_yarn == 'npm' run: | npm ci npm run build - npm run test - trigger_publish: - name: Trigger publish.yaml workflow if package.json version updated ( and secrets.PAT is set ). + npm test + + check_if_version_upgraded: + name: Check if version upgrade + if: github.event_name == 'push' runs-on: ubuntu-latest - env: - PAT: ${{secrets.PAT}} - if: github.event_name == 'push' && github.event.head_commit.author.name != 'ts_ci' - needs: test_node + needs: test + outputs: + from_version: ${{ steps.step1.outputs.from_version }} + to_version: ${{ steps.step1.outputs.to_version }} + is_upgraded_version: ${{steps.step1.outputs.is_upgraded_version }} steps: - - - name: Get version on latest - id: v_latest - uses: garronej/github_actions_toolkit@v1.11 + - uses: garronej/github_actions_toolkit@v2.1 + id: step1 with: - action_name: get_package_json_version - owner: ${{github.repository_owner}} - repo: ${{github.event.repository.name}} - branch: latest - compare_to_version: '0.0.0' + action_name: is_package_json_version_upgraded - - name: Get version on main - id: v_main - uses: garronej/github_actions_toolkit@v1.11 - with: - action_name: get_package_json_version - owner: ${{github.repository_owner}} - repo: ${{github.event.repository.name}} - branch: ${{ github.sha }} - compare_to_version: ${{steps.v_latest.outputs.version || '0.0.0'}} - - - name: 'Trigger the ''publish'' workflow' - if: ${{ !!env.PAT && steps.v_main.outputs.compare_result == '1' }} - uses: garronej/github_actions_toolkit@v1.11 - env: - GITHUB_TOKEN: ${{ secrets.PAT }} + update_changelog: + runs-on: ubuntu-latest + 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: - action_name: dispatch_event - owner: ${{github.repository_owner}} - repo: ${{github.event.repository.name}} - event_type: publish - client_payload_json: | - ${{ - format( - '{{"from_version":"{0}","to_version":"{1}","repo":"{2}"}}', - steps.v_latest.outputs.version, - steps.v_main.outputs.version, - github.event.repository.name - ) - }} + action_name: update_changelog + branch: ${{ github.ref }} + commit_author_email: ts_ci@github.com + + create_github_release: + runs-on: ubuntu-latest + 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: + 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: + name: Release v${{ needs.check_if_version_upgraded.outputs.to_version }} + tag_name: v${{ needs.check_if_version_upgraded.outputs.to_version }} + target_commitish: ${{ github.ref }} + body: ${{ steps.step1.outputs.body }} + draft: false + prerelease: false + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + publish: + 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 }} diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml deleted file mode 100644 index 088eace..0000000 --- a/.github/workflows/publish.yaml +++ /dev/null @@ -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 }} \ No newline at end of file diff --git a/README.md b/README.md index a598a27..723d56e 100644 --- a/README.md +++ b/README.md @@ -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. # Presentation @@ -16,20 +17,17 @@ This template automates the boring and tedious tasks of: - Filling up the ``package.json`` - 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. - Maintaining a CHANGELOG. - Publishing on NPM and creating corresponding GitHub releases. 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. ``import {...} from "my_module/theFile"`` instead of the usual ``import {...} from "my_module/dist/theFile"`` - CDN distribution for importing from an ``.html`` file with a ``