|
| 1 | +# GitHub Actions recipes |
| 2 | + |
| 3 | +We use [GitHub Actions](https://github.com/features/actions) for some of our contionuous integration and other automations. |
| 4 | +This guide is a collection of actions recipes to perform tasks that may be common across repositories. |
| 5 | + |
| 6 | +- [Running front-end unit tests with gulp](#running-front-end-unit-tests-with-gulp) |
| 7 | +- [Running back-end unit tests with tox](#running-back-end-unit-tests-with-tox) |
| 8 | +- [Fetching git history for setuptools-git-version](#fetching-git-history-for-setuptools-git-version) |
| 9 | +- [Attaching a Python wheel file to a GitHub release](#attaching-a-python-wheel-file-to-a-github-release) |
| 10 | +- [Publishing a Python package to PyPI on release](#publishing-a-python-package-to-pypi-on-release) |
| 11 | +- [Publishing MkDocs documentation to GitHub pages](#publishing-mkdocs-documentation-to-github-pages) |
| 12 | + |
| 13 | + |
| 14 | +## Running front-end unit tests |
| 15 | + |
| 16 | +```yml |
| 17 | +jobs: |
| 18 | + |
| 19 | + frontend: |
| 20 | + runs-on: ubuntu-latest |
| 21 | + |
| 22 | + steps: |
| 23 | + - uses: actions/checkout@v2 |
| 24 | + |
| 25 | + - name: Set up Node |
| 26 | + uses: actions/setup-node@v1 |
| 27 | + with: |
| 28 | + node-version: 10.x |
| 29 | + |
| 30 | + - name: Install Node dependencies |
| 31 | + run: | |
| 32 | + npm config set package-lock false |
| 33 | + npm install |
| 34 | +
|
| 35 | + - name: Lint front-end code |
| 36 | + run: gulp lint |
| 37 | + |
| 38 | + - name: Run front-end unit tests |
| 39 | + run: npm test |
| 40 | +``` |
| 41 | +
|
| 42 | +## Running back-end unit tests with tox |
| 43 | +
|
| 44 | +```yml |
| 45 | +jobs: |
| 46 | + backend: |
| 47 | + runs-on: ubuntu-latest |
| 48 | + |
| 49 | + steps: |
| 50 | + - uses: actions/checkout@v2 |
| 51 | + |
| 52 | + - name: Set up Python |
| 53 | + uses: actions/setup-python@v1 |
| 54 | + with: |
| 55 | + python-version: 3.6 |
| 56 | + |
| 57 | + - name: Install Python dependencies |
| 58 | + run: | |
| 59 | + python -m pip install --upgrade pip |
| 60 | + pip install tox |
| 61 | +
|
| 62 | + - name: Run back-end tests |
| 63 | + run: | |
| 64 | + tox |
| 65 | +``` |
| 66 | +
|
| 67 | +## Fetching git history for setuptools-git-version |
| 68 | +
|
| 69 | +Some of our consumerfinance.gov satellite apps use [setuptools-git-version](https://github.com/pyfidelity/setuptools-git-version) to set the Python package version from the latest git tag. This requires tags too be available in the checkout in the GitHub action: |
| 70 | +
|
| 71 | +```yml |
| 72 | + steps: |
| 73 | + - uses: actions/checkout@v2 |
| 74 | + |
| 75 | + - name: Fetch tags and commits needed for setuptools-git-version |
| 76 | + run: | |
| 77 | + git fetch --depth=1 origin +refs/tags/*:refs/tags/* |
| 78 | + git fetch origin ${{ github.head_ref }} && git checkout ${{ github.head_ref }} |
| 79 | +``` |
| 80 | +
|
| 81 | +The intention is for the command `git describe --tags --long --dirty` to succeed. |
| 82 | + |
| 83 | +## Attaching a Python wheel file to a GitHub release |
| 84 | + |
| 85 | +Some of our consumerfinance.gov satellite apps have a Python wheel package file attached to their GitHub releases. |
| 86 | + |
| 87 | +```yml |
| 88 | +name: Publish |
| 89 | +
|
| 90 | +on: |
| 91 | + release: |
| 92 | + types: [published] |
| 93 | +
|
| 94 | +jobs: |
| 95 | + release: |
| 96 | + runs-on: ubuntu-latest |
| 97 | + steps: |
| 98 | + - uses: actions/checkout@v1 |
| 99 | +
|
| 100 | + # If the package has a front-end build we need to set up and install |
| 101 | + # Node and other dependencies |
| 102 | + - name: Set up Node |
| 103 | + uses: actions/setup-node@v1 |
| 104 | + with: |
| 105 | + node-version: 10.x |
| 106 | +
|
| 107 | + - name: Install Node dependencies |
| 108 | + run: | |
| 109 | + npm install -g gulp-cli |
| 110 | + npm config set package-lock false |
| 111 | +
|
| 112 | + - name: Set up Python |
| 113 | + uses: actions/setup-python@v1 |
| 114 | + with: |
| 115 | + python-version: 3.6 |
| 116 | +
|
| 117 | + - name: Install Python dependencies |
| 118 | + run: python -m pip install --upgrade pip wheel |
| 119 | +
|
| 120 | + - name: Build the packages |
| 121 | + id: build |
| 122 | + run: | |
| 123 | + python setup.py sdist bdist_wheel |
| 124 | + # Get the name of the .whl and .tar.gz files and set them as |
| 125 | + # "outputs" of this step so we can upload them |
| 126 | + echo "::set-output name=bdist_wheel::$(cd dist && ls *.whl)" |
| 127 | + echo "::set-output name=sdist::$(cd dist && ls *.tar.gz)" |
| 128 | +
|
| 129 | + - name: Upload the wheel |
| 130 | + uses: actions/upload-release-asset@v1 |
| 131 | + env: |
| 132 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 133 | + with: |
| 134 | + upload_url: ${{ github.event.release.upload_url }} |
| 135 | + asset_path: dist/${{ steps.build.outputs.bdist_wheel }} |
| 136 | + asset_name: ${{ steps.build.outputs.bdist_wheel }} |
| 137 | + asset_content_type: application/zip |
| 138 | +
|
| 139 | + - name: Upload the source distribution |
| 140 | + uses: actions/upload-release-asset@v1 |
| 141 | + env: |
| 142 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 143 | + with: |
| 144 | + upload_url: ${{ github.event.release.upload_url }} |
| 145 | + asset_path: dist/${{ steps.build.outputs.sdist }} |
| 146 | + asset_name: ${{ steps.build.outputs.sdist }} |
| 147 | + asset_content_type: application/gzip |
| 148 | +``` |
| 149 | + |
| 150 | +## Publishing a Python package to PyPI on release |
| 151 | + |
| 152 | +Some of our libraries get [published to PyPI](pypi.md). |
| 153 | +This requires `TWINE_USERNAME` and `TWINE_PASSWORD` set as repository secrets. |
| 154 | + |
| 155 | +```yml |
| 156 | +name: Publish to PyPI |
| 157 | +on: |
| 158 | + release: |
| 159 | + types: [published] |
| 160 | +
|
| 161 | +jobs: |
| 162 | + build: |
| 163 | + runs-on: ubuntu-latest |
| 164 | + steps: |
| 165 | +
|
| 166 | + - uses: actions/checkout@v1 |
| 167 | +
|
| 168 | + - name: Set up Python |
| 169 | + uses: actions/setup-python@v1 |
| 170 | + with: |
| 171 | + python-version: 3.8 |
| 172 | +
|
| 173 | + - name: Install dependencies |
| 174 | + run: | |
| 175 | + python -m pip install --upgrade pip |
| 176 | + pip install twine wheel |
| 177 | +
|
| 178 | + - name: Build the package |
| 179 | + run: | |
| 180 | + python setup.py sdist bdist_wheel --universal |
| 181 | +
|
| 182 | + - name: Upload to PyPI |
| 183 | + run: twine upload dist/* |
| 184 | + env: |
| 185 | + TWINE_USERNAME: ${{ secrets.TWINE_USERNAME }} |
| 186 | + TWINE_PASSWORD: ${{ secrets.TWINE_PASSWORD }} |
| 187 | +``` |
| 188 | + |
| 189 | +## Publishing MkDocs documentation to GitHub pages |
| 190 | + |
| 191 | +```yml |
| 192 | +on: |
| 193 | + push: |
| 194 | + branches: |
| 195 | + - master |
| 196 | +
|
| 197 | +jobs: |
| 198 | +
|
| 199 | + publish: |
| 200 | + runs-on: ubuntu-latest |
| 201 | +
|
| 202 | + steps: |
| 203 | + - uses: actions/checkout@v2 |
| 204 | + - run: | |
| 205 | + git fetch --no-tags --prune --depth=1 origin gh-pages |
| 206 | +
|
| 207 | + - name: Set up Python |
| 208 | + uses: actions/setup-python@v1 |
| 209 | + with: |
| 210 | + python-version: 3.6 |
| 211 | +
|
| 212 | + - name: Install dependencies |
| 213 | + run: | |
| 214 | + python -m pip install --upgrade pip |
| 215 | + pip install -r mkdocs |
| 216 | +
|
| 217 | + - name: Build docs |
| 218 | + run: mkdocs build |
| 219 | +
|
| 220 | + - name: Publish docs |
| 221 | + run: mkdocs gh-deploy |
| 222 | +``` |
0 commit comments