搭建Gitea及Actions

2023/8/7 giteaactions

搭建Gitea及Actions, 配置gitops, 以及问题处理

# 1、安装

# 1.1 gitea安装

apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: git
  name: gitea
  labels:
    app: gitea
spec:
  replicas: 1
  selector:
    matchLabels:
      app: gitea
  template:
    metadata:
      labels:
        app: gitea
    spec:
      containers:
      - name: gitea
        image: gitea/gitea:1.20.2
        ports:
        - containerPort: 3000
          name: gitea-http
        - containerPort: 22
          name: gitea-ssh
        volumeMounts:
        - mountPath: /data
          name: gitea-data
      volumes:
      - name: gitea-data
        persistentVolumeClaim:
          claimName: gitea-pvc
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: gitea-pvc
  namespace: git
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
---
kind: Service
apiVersion: v1
metadata:
  name: gitea-http
  namespace: git
spec:
  selector:
    app: gitea
  type: NodePort
  ports:
  - name: gitea-http
    port: 3000
    targetPort: gitea-http
    nodePort: 8888
---
kind: Service
apiVersion: v1
metadata:
  name: gitea-ssh
  namespace: git
spec:
  selector:
    app: gitea
  type: NodePort
  ports:
  - name: gitea-ssh
    port: 22
    targetPort: gitea-ssh
    nodePort: 2222
  • gitea 在大等于 1.19 版本才支持 acitons, 1.20.x稳定版本
  • actions默认未启动,需要在配置文件中启用 gitea/conf/app.ini
[actions]
ENABLED=true

官方文件链接 (opens new window)

# 1.2 actions安装

---
apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: git
  name: gitea-runner
  labels:
    app: gitea-runner
spec:
  replicas: 1
  selector:
    matchLabels:
      app: gitea-runner
  template:
    metadata:
      labels:
        app: gitea-runner
    spec:
      containers:
        - name: gitea-runner
          image: gitea/act_runner:0.2.5
          env:
            - name: GITEA_INSTANCE_URL
              value: https://yourdomain/
            - name: GITEA_RUNNER_REGISTRATION_TOKEN
              value: xxxxxxxxxxx
          volumeMounts:
            - mountPath: /var/run/docker.sock
              name: gitea-runner-docker
            - mountPath: /data
              name: gitea-runner-data
      volumes:
        - name: gitea-runner-docker
          hostPath:
            path: /var/run/docker.sock
        - name: gitea-runner-data
          persistentVolumeClaim:
            claimName: gitea-runner-pvc
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: gitea-runner-pvc
  namespace: git
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
  • GITEA_INSTANCE_URL: 自建gitea服务访问地址
  • GITEA_RUNNER_REGISTRATION_TOKEN: 用于身份验证和标识,例如 P2U1U0oB4XaRCi8azcngmPCLbRpUGapalhmddh23

官方文件链接 (opens new window)

# 1.3 使用actions

启动完成后需要在gitea中待启用actions的仓库中配置启动 Enable Repository Actions

.gitea/workflows/目录下创建 halo.yml 文件, 以开源博客项目 halo 构建为列

name: Halo Image Builder Actions
run-name: ${{ gitea.actor }} is producting out Gitea Actions
on:
  push:
    tags:
      - v*
jobs:
  Halo-Image-Gitea-Actions:
    runs-on: ubuntu-latest
    container:
      image: catthehacker/ubuntu:act-latest
      volumes:
        - act-toolcache:/opt/hostedtoolcache
    env:
      DOCKERHUB_REGISTRY: 'yourdockerhubdomain'
      DOCKERHUB_NAMESPACE: 'halo'
      APP_NAME: 'halo-app'
    steps:
      - name: Check out repository code
        uses: https://yourdomain/actions/checkout@v3
      - name: Set up JDK 17
        uses: https://yourdomain/actions/setup-java@v3.12.0.2
        with:
          distribution: 'temurin'
          cache: 'gradle'
          java-version: 17
      - name: Build halo by Gradle
        run: ./gradlew clean build -x check
      - name: Login local Dockerhub
        uses: https://yourdomain/docker/login-action@v2
        with:
          registry: ${{ env.DOCKERHUB_REGISTRY }}
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
      - name: Build && Push Docker
        uses: https://yourdomain/docker/build-push-action@master
        with:
          context: .
          file: ./Dockerfile
          push: true
          tags: |
            ${{ env.DOCKERHUB_REGISTRY }}/${{ env.DOCKERHUB_NAMESPACE }}/${{ env.APP_NAME }}:${{ env.GITHUB_RUN_NUMBER }}

部署遇到的问题:

  • 如果使用mysql作为数据库,yaml不能包含特殊符号,例如 🎉
  • 必须指定构建容器, 因为默认使用 node:16-bullseye 没有 docker cli, 推荐使用 catthehacker/ubuntu:act-latest
  • 必须配置容器卷映射, 并且必须为 act-toolcache:/opt/hostedtoolcache 用于缓存下载的 jdk node
  • 编写语法与 github actions 类似,并且兼容大部分 github actions
  • 默认情况下从 https://github.com/ 拉取 action, 但是由于国内 github 域名污染问题,访问时长失败,应将对应的项目拉去到自建的gitea中,并设置为 public
  • setup-java@v3 第一次执行会从 https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.8%2B7/OpenJDK17U-jdk_x64_linux_hotspot_17.0.8_7.tar.gz 下载 jdk, 也是域名污染问题,一般会下载失败,那么需要自建或公共 https://github.com/hunshcn/gh-proxy 代理,并修改 setup-java 源码 src/distributions/temurin/installer.ts, 构建 npm i && npm run build, 设置 tag: v3.12.0.2
core.info(
  `Downloading Java ${javaRelease.version} (${this.distribution}) from https://yourproxydomain/${javaRelease.url} ...`
);
const javaArchivePath = await tc.downloadTool('http://yourproxydomain/' + javaRelease.url);

与GitHub Actions的对比 (opens new window)

Gitea Actions设计 (opens new window)