From 38d2b72806558b8105140686af444b1e170c6758 Mon Sep 17 00:00:00 2001 From: N/Ame <173015200+grqz@users.noreply.github.com> Date: Tue, 3 Sep 2024 20:16:21 +1200 Subject: [PATCH] Add release workflow to github, pypi, and docker hub (#10) * initial commit * modified: .github/workflows/release.yml * modified: .github/workflows/release.yml * fix path * debug * fix * fix * debug * try: pypi * modified project name for testing * test: docker * change docker hub release condition * - add docker meta tags - bump docker/build-push-action to v6 - add annotations metadata entry * - change taggerdate to creatordate - add a simple changelog to github release - run docker/pypi release after gh release as gh release workflow creates a new branch * Rel 0.2.2 - docker rel: + version from github release workflow * rel 0.2.3 - [gh]fix changelog - [docker] try peter-evans/dockerhub-description@v4 * rel 0.2.4 - [docker] get project description as docker img description - [docker] add official repository link - [pypi] remove `needs` to save time - [pypi] more concise builder * rel 0.2.5 - [github] better changelog - [docker] fix description: extract from the `meta` step * Release 0.2.6 - [github] fix unmatched single quotes * Release 0.2.7 - [github] Fix git log cmd when generating changelog: branch not yet created * REL0.2.8: [gh] fix CHANGELOG * REL 0.2.9: [gh] fix changelog path * - [github] fix git log - [github] change git log format * [pypi] test: add yt-dlp-get-pot as a dep * REL 0.2.11 [github] fix dep ver * REL 0.2.12 [github] try to fix dep error * rel 0.2.13: debug * REL 0.2.14 - [github] try to escape `<`&`>` - [github] try to remove dependencies from bundle * REL 0.2.15 - [github] dont include changelog in the release file: users - [github] completely remove dependencies packing code - [plugin] update `plugin/.gitignore` - [github] try to display commit msg in md code block in changelog * REL 0.2.16 - [github] changelog: use commit body * REL 0.2.17 - try to change changelog format * REL 0.2.18 increment version [description placeholder line 1] [description placeholder line 2] * [github] change changelog format * [REL0.2.19] Increment version DESC-1 DESC-2 * REL 0.2.20 - [gh] fix changelog * rel 0.2.21 - fix time format * increment version d * [gh] fix time range for changelog * 0.2.23 Update pyproject.toml * version 0.2.24 Update pyproject.toml * test pr * test the workflow triggered by a pr - [gh] cleanup * [gh] remove git tag prefix - [gh] fix pr run condition * - [docker] more tags - [gh] fix release condition * [gh] try to fix create release if condition * REL 0.2.26 - [gh] remove the redundant assignment to `tag` * REL 0.2.27 - fix docker tags for pr - [docker] cleanup * REL:0.2.27 [gh] cleanup: correct annotation position * Delete build.yml * REL 0.2.28 Prob last build, just to test whether everything is working * try to allow overwriting github releases * [gh] fix condition * read from release notes * 0.0.29: gh cleanup and final rel * Prepare to merge * [PyPI] add `yt-dlp-plugins-get-pot` keyword --- .github/workflows/build.yml | 26 ----- .github/workflows/release.yml | 186 ++++++++++++++++++++++++++++++++++ plugin/.gitignore | 6 ++ plugin/pyproject.toml | 26 +++++ 4 files changed, 218 insertions(+), 26 deletions(-) delete mode 100644 .github/workflows/build.yml create mode 100644 .github/workflows/release.yml create mode 100644 plugin/.gitignore create mode 100644 plugin/pyproject.toml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml deleted file mode 100644 index 31b027c..0000000 --- a/.github/workflows/build.yml +++ /dev/null @@ -1,26 +0,0 @@ -name: build - -on: - workflow_dispatch: - push: - branches: [master] - pull_request: - -concurrency: - group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} - cancel-in-progress: true - -jobs: - build: - runs-on: ubuntu-latest - timeout-minutes: 15 - steps: - - name: Checkout bgutil-ytdlp-pot-provider - uses: actions/checkout@v4 - - name: Build Docker image - uses: docker/build-push-action@v6 - with: - context: server/ - file: server/Dockerfile - push: false - tags: brainicism/bgutil-ytdlp-pot-provider:ci diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..d3da9d6 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,186 @@ +name: Create release +on: + workflow_dispatch: + pull_request: + +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +jobs: + release: + name: Release to Github + permissions: + contents: write + runs-on: ubuntu-latest + outputs: + version: ${{ steps.set_variables.outputs.version }} + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - uses: actions/setup-python@v5 + with: + python-version: "3.8" + + - name: Install Hatch + run: pipx install hatch + + - name: Set variables + id: set_variables + run: | + cp README.md plugin/ + cd plugin/ + metadata=$(hatch project metadata) + echo "::group::Variables" + cat << EOF | tee -a "$GITHUB_OUTPUT" + tag=$(git for-each-ref refs/tags --sort=-creatordate --format='%(refname:strip=2)' --count=1) + version=$(echo ${metadata} | jq -r .version) + project_name=$(echo ${metadata} | jq -r .name) + EOF + echo "::endgroup::" + - name: Bundle + if: github.event_name != 'pull_request' && format('refs/heads/{0}', github.event.repository.default_branch) == github.ref + # only release on default branch + id: bundle + env: + GH_TOKEN: ${{ github.token }} + tag: ${{ steps.set_variables.outputs.tag }} + version: ${{ steps.set_variables.outputs.version }} + project_name: ${{ steps.set_variables.outputs.project_name }} + shell: bash + run: | + cd plugin/ + mkdir bundle/ + cp -r yt_dlp_plugins bundle/ + cd bundle/ + find . -type f -name '*.py[co]' -delete -o -type d -name __pycache__ -delete + zip -9 --recurse-paths "${project_name}" * + printf "## What's Changed\n\n" >> CHANGELOG.md + gh pr list --json author,number,title,mergedAt --state merged -R ${{ github.repository }} \ + | jq -r --arg authortime "$(git log --format='%at' -n1 ${tag})" \ + '.[]|select((.mergedAt|strptime("%Y-%m-%dT%H:%M:%SZ")|mktime)>($authortime|tonumber))| "* \(.title) by @\(.author.login) in #\(.number|tostring)"' \ + >> CHANGELOG.md + printf "\n**Full Changelog**: ${{ github.server_url }}/${{ github.repository }}/compare/${tag}...${version}\n" >> CHANGELOG.md + r=$(cat CHANGELOG.md) # <--- Read release Notes + r="${r//'%'/'%25'}" # Multiline escape sequences for % + r="${r//$'\n'/'%0A'}" # Multiline escape sequences for '\n' + r="${r//$'\r'/'%0D'}" # Multiline escape sequences for '\r' + echo "RELEASE_BODY=$r" | tee -a $GITHUB_OUTPUT # <--- Set environment variable + + - name: Create Release on Github + if: github.event_name != 'pull_request' && format('refs/heads/{0}', github.event.repository.default_branch) == github.ref + # only release on default branch + uses: svenstaro/upload-release-action@v2 + with: + tag: ${{ steps.set_variables.outputs.version }} + release_name: | + ${{ steps.set_variables.outputs.project_name }} ${{ steps.set_variables.outputs.version }} + body: ${{ steps.bundle.outputs.RELEASE_BODY }} + file: plugin/bundle/${{ steps.set_variables.outputs.project_name }}.zip + asset_name: ${{ steps.set_variables.outputs.project_name }}.zip + overwrite: true + + release_pypi: + name: Release to PyPI + if: github.event_name != 'pull_request' && format('refs/heads/{0}', github.event.repository.default_branch) == github.ref + # only release on default branch + runs-on: ubuntu-latest + permissions: + id-token: write # mandatory for trusted publishing + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - uses: actions/setup-python@v5 + with: + python-version: "3.10" + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install build hatchling + + - name: Build + run: | + cd plugin/ + rm -rf dist/** + printf '%s\n\n' \ + "Official repository: <${{ github.server_url }}/${{ github.repository }}>" > ./README.md.new + cat ../README.md >> ./README.md.new && mv -f ./README.md.new ./README.md + python -m build --no-isolation . + + - name: Publish to PyPI + uses: pypa/gh-action-pypi-publish@release/v1 + with: + verbose: true + packages-dir: plugin/dist + + release_docker: + name: Build Docker Image + needs: release + runs-on: ubuntu-latest + permissions: + packages: write + env: + repository: ${{ github.repository }} # like `brainicism/bgutil-ytdlp-pot-provider` + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Docker meta + id: meta + uses: docker/metadata-action@v5 + with: + context: git + images: ${{ env.repository }} + tags: | + type=raw,value=latest,enable={{is_default_branch}} + # mark as latest only if built on default branch of repository + type=raw,value=ci,enable={{is_default_branch}} + # mark as ci only if built on default branch of repository + type=raw,value=${{ needs.release.outputs.version }},enable={{is_default_branch}} + # add the new git tag only if built on default branch of repository + type=sha + type=ref,event=branch + type=ref,event=pr + + - name: Login to DockerHub + if: github.event_name != 'pull_request' + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Build and push + uses: docker/build-push-action@v6 + with: + context: server/ + file: server/Dockerfile + push: ${{ github.event_name != 'pull_request' }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + annotations: ${{ steps.meta.outputs.annotations }} + + - name: Add official repository link to README.md + if: github.event_name != 'pull_request' + run: | + printf '%s\n\n' \ + "Official repository: <${{ github.server_url }}/${{ github.repository }}>" > ./README.md.new + cat ./README.md >> ./README.md.new && mv -f ./README.md.new ./README.md + + - name: Update repo description + if: github.event_name != 'pull_request' + uses: peter-evans/dockerhub-description@v4 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + repository: ${{ env.repository }} + short-description: ${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.description'] }} + readme-filepath: README.md diff --git a/plugin/.gitignore b/plugin/.gitignore new file mode 100644 index 0000000..d237d8f --- /dev/null +++ b/plugin/.gitignore @@ -0,0 +1,6 @@ +dist/** +README.md +CHANGELOG.md +*.pyc +*.pyo +__pycache__/ diff --git a/plugin/pyproject.toml b/plugin/pyproject.toml new file mode 100644 index 0000000..883bc92 --- /dev/null +++ b/plugin/pyproject.toml @@ -0,0 +1,26 @@ +[build-system] +requires = ["hatchling"] +build-backend = "hatchling.build" + +[project] +name = "bgutil-ytdlp-pot-provider" +version = "0.2.1" +readme = {file = "README.md", content-type = "text/markdown"} +requires-python = ">=3.8" +keywords = ["yt-dlp", "yt-dlp-plugin", "yt-dlp-plugins-get-pot"] +authors = [ + { name = "Brainicism", email = "brainicism@gmail.com" }, +] +dependencies = [ + "yt-dlp-get-pot>=0.0.2", +] + +[tool.hatch.env.default] +installer = "uv" +path = ".venv" + +[tool.hatch.build.targets.wheel] +packages = ["yt_dlp_plugins"] + +[tool.hatch.metadata] +allow-direct-references = true