Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: register releases to chocolatey #158

Closed
kbilsted opened this issue Jul 15, 2024 · 12 comments
Closed

feature: register releases to chocolatey #158

kbilsted opened this issue Jul 15, 2024 · 12 comments
Labels
help wanted Extra attention is needed

Comments

@kbilsted
Copy link
Contributor

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
Copy link
Owner

houmain commented Jul 15, 2024

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?

@houmain houmain changed the title feature: register releases to chocolatery feature: register releases to chocolatey Jul 15, 2024
@houmain houmain added the help wanted Extra attention is needed label Jul 15, 2024
@kbilsted
Copy link
Contributor Author

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

@kbilsted
Copy link
Contributor Author

kbilsted commented Jul 16, 2024

wow that was confusing.. found it by

@houmain
Copy link
Owner

houmain commented Jul 17, 2024

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
Copy link
Contributor Author

kbilsted commented Jul 17, 2024

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
Copy link
Owner

houmain commented Jul 17, 2024

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

@kbilsted
Copy link
Contributor Author

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:
- '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

oh btw this is 100% straight chatptg material ;)

@ristomatti
Copy link
Contributor

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/ 😉

@kbilsted
Copy link
Contributor Author

@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

This comment was marked as off-topic.

@ristomatti

This comment was marked as off-topic.

@houmain
Copy link
Owner

houmain commented Jul 29, 2024

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!

@houmain houmain closed this as completed Jul 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants