From 9e3f0e0ac8212f6428eb98c427749a112aa05b73 Mon Sep 17 00:00:00 2001 From: Armand Sauzay <35524799+armand-sauzay@users.noreply.github.com> Date: Fri, 15 Jul 2022 20:14:54 +0200 Subject: [PATCH] feat: initial action creation --- .commitlintrc.yaml | 2 + .github/workflows/ci.yaml | 22 +++++++ .github/workflows/release.yaml | 33 ++++++++++ .pre-commit-config.yaml | 24 +++++++ .releaserc.json | 9 +-- README.md | 1 + lint/README.md | 41 ++++++++++++ lint/action.yaml | 29 ++++++++ release/README.md | 117 +++++++++++++++++++++++++++++++++ release/action.yaml | 60 +++++++++++++++++ test/README.md | 43 ++++++++++++ test/action.yaml | 35 ++++++++++ 12 files changed, 408 insertions(+), 8 deletions(-) create mode 100644 .commitlintrc.yaml create mode 100644 .github/workflows/ci.yaml create mode 100644 .github/workflows/release.yaml create mode 100644 .pre-commit-config.yaml create mode 100644 lint/README.md create mode 100644 lint/action.yaml create mode 100644 release/README.md create mode 100644 release/action.yaml create mode 100644 test/README.md create mode 100644 test/action.yaml diff --git a/.commitlintrc.yaml b/.commitlintrc.yaml new file mode 100644 index 0000000..5d2a5ab --- /dev/null +++ b/.commitlintrc.yaml @@ -0,0 +1,2 @@ +extends: + - "@open-turo/commitlint-config-conventional" diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..576eb96 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,22 @@ +name: CI + +on: + pull_request: + branches: [main] + +jobs: + lint: + name: Lint + runs-on: ubuntu-latest + steps: + - uses: open-turo/actions-gha/lint@v1 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + + test: + name: Test + runs-on: ubuntu-latest + steps: + - uses: open-turo/actions-gha/test@v1 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 0000000..05195d8 --- /dev/null +++ b/.github/workflows/release.yaml @@ -0,0 +1,33 @@ +name: Release + +on: + push: + branches: [main] + +jobs: + lint: + name: Lint + runs-on: ubuntu-latest + steps: + - uses: open-turo/actions-gha/lint@v1 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + + test: + name: Test + runs-on: ubuntu-latest + steps: + - uses: open-turo/actions-gha/test@v1 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + + release: + needs: + - lint + - test + name: Release + runs-on: ubuntu-latest + steps: + - uses: open-turo/actions-gha/release@v1 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..f289315 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,24 @@ +exclude: dist/.* +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.1.0 # Use the ref you want to point at + hooks: + - id: check-json + - id: check-yaml + - id: end-of-file-fixer + - id: trailing-whitespace + - repo: https://github.com/pre-commit/mirrors-prettier + rev: v2.5.1 + hooks: + - id: prettier + stages: [commit] + - repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook + rev: v8.0.0 + hooks: + - id: commitlint + stages: [commit-msg] + additional_dependencies: ["@open-turo/commitlint-config-conventional"] + - repo: https://github.com/rhysd/actionlint + rev: v1.6.8 + hooks: + - id: actionlint diff --git a/.releaserc.json b/.releaserc.json index df364cb..7bf05ec 100644 --- a/.releaserc.json +++ b/.releaserc.json @@ -1,12 +1,5 @@ { - "branches": [ - "main", - { - "channel": "next", - "name": "(f|b|c)/*", - "prerelease": "beta-<%= (/^\\w+-\\d+/.exec(name.substr(2)) || [])[0] %>" - } - ], + "branches": ["main"], "plugins": [ "@semantic-release/commit-analyzer", "@semantic-release/release-notes-generator", diff --git a/README.md b/README.md index c5670f8..a9d54ad 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # actions-python + GitHub Actions for Python repositories diff --git a/lint/README.md b/lint/README.md new file mode 100644 index 0000000..4bb4da9 --- /dev/null +++ b/lint/README.md @@ -0,0 +1,41 @@ +# GitHub Action Lint + +## Description + +GitHub Action that lints a python based repository + +## Usage + +```yaml +jobs: + build: + steps: + - name: Lint + uses: open-turo/actions-python/lint@v1 + with: + ## example value for github-token provided below + github-token: ${{ secrets.GITHUB_TOKEN }} +``` + +## Inputs + +| parameter | description | required | default | +| ------------- | ----------------------------------------------------------------------------------------------------------------- | -------- | ------------------- | +| checkout-repo | Perform checkout as first step of action | `false` | true | +| pypi-token | PyPI token that can checkout the repository as well as create tags/releases against it. e.g. 'secrets.PYPI_TOKEN' | `false` | | +| github-token | GitHub token that can checkout the repository. e.g. 'secrets.GITHUB_TOKEN' | `true` | ${{ github.token }} | + +## Runs + +This action is an `composite` action. + +## Lint Checks + +This action runs the following lint checks: + +- [action-pre-commit](https://github.com/open-turo/action-pre-commit) + +## Notes + +- By default, this action will perform actions/checkout as its first step. +- This expects that `.commitlintrc.yaml` will be present to enforce [`conventional-commit`](https://github.com/wagoid/commitlint-github-action). diff --git a/lint/action.yaml b/lint/action.yaml new file mode 100644 index 0000000..03cd534 --- /dev/null +++ b/lint/action.yaml @@ -0,0 +1,29 @@ +name: Python Lint +description: GitHub Action that lints a Python based repository +inputs: + checkout-repo: + required: false + description: Perform checkout as first step of action + default: "true" + pypi-token: + required: false + description: PyPI token that can checkout the repository as well as create tags/releases against it. e.g. 'secrets.PYPI_TOKEN' + github-token: + required: true + description: GitHub token that can checkout the repository. e.g. 'secrets.GITHUB_TOKEN' + default: ${{ github.token }} +runs: + using: composite + steps: + - name: Checkout + uses: actions/checkout@v3 + if: inputs.checkout-repo == 'true' + with: + fetch-depth: 0 + - name: Setup tools + uses: open-turo/action-setup-tools@v1 + - name: Install dependencies + shell: bash + run: pip install -e . + - name: Pre-commit + uses: open-turo/action-pre-commit@v1 diff --git a/release/README.md b/release/README.md new file mode 100644 index 0000000..a0f61f0 --- /dev/null +++ b/release/README.md @@ -0,0 +1,117 @@ +# GitHub Action Release + +## Description + +GitHub Action that publishes a new release. + +## Configuration + +### Step1: Set any [Semantic Release Configuration](https://github.com/semantic-release/semantic-release/blob/master/docs/usage/configuration.md#configuration) in your repository. + +### Step2: [Add Secrets](https://help.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets) in your repository for the [Semantic Release Authentication](https://github.com/semantic-release/semantic-release/blob/master/docs/usage/ci-configuration.md#authentication) Environment Variables. + +### Step3: Add a [Workflow File](https://help.github.com/en/articles/workflow-syntax-for-github-actions) to your repository to create custom automated processes. + +## Usage + +```yaml +steps: + - name: Release + uses: open-turo/actions-python/release@v1 + with: + ## example value for github-token provided below + github-token: ${{ secrets.GITHUB_TOKEN }} +``` + +**IMPORTANT**: `GITHUB_TOKEN` does not have the required permissions to operate on protected branches. +If you are using this action for protected branches, replace `GITHUB_TOKEN` with [Personal Access Token](https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line). If using the `@semantic-release/git` plugin for protected branches, avoid persisting credentials as part of `actions/checkout@v3` by setting the parameter `persist-credentials: false`. This credential does not have the required permission to operate on protected branches. + +## Inputs + +| parameter | description | required | default | +| ------------- | --------------------------------------------------------------------------------------------------------------------- | -------- | ------- | +| checkout-repo | Perform checkout as first step of action | `false` | true | +| dry-run | Whether to run semantic release in `dry-run` mode. It will override the `dryRun` attribute in your configuration file | `false` | `false` | +| extra-plugins | Extra plugins for pre-install. You can also specify specifying version range for the extra plugins if you prefer. | `false` | | +| github-token | GitHub token that can checkout the repository as well as create tags/releases against it. e.g. 'secrets.GITHUB_TOKEN' | `true` | | + +## Outputs + +| parameter | description | +| ------------------------- | ----------------------------------- | +| new-release-published | Whether a new release was published | +| new-release-version | Version of the new release | +| new-release-major-version | Major version of the new release | + +## Runs + +This action is an `composite` action. + +## Additional Examples + +### extra-plugins example + +The Action can be used with `extra-plugins` option to specify plugins which are not in the [default list of plugins of semantic release](https://semantic-release.gitbook.io/semantic-release/usage/plugins#default-plugins). When using this option, please make sure that these plugins are also mentioned in your [semantic release config's plugins](https://semantic-release.gitbook.io/semantic-release/usage/configuration#plugins) array. + +For example, if you want to use `@semantic-release/git` and `@semantic-release/changelog` extra plugins, these must be added to `extra-plugins` in your actions file and `plugins` in your [release config file](https://semantic-release.gitbook.io/semantic-release/usage/configuration#configuration-file) as shown bellow: + +Github Action Workflow: + +```yaml +steps: + - name: Release + uses: open-turo/actions-python/release@v1 + with: + # You can specify specifying version range for the extra plugins if you prefer. + github-token: ${{ secrets.GITHUB_TOKEN }} + extra-plugins: | + @semantic-release/changelog@3.0.0 + @semantic-release/git +``` + +Similar to parameter `semantic_version`. _It is recommended to manually specify a version of semantic-release plugins to prevent errors caused._ + +Release Config: + +```diff + plugins: [ + . ++ "@semantic-release/changelog" ++ "@semantic-release/git", + ] +``` + +### dry-run example + +```yaml +jobs: + build: + steps: + - name: Release + uses: open-turo/actions-python/release@v1 + with: + dry-run: true + github-token: ${{ secrets.GITHUB_TOKEN }} +``` + +### using output parameters example + +```yaml +jobs: + build: + steps: + - name: Release + uses: open-turo/actions-python/release@v1 + id: semantic # Need an `id` for output variables + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + - name: Do something when a new release published + if: steps.semantic.outputs.new-release-published == 'true' + run: | + echo ${{ steps.semantic.outputs.new-release-version }} + echo ${{ steps.semantic.outputs.new-release-major-version }} +``` + +## Notes + +- By default, this action will perform actions/checkout as its first step. diff --git a/release/action.yaml b/release/action.yaml new file mode 100644 index 0000000..1557585 --- /dev/null +++ b/release/action.yaml @@ -0,0 +1,60 @@ +name: Python Release & Publish +description: Python that publishes a new release. +inputs: + checkout-repo: + required: false + description: Perform checkout as first step of action + default: "true" + github-token: + required: true + description: GitHub token that can checkout the repository as well as create tags/releases against it. e.g. 'secrets.GITHUB_TOKEN' + default: ${{ github.token }} + pypi-token: + required: false + description: PyPI token that can checkout the repository as well as create tags/releases against it. e.g. 'secrets.PYPI_TOKEN' + dry-run: + required: false + description: Whether to run semantic release in `dry-run` mode. It will override the `dryRun` attribute in your configuration file + default: "false" + extra-plugins: + required: false + description: Extra plugins for pre-install. You can also specify specifying version range for the extra plugins if you prefer. +outputs: + new-release-published: + description: Whether a new release was published + value: ${{ steps.release.outputs.new_release_published }} + new-release-version: + description: Version of the new release + value: ${{ steps.release.outputs.new_release_version }} + new-release-major-version: + description: Major version of the new release + value: ${{ steps.release.outputs.new_release_major_version }} +runs: + using: composite + steps: + - name: Checkout + uses: actions/checkout@v3 + if: inputs.checkout-repo + with: + fetch-depth: 0 + - name: Set node.js version + if: hashFiles('.node-version') == '' + shell: bash + run: echo '16.14.2' > .node-version + - name: Setup tools + uses: open-turo/action-setup-tools@v1 + # - name: Install dependencies + # shell: bash + # run: pip install -e . + - name: Install setuptools + shell: bash + run: python -m pip install --upgrade setuptools wheel twine + - name: Release + id: release + uses: cycjimmy/semantic-release-action@v2 + with: + dry_run: ${{ inputs.dry-run }} + extra_plugins: ${{ inputs.extra-plugins }} + env: + GITHUB_TOKEN: ${{ inputs.github-token }} + PYPI_TOKEN: ${{ inputs.pypi-token }} diff --git a/test/README.md b/test/README.md new file mode 100644 index 0000000..a93a7c7 --- /dev/null +++ b/test/README.md @@ -0,0 +1,43 @@ +# `open-turo/actions-python/test` GitHub Action for running Python Tests + +## Description + +GitHub Action for running tests in a Python repository using [pytest](https://github.com/pytest-dev/pytest). This action will also check out the repository if `checkout-repo` is passed, as well as `pip install .[dev]` and `pip install .[test]` for dependencies. + +## Usage + +```yaml +jobs: + test: + steps: + - name: Test + uses: open-turo/actions-python/test@v1 + with: + ## example value for github-token provided below + github-token: ${{ secrets.GITHUB_TOKEN }} +``` + +## Inputs + +| parameter | description | required | default | +| ------------- | ----------------------------------------------------------------------------------------------------------------- | -------- | ------------------- | +| checkout-repo | Perform checkout as first step of action | `false` | true | +| github-token | GitHub token that can checkout the repository. e.g. 'secrets.GITHUB_TOKEN' | `true` | ${{ github.token }} | +| pypi-token | PyPI token that can checkout the repository as well as create tags/releases against it. e.g. 'secrets.PYPI_TOKEN' | `false` | | +| test-flags | Flags and args for test command | `false` | | + +## Runs + +This action is an `composite` action. + +## Test + +It will run: + +```shell +pytest ${test-flags} +``` + +## Notes + +- By default, this action will perform actions/checkout as its first step. diff --git a/test/action.yaml b/test/action.yaml new file mode 100644 index 0000000..c7bee24 --- /dev/null +++ b/test/action.yaml @@ -0,0 +1,35 @@ +name: Python Test +description: GitHub Action that tests a python based repository +inputs: + checkout-repo: + required: false + description: Perform checkout as first step of action + default: "true" + github-token: + required: true + description: GitHub token that can checkout the repository. e.g. 'secrets.GITHUB_TOKEN' + default: ${{ github.token }} + pypi-token: + required: false + description: PyPI token that can checkout the repository as well as create tags/releases against it. e.g. 'secrets.PYPI_TOKEN' + test-flags: + required: false + description: Flags and args for test command + default: "" +runs: + using: composite + steps: + - name: Checkout + uses: actions/checkout@v3 + if: inputs.checkout-repo == 'true' + - name: Setup tools + uses: open-turo/action-setup-tools@v1 + - name: Install dependencies + shell: bash + run: | + pip install -e . + pip install -e .[dev] || true + pip install -e .[test] || true + - name: Run tests + shell: bash + run: pytest ${{ inputs.test-flags }}