1*61c4878aSAndroid Build Coastguard Worker.. _docs-github-actions: 2*61c4878aSAndroid Build Coastguard Worker 3*61c4878aSAndroid Build Coastguard Worker=========================================== 4*61c4878aSAndroid Build Coastguard WorkerSet up GitHub Actions for a Pigweed project 5*61c4878aSAndroid Build Coastguard Worker=========================================== 6*61c4878aSAndroid Build Coastguard Worker.. _GitHub Actions: https://docs.github.com/en/actions 7*61c4878aSAndroid Build Coastguard Worker 8*61c4878aSAndroid Build Coastguard WorkerThis tutorial shows you how to set up `GitHub Actions`_ to build and test your 9*61c4878aSAndroid Build Coastguard WorkerBazel-based Pigweed project. You'll learn how to set up both presubmit and 10*61c4878aSAndroid Build Coastguard Workerpostsubmit Actions. 11*61c4878aSAndroid Build Coastguard Worker 12*61c4878aSAndroid Build Coastguard Worker.. _docs-github-actions-examples: 13*61c4878aSAndroid Build Coastguard Worker 14*61c4878aSAndroid Build Coastguard Worker-------- 15*61c4878aSAndroid Build Coastguard WorkerExamples 16*61c4878aSAndroid Build Coastguard Worker-------- 17*61c4878aSAndroid Build Coastguard WorkerPigweed's :ref:`Bazel quickstart repo <docs-get-started-bazel>` demonstrates 18*61c4878aSAndroid Build Coastguard WorkerGitHub Actions integration: 19*61c4878aSAndroid Build Coastguard Worker 20*61c4878aSAndroid Build Coastguard Worker* `//.github/workflows/presubmit.yaml <https://pigweed.googlesource.com/pigweed/quickstart/bazel/+/refs/heads/main/.github/workflows/presubmit.yaml>`_ 21*61c4878aSAndroid Build Coastguard Worker* `//.github/workflows/postsubmit.yaml <https://pigweed.googlesource.com/pigweed/quickstart/bazel/+/refs/heads/main/.github/workflows/postsubmit.yaml>`_ 22*61c4878aSAndroid Build Coastguard Worker 23*61c4878aSAndroid Build Coastguard Worker.. _docs-github-actions-enable: 24*61c4878aSAndroid Build Coastguard Worker 25*61c4878aSAndroid Build Coastguard Worker-------------- 26*61c4878aSAndroid Build Coastguard WorkerEnable Actions 27*61c4878aSAndroid Build Coastguard Worker-------------- 28*61c4878aSAndroid Build Coastguard Worker.. _Managing GitHub Actions settings for a repository: https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/enabling-features-for-your-repository/managing-github-actions-settings-for-a-repository 29*61c4878aSAndroid Build Coastguard Worker 30*61c4878aSAndroid Build Coastguard WorkerMake sure that Actions are enabled for your repo. See 31*61c4878aSAndroid Build Coastguard Worker`Managing GitHub Actions settings for a repository`_. 32*61c4878aSAndroid Build Coastguard Worker 33*61c4878aSAndroid Build Coastguard Worker----------------------------------------- 34*61c4878aSAndroid Build Coastguard WorkerCreate a .bazelversion file for your repo 35*61c4878aSAndroid Build Coastguard Worker----------------------------------------- 36*61c4878aSAndroid Build Coastguard Worker.. _.bazelversion: https://github.com/bazelbuild/bazelisk?tab=readme-ov-file#how-does-bazelisk-know-which-bazel-version-to-run 37*61c4878aSAndroid Build Coastguard Worker 38*61c4878aSAndroid Build Coastguard WorkerMake sure that `.bazelversion`_ exists in your repo. This file specifies what 39*61c4878aSAndroid Build Coastguard Workerversion of Bazel the Actions should use. 40*61c4878aSAndroid Build Coastguard Worker 41*61c4878aSAndroid Build Coastguard Worker.. _docs-github-actions-presubmit: 42*61c4878aSAndroid Build Coastguard Worker 43*61c4878aSAndroid Build Coastguard Worker------------------------- 44*61c4878aSAndroid Build Coastguard WorkerCreate a presubmit Action 45*61c4878aSAndroid Build Coastguard Worker------------------------- 46*61c4878aSAndroid Build Coastguard WorkerA presubmit Action runs when a pull request is sent. Creating a presubmit 47*61c4878aSAndroid Build Coastguard WorkerAction that makes sure pull requests build and pass tests involves four steps: 48*61c4878aSAndroid Build Coastguard Worker 49*61c4878aSAndroid Build Coastguard Worker#. Checking out your repo's code. 50*61c4878aSAndroid Build Coastguard Worker 51*61c4878aSAndroid Build Coastguard Worker#. Installing Bazel. 52*61c4878aSAndroid Build Coastguard Worker 53*61c4878aSAndroid Build Coastguard Worker#. Building your code. 54*61c4878aSAndroid Build Coastguard Worker 55*61c4878aSAndroid Build Coastguard Worker#. Testing your code. 56*61c4878aSAndroid Build Coastguard Worker 57*61c4878aSAndroid Build Coastguard WorkerThe first two steps are handled by community-managed extensions. The last 58*61c4878aSAndroid Build Coastguard Workertwo steps require just a few lines of code. 59*61c4878aSAndroid Build Coastguard Worker 60*61c4878aSAndroid Build Coastguard Worker#. From the root of your repository, create ``.github/workflows/presubmit.yaml``. 61*61c4878aSAndroid Build Coastguard Worker The path to the file must be ``.github/workflows`` but you can name the file 62*61c4878aSAndroid Build Coastguard Worker whatever you want. 63*61c4878aSAndroid Build Coastguard Worker 64*61c4878aSAndroid Build Coastguard Worker#. Put the following YAML into the file: 65*61c4878aSAndroid Build Coastguard Worker 66*61c4878aSAndroid Build Coastguard Worker .. code-block:: yaml 67*61c4878aSAndroid Build Coastguard Worker 68*61c4878aSAndroid Build Coastguard Worker name: presubmit 69*61c4878aSAndroid Build Coastguard Worker run-name: presubmit run triggered by ${{ github.actor }} 70*61c4878aSAndroid Build Coastguard Worker 71*61c4878aSAndroid Build Coastguard Worker on: 72*61c4878aSAndroid Build Coastguard Worker pull_request: 73*61c4878aSAndroid Build Coastguard Worker types: [opened, synchronize, reopened] 74*61c4878aSAndroid Build Coastguard Worker 75*61c4878aSAndroid Build Coastguard Worker jobs: 76*61c4878aSAndroid Build Coastguard Worker bazel-build-test: 77*61c4878aSAndroid Build Coastguard Worker runs-on: ubuntu-latest 78*61c4878aSAndroid Build Coastguard Worker steps: 79*61c4878aSAndroid Build Coastguard Worker - name: Checkout 80*61c4878aSAndroid Build Coastguard Worker # Check out this repo's code. 81*61c4878aSAndroid Build Coastguard Worker # https://github.com/actions/checkout 82*61c4878aSAndroid Build Coastguard Worker uses: actions/checkout@v4 83*61c4878aSAndroid Build Coastguard Worker with: 84*61c4878aSAndroid Build Coastguard Worker fetch-depth: 0 85*61c4878aSAndroid Build Coastguard Worker submodules: recursive 86*61c4878aSAndroid Build Coastguard Worker - name: Get Bazel 87*61c4878aSAndroid Build Coastguard Worker # Install Bazel. 88*61c4878aSAndroid Build Coastguard Worker # https://github.com/bazel-contrib/setup-bazel 89*61c4878aSAndroid Build Coastguard Worker uses: bazel-contrib/setup-bazel@0.8.1 90*61c4878aSAndroid Build Coastguard Worker with: 91*61c4878aSAndroid Build Coastguard Worker # Avoid downloading Bazel every time. 92*61c4878aSAndroid Build Coastguard Worker bazelisk-cache: true 93*61c4878aSAndroid Build Coastguard Worker # Store build cache per workflow. 94*61c4878aSAndroid Build Coastguard Worker disk-cache: ${{ github.workflow }} 95*61c4878aSAndroid Build Coastguard Worker # Share repository cache between workflows. 96*61c4878aSAndroid Build Coastguard Worker repository-cache: true 97*61c4878aSAndroid Build Coastguard Worker - name: Bazel Build 98*61c4878aSAndroid Build Coastguard Worker # Always use bazelisk rather than bazel to 99*61c4878aSAndroid Build Coastguard Worker # guarantee that the correct version of Bazel 100*61c4878aSAndroid Build Coastguard Worker # (sourced from .bazelversion) is used. 101*61c4878aSAndroid Build Coastguard Worker run: bazelisk build ... 102*61c4878aSAndroid Build Coastguard Worker - name: Bazel Test 103*61c4878aSAndroid Build Coastguard Worker run: bazelisk test ... 104*61c4878aSAndroid Build Coastguard Worker 105*61c4878aSAndroid Build Coastguard Worker#. Commit the file. 106*61c4878aSAndroid Build Coastguard Worker 107*61c4878aSAndroid Build Coastguard WorkerThe Action runs whenever a pull request is opened, updated, or 108*61c4878aSAndroid Build Coastguard Workerreopened. 109*61c4878aSAndroid Build Coastguard Worker 110*61c4878aSAndroid Build Coastguard Worker.. _Bazelisk: https://bazel.build/install/bazelisk 111*61c4878aSAndroid Build Coastguard Worker 112*61c4878aSAndroid Build Coastguard Worker.. note:: 113*61c4878aSAndroid Build Coastguard Worker 114*61c4878aSAndroid Build Coastguard Worker In general, Pigweed recommends always launching Bazel through 115*61c4878aSAndroid Build Coastguard Worker the ``bazelisk`` command rather than the ``bazel`` command. 116*61c4878aSAndroid Build Coastguard Worker `Bazelisk`_ guarantees that you're always running the correct 117*61c4878aSAndroid Build Coastguard Worker version of Bazel for your project, as defined in your project's 118*61c4878aSAndroid Build Coastguard Worker ``.bazelversion`` file. It would technically be OK to use the 119*61c4878aSAndroid Build Coastguard Worker ``bazel`` command in your GitHub Actions code because the 120*61c4878aSAndroid Build Coastguard Worker ``bazel-contrib/setup-bazel`` extension also makes sure to launch 121*61c4878aSAndroid Build Coastguard Worker the correct version of Bazel based on what's defined in ``.bazelversion``, 122*61c4878aSAndroid Build Coastguard Worker but in practice Pigweed finds it safer to just use ``bazelisk`` everywhere. 123*61c4878aSAndroid Build Coastguard Worker 124*61c4878aSAndroid Build Coastguard Worker.. _docs-github-actions-postsubmit: 125*61c4878aSAndroid Build Coastguard Worker 126*61c4878aSAndroid Build Coastguard Worker-------------------------- 127*61c4878aSAndroid Build Coastguard WorkerCreate a postsubmit Action 128*61c4878aSAndroid Build Coastguard Worker-------------------------- 129*61c4878aSAndroid Build Coastguard WorkerA postsubmit Action runs after a pull request has been merged. 130*61c4878aSAndroid Build Coastguard WorkerThe process for creating a postsubmit Action that builds and tests your code 131*61c4878aSAndroid Build Coastguard Workerwhen a new commit is pushed is almost identical to 132*61c4878aSAndroid Build Coastguard Worker:ref:`the presubmit Action setup <docs-github-actions-presubmit>`. The 133*61c4878aSAndroid Build Coastguard Workeronly thing that changes is the ``on`` field in the YAML: 134*61c4878aSAndroid Build Coastguard Worker 135*61c4878aSAndroid Build Coastguard Worker.. code-block:: yaml 136*61c4878aSAndroid Build Coastguard Worker 137*61c4878aSAndroid Build Coastguard Worker name: postsubmit 138*61c4878aSAndroid Build Coastguard Worker run-name: postsubmit run 139*61c4878aSAndroid Build Coastguard Worker 140*61c4878aSAndroid Build Coastguard Worker on: 141*61c4878aSAndroid Build Coastguard Worker push 142*61c4878aSAndroid Build Coastguard Worker 143*61c4878aSAndroid Build Coastguard Worker jobs: 144*61c4878aSAndroid Build Coastguard Worker bazel-build-test: 145*61c4878aSAndroid Build Coastguard Worker runs-on: ubuntu-latest 146*61c4878aSAndroid Build Coastguard Worker steps: 147*61c4878aSAndroid Build Coastguard Worker - name: Checkout 148*61c4878aSAndroid Build Coastguard Worker # Check out this repo's code. 149*61c4878aSAndroid Build Coastguard Worker # https://github.com/actions/checkout 150*61c4878aSAndroid Build Coastguard Worker uses: actions/checkout@v4 151*61c4878aSAndroid Build Coastguard Worker with: 152*61c4878aSAndroid Build Coastguard Worker fetch-depth: 0 153*61c4878aSAndroid Build Coastguard Worker submodules: recursive 154*61c4878aSAndroid Build Coastguard Worker - name: Get Bazel 155*61c4878aSAndroid Build Coastguard Worker # Install Bazel. 156*61c4878aSAndroid Build Coastguard Worker # https://github.com/bazel-contrib/setup-bazel 157*61c4878aSAndroid Build Coastguard Worker uses: bazel-contrib/setup-bazel@0.8.1 158*61c4878aSAndroid Build Coastguard Worker with: 159*61c4878aSAndroid Build Coastguard Worker # Avoid downloading Bazel every time. 160*61c4878aSAndroid Build Coastguard Worker bazelisk-cache: true 161*61c4878aSAndroid Build Coastguard Worker # Store build cache per workflow. 162*61c4878aSAndroid Build Coastguard Worker disk-cache: ${{ github.workflow }} 163*61c4878aSAndroid Build Coastguard Worker # Share repository cache between workflows. 164*61c4878aSAndroid Build Coastguard Worker repository-cache: true 165*61c4878aSAndroid Build Coastguard Worker - name: Bazel Build 166*61c4878aSAndroid Build Coastguard Worker # Always use bazelisk rather than bazel to 167*61c4878aSAndroid Build Coastguard Worker # guarantee that the correct version of Bazel 168*61c4878aSAndroid Build Coastguard Worker # (sourced from .bazelversion) is used. 169*61c4878aSAndroid Build Coastguard Worker run: bazelisk build ... 170*61c4878aSAndroid Build Coastguard Worker - name: Bazel Test 171*61c4878aSAndroid Build Coastguard Worker run: bazelisk test ... 172*61c4878aSAndroid Build Coastguard Worker 173*61c4878aSAndroid Build Coastguard Worker.. _docs-github-actions-linter: 174*61c4878aSAndroid Build Coastguard Worker 175*61c4878aSAndroid Build Coastguard Worker-------------------------------------------------------------- 176*61c4878aSAndroid Build Coastguard WorkerCreate a linter Action that uses pw_presubmit and pw_env_setup 177*61c4878aSAndroid Build Coastguard Worker-------------------------------------------------------------- 178*61c4878aSAndroid Build Coastguard WorkerThe following code demonstrates a presubmit linter Action that uses 179*61c4878aSAndroid Build Coastguard Worker:ref:`module-pw_env_setup` and :ref:`module-pw_presubmit`. 180*61c4878aSAndroid Build Coastguard Worker 181*61c4878aSAndroid Build Coastguard Worker.. code-block:: yaml 182*61c4878aSAndroid Build Coastguard Worker 183*61c4878aSAndroid Build Coastguard Worker name: lintformat 184*61c4878aSAndroid Build Coastguard Worker 185*61c4878aSAndroid Build Coastguard Worker on: 186*61c4878aSAndroid Build Coastguard Worker pull_request: 187*61c4878aSAndroid Build Coastguard Worker types: [opened, synchronize, reopened] 188*61c4878aSAndroid Build Coastguard Worker 189*61c4878aSAndroid Build Coastguard Worker jobs: 190*61c4878aSAndroid Build Coastguard Worker bazel-build-test: 191*61c4878aSAndroid Build Coastguard Worker runs-on: ubuntu-latest 192*61c4878aSAndroid Build Coastguard Worker steps: 193*61c4878aSAndroid Build Coastguard Worker - name: Checkout 194*61c4878aSAndroid Build Coastguard Worker uses: actions/checkout@v4 195*61c4878aSAndroid Build Coastguard Worker with: 196*61c4878aSAndroid Build Coastguard Worker fetch-depth: 0 197*61c4878aSAndroid Build Coastguard Worker submodules: recursive 198*61c4878aSAndroid Build Coastguard Worker - name: Bootstrap 199*61c4878aSAndroid Build Coastguard Worker # When run locally, bootstrap.sh has checks to ensure that 200*61c4878aSAndroid Build Coastguard Worker # it's sourced (source bootstrap.sh) rather than executed 201*61c4878aSAndroid Build Coastguard Worker # directly. run.sh gets around this. 202*61c4878aSAndroid Build Coastguard Worker run: pw_env_setup/run.sh bootstrap.sh 203*61c4878aSAndroid Build Coastguard Worker - name: lintformat 204*61c4878aSAndroid Build Coastguard Worker run: pw presubmit --program lintformat --keep-going 205*61c4878aSAndroid Build Coastguard Worker 206*61c4878aSAndroid Build Coastguard Worker.. _understood by GitHub: https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-environment-variable 207*61c4878aSAndroid Build Coastguard Worker 208*61c4878aSAndroid Build Coastguard WorkerWhen ``pw_env_setup`` is run within a GitHub Action, it recognizes this from 209*61c4878aSAndroid Build Coastguard Workerthe environment and writes the environment variables in a way that is 210*61c4878aSAndroid Build Coastguard Worker`understood by GitHub`_, and GitHub makes those variables available to 211*61c4878aSAndroid Build Coastguard Workersubsequent steps. 212*61c4878aSAndroid Build Coastguard Worker 213*61c4878aSAndroid Build Coastguard Worker------------------- 214*61c4878aSAndroid Build Coastguard WorkerCreate more Actions 215*61c4878aSAndroid Build Coastguard Worker------------------- 216*61c4878aSAndroid Build Coastguard Worker.. _official GitHub Actions docs: https://docs.github.com/en/actions 217*61c4878aSAndroid Build Coastguard Worker 218*61c4878aSAndroid Build Coastguard WorkerYou can create as many Actions as you want! Just add new files to 219*61c4878aSAndroid Build Coastguard Worker``//.github/workflows`` and tweak the options as needed. Check out 220*61c4878aSAndroid Build Coastguard Workerthe `official GitHub Actions docs`_ to learn more. 221*61c4878aSAndroid Build Coastguard Worker 222*61c4878aSAndroid Build Coastguard Worker.. _docs-github-actions-support: 223*61c4878aSAndroid Build Coastguard Worker 224*61c4878aSAndroid Build Coastguard Worker------- 225*61c4878aSAndroid Build Coastguard WorkerSupport 226*61c4878aSAndroid Build Coastguard Worker------- 227*61c4878aSAndroid Build Coastguard WorkerPlease start a discussion in Pigweed's `Discord <https://discord.gg/M9NSeTA>`_. 228