Spinning up a local Gitea service for GitOps practice
I wanted a quick throwaway git service for some GitOps practice. Gitea is an open source Git hosting service. It is powerful for self-hosting a GitHub-like service, but also lightweight and straightforward to spin up as needed for little experiments.
Spin up a Gitea service in our cluster
First let's create ourselves a cluster with k3d.
k3d cluster create my-cluster
Then we can install a git server in the cluster using the Gitea Helm chart:
helm install gitea oci://registry-1.docker.io/giteacharts/gitea \
--namespace git --create-namespace \
--set gitea.admin.username=bob \
--set gitea.admin.password=password
Wait for Gitea to start up, which you can see with kubectl get pods -n git
, and then port forward onto the Gitea console so that we can access the Gitea service from our local machine.
kubectl --namespace git port-forward svc/gitea-http 3000:3000 &
Create the git repository in Gitea
We'll create a Git repository in Gitea with our bob
user with the
Gitea API.
First of all we need an access token to use the Gitea API:
ACCESS_TOKEN=`
curl -s -H "Content-Type: application/json" \
-d '{"name":"my-access-token", "scopes":["write:user","write:repository"]}' \
-u bob:password http://localhost:3000/api/v1/users/bob/tokens |
jq -r '.sha1'
`
Then we can create the repository:
curl -s -H "Content-Type: application/json" \
-H "Authorization: token $ACCESS_TOKEN" \
-d '{"name":"gitops"}' http://localhost:3000/api/v1/user/repos
You can, if you prefer, click this repository in by logging in to http://localhost:3000
with user
bob
and password password
and then creating a new Git repository via the
dashboard. I do want to script this up, so having the command line
approach is more powerful for this purpose. There is also a Gitea command
line which interfaces
with the API..
Push to our Git repository in the cluster
We can now prepare a Git repository locally and push a change to the Git remote served on our in-cluster Gitea service.
mkdir -p gitops
cd gitops
git init
echo "# GitOps" > README.md
git checkout -b main
git add .
git commit -m "Bootstrap repository"
git remote add origin http://localhost:3000/bob/gitops.git
git push -u origin main
You'll be prompted for the Git username bob
and password password
after which changes are pushed to the Gitea remote repository.
Ready to play with GitOps
This was a quick demonstration of how we can set up a temporary Git hosted service along with a Git repository. Sometimes when I'm experimenting with Git or GitOps workflows, I like to discard the repository at the end. Having a quick way to spin up a Git service and temporary repository is handy, without leaving a trail of destruction on other Git platforms, and I'll be using this approach in my next blog around getting started with ArgoCD.
Clean up
If we wanted to just drop the Git service we can delete the namespace
kubectl delete namespace git
Or we can delete the k3d cluster for a full clean
k3d cluster delete my-cluster