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

6.8 KiB

categories date draft tags title type url
Software Development
2023-11-18 22:44:24 false
gitea
python
Gitea Actions and PDM posts /2023/11/18/gitea-actions-and-pdm/

Gitea actions is the new Github-compatible CI/automation pipeline feature that ships with Gitea and Forgejo. In theory it is interoperable with Github actions but there are still a few rough edges and for that reason, the feature is still disabled by default.

I have been trying to get a django project that uses PDM for Python dependency management to to install itself and run some tests in a Gitea CI environment.

In theory, my workflow is simple:

  • Check out the code with actions/checkout@v4
  • Install pdm with with pdm-project/setup-pdm@v3
  • Use a run: block to get pdm to install dependencies with pdm install
  • Use another run block to have PDM run my tests: pdm run manage.py test

Unfortunately there were a couple of odd quirks that I had to resolve first before I could get it working.

Add a Github Key

I was initially getting an error about being Unauthorized from the pdm-project/setup-pdm@v3 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 create a Github token and then set up a new project secret and paste in the token that we just created. I use the name GH_TOKEN because Gitea does not allow you to use any secret prefixed GITHUB.

A screenshot of the project settings in gitea. Navigate to Actions, Secrets, Add Secret and reate a new secret called GH_TOKEN.
Create a secret called GH_TOKEN which we can pass to the action.

Now, we can pass the token into the setup pdm step of the yaml like so:

      - uses: pdm-project/setup-pdm@v3
        with:
          python-version: 3.10
          token: ${{ secrets.GH_TOKEN }}

Change the Container Image

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. This forum post 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 Act, a local runner for Github actions and the official Act project recommends images by catthehacker 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.

We can pass a container in via the job container directive like so:

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 }}

With this change in place, the rest of my pipeline seems to have burst into life.

Here is the full yaml file for my CI:

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