You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
8.3 KiB

  1. # Building QMK with GitHub Userspace
  2. This is an intermediate QMK tutorial to setup an out-of-tree build environment with a personal GitHub repository. It avoids using a fork of the QMK firmware to store and build your keymap within its source tree. Keymap files will instead be stored in your own personal GitHub repository, in [Userspace](https://docs.qmk.fm/#/feature_userspace) format, and built with an action workflow. Unlike the [default tutorial](https://docs.qmk.fm/#/newbs), this guide requires some familiarity with using Git.
  3. ?> **Is This Guide For Me?**<br>
  4. This is a lean setup to avoid space-consuming local build environment in your computer. Troubleshooting compile-time errors will be slower with commit uploads to GitHub for the compiler workflow.
  5. ## Prerequisites
  6. The following are required to get started:
  7. * [GitHub Account](https://github.com/new)
  8. * A working account is required to setup and host your repository for GitHub Actions to build QMK firmware.
  9. * [Text editor](newbs_learn_more_resources.md#text-editor-resources)
  10. * You’ll need a program that can edit and save plain text files. The default editor that comes with many OS's does not save plain text files, so you'll need to make sure that whatever editor you chose does.
  11. * [Toolbox](https://github.com/qmk/qmk_toolbox)
  12. * A graphical program for Windows and macOS that allows you to both program and debug your custom keyboard.
  13. ## Environment Setup
  14. ?> If you are familiar with using [github.dev](https://docs.github.com/en/codespaces/the-githubdev-web-based-editor), you can skip to [step 2](#_2-create-github-repository) and commit the code files that follows directly on GitHub using the web-based VSCode editor.
  15. ### 1. Install Git
  16. A working Git client is required for your local operating system to commit and push changes to GitHub.
  17. <!-- tabs:start -->
  18. ### ** Windows **
  19. QMK maintains a bundle of MSYS2, the CLI and all necessary dependencies including Git. Install [QMK MSYS](https://msys.qmk.fm/) with the latest release [here](https://github.com/qmk/qmk_distro_msys/releases/latest). Git will be part of the bundle.
  20. ### ** macOS **
  21. Install Homebrew following the instructions on https://brew.sh. Git will be part of the bundle.
  22. ### ** Linux/WSL **
  23. It's very likely that you already have Git installed. If not, use one of the following commands:
  24. * Debian / Ubuntu / Devuan: `sudo apt install -y git`
  25. * Fedora / Red Hat / CentOS: `sudo yum -y install git`
  26. * Arch / Manjaro: `sudo pacman --needed --noconfirm -S git`
  27. * Void: `sudo xbps-install -y git`
  28. * Solus: `sudo eopkg -y install git`
  29. * Sabayon: `sudo equo install dev-vcs/git`
  30. * Gentoo: `sudo emerge dev-vcs/git`
  31. <!-- tabs:end -->
  32. ### 2. GitHub authentication
  33. If your GitHub account is not configured for [authenticated Git operations](https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/), you will need to setup at least one of the following:
  34. * [Personal access token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token)
  35. * [Connecting with SSH](https://docs.github.com/en/authentication/connecting-to-github-with-ssh)
  36. ### 3. Create a repository
  37. You will need a personal GitHub repository to host your QMK code. Follow [this guide](https://docs.github.com/en/get-started/quickstart/create-a-repo#create-a-repository) to create one named `qmk_keymap`. Do not proceed to commit any files just yet.
  38. ## Initial Code Commit
  39. ### Create template files
  40. Run the following commands in your computer to create a folder with a few template files:
  41. ```
  42. mkdir -p ~/qmk_keymap/.github/workflows
  43. touch ~/qmk_keymap/.github/workflows/build.yml
  44. touch ~/qmk_keymap/config.h
  45. echo "SRC += source.c" > ~/qmk_keymap/rules.mk
  46. echo "#include QMK_KEYBOARD_H" > ~/qmk_keymap/source.c
  47. ```
  48. ?> For Windows user running MSYS, those commands will create the folder `qmk_keymap/` and its content in the `C:\Users\<windows_username>\qmk_keymap\` path location.
  49. ### Add a JSON keymap
  50. Visit the [QMK Configurator](https://config.qmk.fm/#/) to create a keymap file:
  51. 1. Select your keyboard from the drop-down list (and choose a layout if required).
  52. 2. Use your GitHub username for the **Keymap Name** field.
  53. 3. Customise the key layout according to your preference.
  54. 4. Select download next to **KEYMAP.JSON** and save the JSON file into the `~/qmk_keymap/` folder.
  55. !> **Important:** Make sure that the GitHub username you use in step 2 is correct. If it is not, the build process will fail to locate your files in the right folder.
  56. ### Add a GitHub Action workflow
  57. Open the file `~/qmk_keymap/.github/workflows/build.yml` with your favorite [text editor](newbs_learn_more_resources.md#text-editor-resources), paste the following workflow content, and save it:
  58. ```yml
  59. name: Build QMK firmware
  60. on: [push, workflow_dispatch]
  61. jobs:
  62. build:
  63. runs-on: ubuntu-latest
  64. container: ghcr.io/qmk/qmk_cli
  65. strategy:
  66. fail-fast: false
  67. matrix:
  68. # List of keymap json files to build
  69. file:
  70. - username.json
  71. # End of json file list
  72. steps:
  73. - name: Disable git safe directory checks
  74. run : git config --global --add safe.directory '*'
  75. - name: Checkout QMK
  76. uses: actions/checkout@v3
  77. with:
  78. repository: qmk/qmk_firmware
  79. submodules: recursive
  80. - name: Checkout userspace
  81. uses: actions/checkout@v3
  82. with:
  83. path: users/${{ github.actor }}
  84. - name: Build firmware
  85. run: qmk compile "users/${{ github.actor }}/${{ matrix.file }}"
  86. - name: Archive firmware
  87. uses: actions/upload-artifact@v3
  88. continue-on-error: true
  89. with:
  90. name: ${{ matrix.file }}_${{ github.actor }}
  91. path: |
  92. *.hex
  93. *.bin
  94. *.uf2
  95. ```
  96. Replace `username.json` with the JSON file name that was downloaded from [QMK Configurator](https://config.qmk.fm/#/) in the previous step.
  97. !> Do note that the `build.yml` file requires ***proper indentation*** for every line. Incorrect spacing will trigger workflow syntax errors.
  98. ### Commit files to GitHub
  99. If you have completed all steps correctly, the folder `qmk_keymap/` will contain the following files:
  100. ```
  101. ├── .github
  102. │   └── workflows
  103. │   └── build.yml
  104. ├── rules.mk
  105. ├── config.h
  106. ├── source.c
  107. └── username.json
  108. ```
  109. To commit and push them into GitHub, run the following commands (replacing `gh-username` with your GitHub user name):
  110. ```
  111. cd ~/qmk_keymap
  112. git init
  113. git add -A
  114. git commit -m "Initial QMK keymap commit"
  115. git branch -M main
  116. git remote add origin https://github.com/gh-username/qmk_keymap.git
  117. git push -u origin main
  118. ```
  119. ?> Use your GitHub personal access token at the password prompt. If you have setup SSH access, replace `https://github.com/gh-username/qmk_keymap.git` with `git@github.com:gh-username/qmk_keymap.git` in the remote origin command above.
  120. ### Review workflow output
  121. Files committed to GitHub in the previous step will automatically trigger the workflow to build the JSON file listed in `build.yml`. To review its output:
  122. 1. Visit your "**qmk_keymap**" repository page on [GitHub](https://github.com/).
  123. 2. Select **Actions** tab to display the "**Build QMK Firmware**" workflow.
  124. 3. Select that workflow to display its run from the last commit.
  125. 4. Successfully compiled firmware will be under the "**Artifacts**" section.
  126. 5. If there are build errors, review the job log for details.
  127. Download and flash the firmware file into your keyboard using [QMK Toolbox](https://docs.qmk.fm/#/newbs_flashing?id=flashing-your-keyboard-with-qmk-toolbox).
  128. ## Customising your keymap
  129. This setup and workflow relies on the QMK [Userspace](https://docs.qmk.fm/#/feature_userspace) feature. The build process will copy the QMK source codes and clone your repository into its `users/` folder in a container. You must adhere to the following guidelines when customising your keymaps:
  130. * Keymap layout files must be retained in JSON format and cannot be converted to `keymap.c`.
  131. * User callback and functions (e.g. `process_record_user()`) can be placed in the `source.c` file.
  132. * Multiple keymap JSON files can be built in the same workflow. List them under `matrix.file:`, e.g.:
  133. ```yml
  134. file:
  135. - planck.json
  136. - crkbd.json
  137. ```
  138. * Code changes will require Git commit into GitHub to trigger the build workflow.
  139. ?> See [GitHub Actions guide](https://docs.github.com/en/actions/learn-github-actions) to learn more about development workflow.