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.

189 lines
8.0 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. ### Add a GitHub Action workflow
  56. 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:
  57. ```yml
  58. name: Build QMK firmware
  59. on: [push, workflow_dispatch]
  60. jobs:
  61. build:
  62. runs-on: ubuntu-latest
  63. container: qmkfm/qmk_cli
  64. strategy:
  65. fail-fast: false
  66. matrix:
  67. # List of keymap json files to build
  68. file:
  69. - username.json
  70. # End of json file list
  71. steps:
  72. - name: Checkout QMK
  73. uses: actions/checkout@v3
  74. with:
  75. repository: qmk/qmk_firmware
  76. submodules: recursive
  77. - name: Checkout userspace
  78. uses: actions/checkout@v3
  79. with:
  80. path: users/${{ github.actor }}
  81. - name: Build firmware
  82. run: qmk compile "users/${{ github.actor }}/${{ matrix.file }}"
  83. - name: Archive firmware
  84. uses: actions/upload-artifact@v3
  85. continue-on-error: true
  86. with:
  87. name: ${{ matrix.file }}_${{ github.actor }}
  88. path: |
  89. *.hex
  90. *.bin
  91. *.uf2
  92. ```
  93. Replace `username.json` with the JSON file name that was downloaded from [QMK Configurator](https://config.qmk.fm/#/) in the previous step.
  94. !> Do note that the `build.yml` file requires ***proper indentation*** for every line. Incorrect spacing will trigger workflow syntax errors.
  95. ### Commit files to GitHub
  96. If you have completed all steps correctly, the folder `qmk_keymap/` will contain the following files:
  97. ```
  98. |-- .github
  99. | `-- workflows
  100. | `-- build.yml
  101. |-- rules.mk
  102. |-- config.h
  103. |-- source.c
  104. |-- username.json
  105. ```
  106. To commit and push them into GitHub, run the following commands (replacing `gh-username` with your GitHub user name):
  107. ```
  108. cd ~/qmk_keymap
  109. git init
  110. git add -A
  111. git commit -m "Initial QMK keymap commit"
  112. git branch -M main
  113. git remote add origin https://github.com/gh-username/qmk_keymap.git
  114. git push -u origin main
  115. ```
  116. ?> 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.
  117. ### Review workflow output
  118. 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:
  119. 1. Visit your "**qmk_keymap**" repository page on [GitHub](https://github.com/).
  120. 2. Select **Actions** tab to display the "**Build QMK Firmware**" workflow.
  121. 3. Select that workflow to display its run from the last commit.
  122. 4. Successfully compiled firmware will be under the "**Artifacts**" section.
  123. 5. If there are build errors, review the job log for details.
  124. 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).
  125. ## Customising your keymap
  126. 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:
  127. * Keymap layout files must be retained in JSON format and cannot be converted to `keymap.c`.
  128. * User callback and functions (e.g. `process_record_user()`) can be placed in the `source.c` file.
  129. * Multiple keymap JSON files can be built in the same workflow. List them under `matrix.file:`, e.g.:
  130. ```yml
  131. file:
  132. - planck.json
  133. - crkbd.json
  134. ```
  135. * Code changes will require Git commit into GitHub to trigger the build workflow.
  136. ?> See [GitHub Actions guide](https://docs.github.com/en/actions/learn-github-actions) to learn more about development workflow.