From 7fb648969c57230e14b0afa5c4b1124ee3fc0ca9 Mon Sep 17 00:00:00 2001 From: Katie Gengler Date: Wed, 22 Feb 2023 16:21:35 -0500 Subject: [PATCH] An action to push a npm package contents to a branch --- README.md | 46 ++++++++++++++++++++++++++++++++++++- action.yml | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 action.yml diff --git a/README.md b/README.md index 2e0309c..625a194 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,46 @@ # put-built-npm-package-contents-on-branch -An action to build an npm package and push it's contents to a branch + +An action to build an npm package and push its contents to a branch. + +**Note:** This action assumes there is only one tarball in the working-directory +after the `pack` command. It will not work as expected with any others in the +directory. + +## Inputs + +### `token` (required) + +**Description:** A GitHub token with write access to the repository contents. +Either a personal access token or `${{ secrets.GITHUB_TOKEN }}` if write access +for that default token has been enabled in the repository settings. + +### `working-directory` (default: `'./'`) + +**Description:** The directory to run the pack command in. Should be the directory +that contains the package you want to publish to the branch. + +### `pack-command` (default: `npm pack`) + +**Description:** The command to generate the package tarball. + +### `branch` (default: `dist`) + +**Description:** The branch to push the package contents to. + +### `tmp-dir` (default: `./tmp/dist-action`) + +**Description:** Empty directory where the package tarball will be extracted. +This directory will be created if it does not exist. It will be deleted after +the action is complete. + +### `commit-message` (default: `"Publish dist for ${{ github.sha }}"`) + +**Description:** The commit message to use when pushing the built package to the branch. + +### `commit-author` (default: `github-actions[bot]`) + +**Description:** The author to use when pushing the built package to the branch. + +### `commit-email` (default: `github-actions[bot]@users.noreply.github.com`) + +**Description:** The email to use when pushing the built package to the branch. diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..8d367c0 --- /dev/null +++ b/action.yml @@ -0,0 +1,67 @@ +name: Publish Built package to a branch +description: Build each commit and publish to a branch +author: Katie Gengler +inputs: + token: + description: 'A GitHub token with write access to the repository contents' + required: true + working-directory: + description: 'Directory in which to run the pack command' + required: false + default: './' + pack-command: + description: 'Command to generate the package tarball' + required: false + default: 'npm pack' + branch: + description: 'Branch name to push the contents to' + required: false + default: 'dist' + tmp-dir: + description: 'Empty directory in which to unpack the package tarball; will be created if it does not exist' + required: false + default: './tmp/dist-action' + commit-message: + description: 'Commit message to use when pushing to the branch' + required: false + default: 'Publish dist for ${{ github.sha }}' + commit-user: + description: 'Name of the user to use when committing to the branch' + required: false + default: 'github-actions[bot]' + commit-email: + description: 'Email of the user to use when committing to the branch' + required: false + default: 'github-actions[bot]@users.noreply.github.com' + +runs: + using: "composite" + steps: + - name: 'Pack' + shell: 'bash' + run: ${{ inputs.pack-command }} + working-directory: ${{ inputs.working-directory }} + - name: 'Make tmpdir' + shell: 'bash' + run: mkdir -p ${{ inputs.tmp-dir }} + working-directory: ${{ inputs.working-directory }} + - name: 'Unpack' + shell: 'bash' + run: tar -xvzf *.tgz -C ${{ inputs.tmp-dir }} + working-directory: ${{ inputs.working-directory }} + - name: 'Upload published package contents to branch' + shell: 'bash' + run: | + cd tmp/dist-action/package + git config --global init.defaultBranch main + git init + git config user.name ${{ inputs.commit-user }} + git config user.email ${{ inputs.commit-email }} + git add . + git commit -m ${{ inputs.commit-message }} + git push --force "https://${{ github.actor }}:${{ inputs.token }}@github.com/${{ github.repository }}" main:${{ inputs.branch }} + working-directory: ${{ inputs.working-directory }} + - name: 'Cleanup' + shell: 'bash' + run: rm -rf ${{ inputs.tmp-dir }} + working-directory: ${{ inputs.working-directory }}