keymap-drawer icon indicating copy to clipboard operation
keymap-drawer copied to clipboard

Get help action to add to ZMK archive.

Open Eh2406 opened this issue 2 years ago • 6 comments

When using ZMK the workflow is that when you push it commit a github action starts that builds a zip download of the firmware. I would really love the key map image to be part of that zip archive. That way if I have multiple archives on my computer I easily tell what key map each set of firmware will use.

I don't know if this is possible, but it would be really cool if it was!

Eh2406 avatar Nov 14 '23 00:11 Eh2406

Hi, this is certainly achievable, but I am pretty sure it would require editing .github/workflows/build.yml in your config repo since that's the only way to write to the same output artifact that I know of.

Copying from the reusable workflow, you could probably append an extra step to the end so that it looks like this:

on: [push, pull_request, workflow_dispatch]

jobs:
  build:
    uses: zmkfirmware/zmk/.github/workflows/build-user-config.yml@main

  draw:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 1
          submodules: recursive

      - name: Install keymap-drawer (pypi)
        run: python3 -m pip install keymap-drawer

      - name: Draw keymaps
        run: |
          get_args() {
              local keyboard=$2
              eval set -- "$1"
              for arg; do
                  local key=${arg%%:*}
                  local val=${arg#*:}
                  if [ "$key" = "$keyboard" ]; then
                      echo "$val"
                      break
                  fi
              done
          }

          mkdir -p "${{ inputs.output_folder }}"

          config_path="${{ inputs.config_path }}"
          [ -e "$config_path" ] && config_arg=(-c "$config_path") || config_arg=()
          for keymap_file in ${{ inputs.keymap_patterns }}; do
              keyboard=$(basename -s .keymap "$keymap_file")
              echo "INFO: drawing for $keyboard"

              parse_args=$(get_args "${{ inputs.parse_args }}" "$keyboard")
              echo "INFO:   got extra parse args: $parse_args"
              draw_args=$(get_args "${{ inputs.draw_args }}" "$keyboard")
              echo "INFO:   got extra draw args: $draw_args"

              if [ -f "config/${keyboard}.json" ]; then
                  echo "INFO:   found config/${keyboard}.json";
                  draw_args+=" -j config/${keyboard}.json"
              fi

              keymap "${config_arg[@]}" parse -z "$keymap_file" $parse_args >"${{ inputs.output_folder }}/$keyboard.yaml" \
              && keymap "${config_arg[@]}" draw "${{ inputs.output_folder }}/$keyboard.yaml" $draw_args >"${{ inputs.output_folder }}/$keyboard.svg" \
              || echo "ERROR: parsing or drawing failed for $keyboard!"
          done

    - name: Upload drawings
      uses: actions/upload-artifact@v2
      with:
        name: ${{ inputs.archive_name }}
        path: ${{ inputs.output_folder }}/*

You will have to replace all ${{ inputs.* }} values except for ${{ inputs.archive_name }} above (which comes from the ZMK workflow) with your own parameter values.

Here we just called an upload-artifact step rather than a commit step in the workflow. Hopefully with the same filename as ZMK (${{ inputs.archive_name }} -> firmware) it will add on the files to the same output zip.

caksoylar avatar Nov 14 '23 05:11 caksoylar

Thank you so much for the quick and thoughtful response. I tried it and it works perfectly! How hard would it be to add this to the reusable workflow? Could this be another option documented in the readme?

If it would be difficult, or its not something you're interested in merging, feel free to close. And thank you.

Eh2406 avatar Nov 15 '23 15:11 Eh2406

I think it makes sense to add an alternative reusable workflow which integrates build with draw like above, but properly parametrized with the combination of parameters from both steps. I'll take a stab at that soon and close this issue if it happens.

caksoylar avatar Nov 16 '23 18:11 caksoylar

Hm, I believe that you cannot add files to an already existing artifact. You probably would have to pull and extract the ZMK artifact "firmware" then pull/extract the keymap-drawer artifact (see today's PR) merge them and then upload them overwriting the ZMK's firmware artifact. It would get a new id, but keep the name then. And you would need to chain both jobs together with 'need'. That's what my tinktering with this stuff in the last 2 days got me understanding...

michaelrommel avatar Feb 14 '24 21:02 michaelrommel

Hm, I believe that you cannot add files to an already existing artifact.

I think this may have been a Github change recently. The code caksoylar was working for my, but now gives warnings about updating actions/upload-artifact@v2 to v3

Eh2406 avatar Feb 15 '24 16:02 Eh2406

Sadly the new v3 version doesn't support adding files to existing artifacts. However there is a new merge action that can combine multiple artifacts; we switched to it for ZMK builds: https://github.com/zmkfirmware/zmk/pull/2141

caksoylar avatar Feb 15 '24 22:02 caksoylar