houmain / keymapper

A cross-platform context-aware key remapper.
GNU General Public License v3.0
280 stars 23 forks source link

feature: register releases to chocolatey #158

Closed kbilsted closed 3 weeks ago

kbilsted commented 1 month ago

on windows chocolatery is a very convenient way to install and update software. similar to package managers on linux.

it would be great to have releases pushed to chocolatery https://docs.chocolatey.org/en-us/create/create-packages/

houmain commented 1 month ago

On Windows one can already install and update keymapper using winget install keymapper. I will add that to the documentation.

Do you have experience with chocolatey package creation? I just had a quick look but did not find out how to get an API key one needs to push to the community feed. Maybe someone else but me is willing to maintain it?

kbilsted commented 1 month ago

winget i havent used but i think it will suffice for my needs. Will try it on next release

kbilsted commented 1 month ago

wow that was confusing.. found it by

houmain commented 1 month ago

Thanks, ok, then I will maintain it myself. There should really be a tool for automating all these deployments... It is already under review: https://community.chocolatey.org/packages/keymapper/4.4.4

kbilsted commented 1 month ago

I was thinking you wanted to publish through a github pipeline script

Publishing a .msi package for Windows to Chocolatey from a GitHub Action involves several steps. Here’s a detailed guide to achieve this:

Prerequisites Chocolatey Account: You need an account on the Chocolatey Community Repository. API Key: Get your API key from the Chocolatey website. GitHub Repository: Ensure your project is hosted on GitHub and has a .msi package that you want to publish. Step-by-Step Guide

  1. Prepare the Chocolatey Package Create the necessary Chocolatey package files (.nuspec, tools, etc.).

Ensure you have the choco CLI tool installed on your local machine for testing.

Test your package locally using choco pack and choco install commands.

  1. Add Secrets to GitHub Go to your GitHub repository. Navigate to Settings > Secrets and variables > Actions. Add a new repository secret: CHOCOLATEY_API_KEY: Your Chocolatey API key.
  2. Create a GitHub Action Workflow Create a GitHub Actions workflow file in your repository (e.g., .github/workflows/publish.yml).
name: Publish Chocolatey Package

on:
  push:
    tags:
      - 'v*' # This triggers the workflow on version tags

jobs:
  publish:
    runs-on: windows-latest

    steps:
    - name: Checkout repository
      uses: actions/checkout@v3

    - name: Set up Chocolatey
      run: |
        Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

    - name: Pack Chocolatey Package
      run: choco pack .\path\to\your\package.nuspec

    - name: Push to Chocolatey
      env:
        CHOCO_API_KEY: ${{ secrets.CHOCOLATEY_API_KEY }}
      run: choco push .\your-package.1.0.0.nupkg --source https://push.chocolatey.org/ --api-key $env:CHOCO_API_KEY

Explanation Trigger on Tag: The workflow is triggered when a new version tag (e.g., v1.0.0) is pushed to the repository.

Checkout Repository: Uses actions/checkout@v3 to check out the repository code.

Set up Chocolatey: Installs Chocolatey on the runner.

Pack Chocolatey Package: Packs the .nuspec file into a .nupkg.

Push to Chocolatey: Pushes the .nupkg file to the Chocolatey repository using the API key stored in GitHub Secrets.

Additional Tips

Versioning: Ensure your .nuspec file version matches the tag name to avoid version conflicts.

Testing: Before pushing to Chocolatey, thoroughly test your .msi and Chocolatey package locally.

Error Handling: Add additional error handling and notifications in the workflow for better feedback. By following these steps, you can automate the publishing of your .msi package to Chocolatey using GitHub Actions. This setup ensures that your package is consistently and reliably published whenever you push a new version tag.

houmain commented 1 month ago

Thanks! Unfortunately this guide does not show how to patch the version and checksum inside keymapper.nuspec and chocolateyinstall.ps1 first.

kbilsted commented 1 month ago

To patch the version and checksum inside keymapper.nuspec and chocolateyinstall.ps1 files before packaging and pushing the Chocolatey package, you can use a script within your GitHub Actions workflow. This script will dynamically update these files with the correct version and checksum values.

Detailed Steps Determine the Version and Calculate Checksum: Extract the version from the Git tag and calculate the checksum of the .msi file. Patch the .nuspec and chocolateyinstall.ps1 Files: Use PowerShell to update these files with the new version and checksum. Pack and Push the Chocolatey Package: Continue with packaging and pushing the package. Here is a more comprehensive GitHub Actions workflow:

name: Publish Chocolatey Package

on:
  push:
    tags:
      - 'v*' # This triggers the workflow on version tags

