Automating Docker builds with Gitea Actions
I run Gitea as my self-hosted Git server, and I got tired of building and pushing Docker images by hand. Gitea Actions can do it for me: every push to main builds an image, tags it, and pushes it to my private registry.
Gitea Actions is compatible with GitHub Actions, so if you've written a GitHub workflow before, this will look familiar.
Before you start
You need three things:
- A runner. Gitea doesn't run jobs itself. You need act_runner registered to your instance, and the runner must be able to run Docker commands. Check this first, it's the step most people get stuck on.
- A
Dockerfilein the root of your repository. - Two secrets in your repository settings (Settings > Actions > Secrets):
REGISTRY_USERandREGISTRY_PASSWORD.
The workflow
Save this as .gitea/workflows/docker.yml:
name: Build and push Docker image
on:
push:
branches: [ main ]
env:
IMAGE: registry.example.com/your-username/your-repository
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Log in to registry
uses: docker/login-action@v3
with:
registry: registry.example.com
username: ${{ secrets.REGISTRY_USER }}
password: ${{ secrets.REGISTRY_PASSWORD }}
- name: Build and push
env:
BRANCH: ${{ github.ref_name }}
COMMIT: ${{ github.sha }}
run: |
TAG="${BRANCH}-${COMMIT:0:7}"
docker build --build-arg BUILD_IDENTIFIER="${COMMIT:0:7}" -t "$IMAGE:$TAG" .
docker tag "$IMAGE:$TAG" "$IMAGE:${BRANCH}-latest"
docker push "$IMAGE:$TAG"
docker push "$IMAGE:${BRANCH}-latest"
Push to main and the job shows up under the Actions tab of your repository.
How it works
on.push.branches limits the workflow to pushes on main. Pushes to other branches don't trigger a build.
The IMAGE variable at the top holds the full image name, so you only have to change it in one place.
actions/checkout fetches your code so the next steps can use it.
docker/login-action logs in to your registry with the secrets. The credentials never appear in the workflow file or in your Git history. The action also logs out again when the job ends, even if a step fails, so you don't need a separate logout step.
The build step creates two tags for the same image:
main-a1b2c3dcontains the first 7 characters of the commit hash. Every build gets its own tag, so you can always trace an image back to the commit that produced it, and roll back to an older one.main-latestalways points to the most recent build. That's handy in deployment scripts and compose files.
github.sha is the full 40-character hash. ${COMMIT:0:7} is plain Bash substring syntax that cuts it down to the same short hash Git shows you.
The BUILD_IDENTIFIER build argument passes the short hash into the image. Use it in your Dockerfile with ARG BUILD_IDENTIFIER if you want to show the version somewhere in your app. Don't need it? Remove the --build-arg.
Changing it for your own setup
- Replace
registry.example.comand the image name with your own. - Add a test step before the build, so a broken commit never becomes an image.
- Add more branches to
on.push.branches. The branch name is part of the tag, so images from different branches don't overwrite each other.
Running the image locally with Docker Compose? The Docker Compose cheat sheet has the commands I use every day.