brainsteam.co.uk/brainsteam/content/posts/2023/11/18/Gitea Actions and PDM.md

152 lines
6.9 KiB
Markdown

---
categories:
- Software Development
date: '2023-11-18 22:44:24'
draft: false
preview: /social/a8604cb16d569cb06b3ccc36a7b4bb176df3b6806c3ccf80d3724f9a282bfac9.png
tags:
- gitea
- python
title: Gitea Actions and PDM
type: posts
url: /2023/11/18/gitea-actions-and-pdm/
---
<!-- wp:paragraph -->
<p><a href="https://about.gitea.com/">Gitea</a> actions is the new Github-compatible CI/automation pipeline feature that ships with Gitea and <a href="https://forgejo.org/">Forgejo</a>. In theory it is interoperable with <a href="https://docs.github.com/en/actions">Github actions </a>but there are still a few rough edges and for that reason, the feature is still disabled by default.</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph -->
<p>I have been trying to get <a href="https://brainsteam.co.uk/2023/11/13/gastronaut-fediverse-recipe-app/" data-type="post" data-id="380">a django project</a> that uses <a href="https://brainsteam.co.uk/2021/04/01/opinionated-guide-to-virtualenvs/" data-type="post" data-id="193">PDM</a> for Python dependency management to to install itself and run some tests in a Gitea CI environment.</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph -->
<p>In theory, my workflow is simple:</p>
<!-- /wp:paragraph -->
<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Check out the code with actions/checkout@v4</li>
<!-- /wp:list-item -->
<!-- wp:list-item -->
<li>Install pdm with with<code> pdm-project/setup-pdm@v3</code></li>
<!-- /wp:list-item -->
<!-- wp:list-item -->
<li>Use a run: block to get pdm to install dependencies with <code>pdm install</code></li>
<!-- /wp:list-item -->
<!-- wp:list-item -->
<li>Use another run block to have PDM run my tests: <code>pdm run manage.py test</code></li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->
<!-- wp:paragraph -->
<p>Unfortunately there were a couple of odd quirks that I had to resolve first before I could get it working.</p>
<!-- /wp:paragraph -->
<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">Add a Github Key</h3>
<!-- /wp:heading -->
<!-- wp:paragraph -->
<p>I was initially getting an error about being Unauthorized from the <code>pdm-project/setup-pdm@v3</code> step. This is because the action attempts to download a list of pre-built python distributions from github and since we're running outside of github it is initially unable to get this list without an additional API key. All we need to do is <a href="https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens#creating-a-personal-access-token-classic">create a Github token</a> and then set up a new project secret and paste in the token that we just created. I use the name <code>GH_TOKEN</code> because Gitea does not allow you to use any secret prefixed <code>GITHUB</code>.</p>
<!-- /wp:paragraph -->
<!-- wp:image {"id":410,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="/media/image-10-1024x404_47995791.png" alt="A screenshot of the project settings in gitea. Navigate to Actions, Secrets, Add Secret and reate a new secret called GH_TOKEN." class="wp-image-410"/><figcaption class="wp-element-caption">Create a secret called GH_TOKEN which we can pass to the action.</figcaption></figure>
<!-- /wp:image -->
<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->
<!-- wp:paragraph -->
<p>Now, we can pass the token into the setup pdm step of the yaml like so:</p>
<!-- /wp:paragraph -->
<!-- wp:enlighter/codeblock {"language":"yaml"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="yaml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""> - uses: pdm-project/setup-pdm@v3
with:
python-version: 3.10
token: ${{ secrets.GH_TOKEN }}</pre>
<!-- /wp:enlighter/codeblock -->
<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->
<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">Change the Container Image</h3>
<!-- /wp:heading -->
<!-- wp:paragraph -->
<p>Once I resolved the authorization error above, I started getting error messages about how no Python releases were available for the given OS and architecture. I thought that was weird because in theory we're running Ubuntu on x64. <a href="https://forum.gitea.com/t/gitea-actions-with-python/7605/4">This forum post</a> suggested that changing the docker image that the runner uses to execute the pipeline might work. I'm not 100% sure what the default Gitea action runner uses as its base image but Gitea Actions are based on <a href="https://github.com/nektos/act">Act</a>, a local runner for Github actions and the official Act project recommends images by <a href="https://github.com/catthehacker/docker_images">catthehacker</a> for use as runners. By specifying one of these images, we seem to be able to 'fix' whatever metadata is missing from the default image.</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph -->
<p>We can pass a container in via the job <code>container</code> directive like so:</p>
<!-- /wp:paragraph -->
<!-- wp:enlighter/codeblock {"language":"yaml"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="yaml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">jobs:
run_tests:
runs-on: ubuntu-latest
container: catthehacker/ubuntu:act-latest
steps:
...
- uses: pdm-project/setup-pdm@v3
with:
python-version: 3.10
token: ${{ secrets.GH_TOKEN }}</pre>
<!-- /wp:enlighter/codeblock -->
<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->
<!-- wp:paragraph -->
<p>With this change in place, the rest of my pipeline seems to have burst into life. </p>
<!-- /wp:paragraph -->
<!-- wp:paragraph -->
<p>Here is the full yaml file for my CI:</p>
<!-- /wp:paragraph -->
<!-- wp:enlighter/codeblock {"language":"yaml"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="yaml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">name: Run Tests
run-name: ${{ gitea.actor }} is testing out Gitea Actions 🚀
on: [push]
jobs:
run_tests:
runs-on: ubuntu-latest
container: catthehacker/ubuntu:act-latest
steps:
- name: Checkout Codebase
uses: actions/checkout@v3
- name: Set up python
run: |
apt-get update && apt-get install -y python3-venv
pip install --upgrade pdm
- uses: pdm-project/setup-pdm@v3
with:
python-version: 3.10
token: ${{ secrets.GH_TOKEN }}
- name: Install dependencies
run: cd ${{ gitea.workspace }} && pdm install
- name: Run tests
run: |
cd ${{ gitea.workspace }} && pdm run manage.py test
</pre>
<!-- /wp:enlighter/codeblock -->
<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->