jobs:
  publish:
    runs-on: windows-latest

    steps:
    - name: Checkout repository
      uses: actions/checkout@v3

    - name: Set up Chocolatey
      run: |
        Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

    - name: Extract Version from Tag
      id: extract_version
      run: echo "::set-output name=VERSION::${GITHUB_REF#refs/tags/v}"

    - name: Calculate Checksum
      id: calculate_checksum
      run: |
        $filePath = ".\path\to\your\installer.msi"
        $checksum = Get-FileHash $filePath -Algorithm sha256 | Select-Object -ExpandProperty Hash
        echo "::set-output name=CHECKSUM::$checksum"

    - name: Patch .nuspec and chocolateyinstall.ps1
      run: |
        $version = "${{ steps.extract_version.outputs.VERSION }}"
        $checksum = "${{ steps.calculate_checksum.outputs.CHECKSUM }}"

        # Update .nuspec file
        (Get-Content .\path\to\keymapper.nuspec) -replace '<version>.*</version>', "<version>$version</version>" | Set-Content .\path\to\keymapper.nuspec

        # Update chocolateyinstall.ps1 file
        (Get-Content .\path\to\tools\chocolateyinstall.ps1) -replace 'url64bit.*', "url64bit = 'https://path/to/your/installer.msi'" -replace 'checksum64bit.*', "checksum64bit = '$checksum'" | Set-Content .\path\to\tools\chocolateyinstall.ps1

    - name: Pack Chocolatey Package
      run: choco pack .\path\to\your\keymapper.nuspec

    - name: Push to Chocolatey
      env:
        CHOCO_API_KEY: ${{ secrets.CHOCOLATEY_API_KEY }}
      run: choco push .\keymapper.${{ steps.extract_version.outputs.VERSION }}.nupkg --source https://push.chocolatey.org/ --api-key $env:CHOCO_API_KEY

name: Publish Chocolatey Package

on: push: tags:

jobs: publish: runs-on: windows-latest

steps:
- name: Checkout repository
  uses: actions/checkout@v3

- name: Set up Chocolatey
  run: |
    Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

- name: Extract Version from Tag
  id: extract_version
  run: echo "::set-output name=VERSION::${GITHUB_REF#refs/tags/v}"

- name: Calculate Checksum
  id: calculate_checksum
  run: |
    $filePath = ".\path\to\your\installer.msi"
    $checksum = Get-FileHash $filePath -Algorithm sha256 | Select-Object -ExpandProperty Hash
    echo "::set-output name=CHECKSUM::$checksum"

- name: Patch .nuspec and chocolateyinstall.ps1
  run: |
    $version = "${{ steps.extract_version.outputs.VERSION }}"
    $checksum = "${{ steps.calculate_checksum.outputs.CHECKSUM }}"

    # Update .nuspec file
    (Get-Content .\path\to\keymapper.nuspec) -replace '<version>.*</version>', "<version>$version</version>" | Set-Content .\path\to\keymapper.nuspec

    # Update chocolateyinstall.ps1 file
    (Get-Content .\path\to\tools\chocolateyinstall.ps1) -replace 'url64bit.*', "url64bit = 'https://path/to/your/installer.msi'" -replace 'checksum64bit.*', "checksum64bit = '$checksum'" | Set-Content .\path\to\tools\chocolateyinstall.ps1

- name: Pack Chocolatey Package
  run: choco pack .\path\to\your\keymapper.nuspec

- name: Push to Chocolatey
  env:
    CHOCO_API_KEY: ${{ secrets.CHOCOLATEY_API_KEY }}
  run: choco push .\keymapper.${{ steps.extract_version.outputs.VERSION }}.nupkg --source https://push.chocolatey.org/ --api-key $env:CHOCO_API_KEY

oh btw this is 100% straight chatptg material ;)

ristomatti commented 1 month ago

oh btw this is 100% straight chatptg material ;)

I knew it already after reading the highlighted phrase:

Publishing a .msi package for Windows to Chocolatey from a GitHub Action involves several steps. Here’s a detailed guide to achieve this:

P.S. Before blindly posting GPT answers again, you might want to read this: https://daniel.haxx.se/blog/2024/01/02/the-i-in-llm-stands-for-intelligence/ :wink:

kbilsted commented 1 month ago

@ristomatti already read it. currently im testing it out. like it really helped me with some error messages the other day and it can do some python coding - and stupid almost of-the-shelve github actions I would think it would do fairly well among me (us?) who dont know github actions.

ristomatti commented 1 month ago

I agree LLM's can be a huge time saver for many use cases but extremely unlikely to get anything this long/specific right without several iterations and at least some hand holding/guidance. I've been experimenting with them quite a bit myself. Some examples of things I've found them to be a big time saver for me:

This is way off topic, my apologies!

P.S. To avoid being completely off topic, this mostly written by GPT-4 Turbo by using keymapperctl --help as the spec: https://github.com/houmain/keymapper/discussions/139 :grin:

P.P.S. It can be more useful to provide the actual conversation with the ability to continue from there. This can be done with Phind for example: https://www.phind.com/search?cache=guvvejiwhuvkeb5m44kmzedq

ristomatti commented 1 month ago

There should really be a tool for automating all these deployments...

This got me thinking, there's got to be one already but the closest I could find were these (no choco to be found though and not winget either):

I have to say though, the fpm project seems to have guiding principles you don't see every day:

fpm, as a project, exists to help you build packages, therefore:

  • If fpm is not helping you make packages easily, then there is a bug in fpm.
  • If you are having a bad time with fpm, then there is a bug in fpm.
  • If the documentation is confusing, then this is a bug in fpm.

Edit: Yet another only vaguely related but interesting tool found while searching: https://github.com/nektos/act

houmain commented 3 weeks ago

It is now available: https://community.chocolatey.org/packages/keymapper This update action looks very complicated but I will have a closer look some day, thanks!