Docker 搭建 Gitea

140 阅读1分钟

目的

个人学习实践使用,Gitea 数据库是 PostgreSQL

参考

  1. docs.gitea.com/zh-cn/
  2. about.gitea.com/resources/t…

注意点

  1. 关于 Job 容器拉取不到 Gitea 实例代码,无法通信的问题,建议 docs.gitea.com/zh-cn/usage… 多看几遍
  2. 方便容器间通讯,自定义了 name=gitea 的 networks

实施

创建 Gitea 目录和 docker-compose 配置文件

mkdir -p gitea/{data,config,postgres,runner}
cd gitea  
touch docker-compose.yml

配置 docker-compose.yml

version: "3.8"

networks:
  gitea:
    external: false
    name: gitea

services:
  gitea:
    image: gitea/gitea:latest-rootless
    container_name: gitea
    environment:
      - GITEA__database__DB_TYPE=postgres
      - GITEA__database__HOST=db:5432
      - GITEA__database__NAME=gitea
      - GITEA__database__USER=gitea
      - GITEA__database__PASSWD=gitea
    restart: always
    networks:
      - gitea
    volumes:
      - ./data:/data
      - ./config:/etc/gitea
      - /etc/timezone:/etc/timezone:ro
      - /etc/localtime:/etc/localtime:ro
    ports:
      - "3000:3000"
      - "2222:22"
    depends_on:
      - db

  db:
    image: postgres:latest
    container_name: db
    restart: always
    environment:
      - POSTGRES_USER=gitea
      - POSTGRES_PASSWORD=gitea
      - POSTGRES_DB=gitea
    networks:
      - gitea
    volumes:
      - ./postgres:/var/lib/postgresql/data

  runner:
    image: gitea/act_runner:latest
    container_name: runner
    environment:
      CONFIG_FILE: /config.yaml
      GITEA_INSTANCE_URL: "http://gitea:3000"
      GITEA_RUNNER_REGISTRATION_TOKEN: "hlXZDNT8eh6iCC1Vibqwc2sw56rASgqlhXIhoyPq" # 更改成自己的token
      GITEA_RUNNER_NAME: "my-runner" # 更改成自己的名字
      # GITEA_RUNNER_LABELS: "${RUNNER_LABELS}" 这里注释,让系统自动生成
    networks:
      - gitea
    volumes:
      - ./runner/config.yaml:/config.yaml
      - ./runner/data:/data
      - /var/run/docker.sock:/var/run/docker.sock
    depends_on:
      - gitea

配置完之后,启动容器

docker-compose up -d

image.png

初始化访问

访问 http://localhost:3000/ ,点击【立即安装】

image.png

注册管理员账号

image.png

image.png

配置 Action Runner

获取 TOKEN

访问 http://localhost:3000/user/settings/actions/runners 获取 TOKEN

image.png

生成配置
docker run --entrypoint="" --rm -it gitea/act_runner:latest act_runner generate-config >./runner/config.yaml
修改配置

修改 container.network="gitea" ,使 Job 容器用之前已经创建好的 gitea ,以便和 Gitea 实例正常通讯拉取代码

container:
  # Specifies the network to which the container will connect.
  # Could be host, bridge or the name of a custom network.
  # If it's empty, act_runner will create a network automatically.
  network: "gitea" # 修改成之前定义好的 networks.name
  # Whether to use privileged mode or not when launching task containers (privileged mode is required for Docker-in-Docker).
  privileged: false
  # And other options to be used when the container is started (eg, --add-host=my.gitea.url:host-gateway).
  options:
  # The parent directory of a job's working directory.
  # NOTE: There is no need to add the first '/' of the path as act_runner will add it automatically.
  # If the path starts with '/', the '/' will be trimmed.
  # For example, if the parent directory is /path/to/my/dir, workdir_parent should be path/to/my/dir
  # If it's empty, /workspace will be used.
  workdir_parent:
  # Volumes (including bind mounts) can be mounted to containers. Glob syntax is supported, see https://github.com/gobwas/glob
  # You can specify multiple volumes. If the sequence is empty, no volumes can be mounted.
  # For example, if you only allow containers to mount the `data` volume and all the json files in `/src`, you should change the config to:
  # valid_volumes:
  #   - data
  #   - /src/*.json
  # If you want to allow any volume, please use the following configuration:
  # valid_volumes:
  #   - '**'
  valid_volumes: []
  # overrides the docker client host with the specified one.
  # If it's empty, act_runner will find an available docker host automatically.
  # If it's "-", act_runner will find an available docker host automatically, but the docker host won't be mounted to the job containers and service containers.
  # If it's not empty or "-", the specified docker host will be used. An error will be returned if it doesn't work.
  docker_host: ""
  # Pull docker image(s) even if already present
  force_pull: true
  # Rebuild docker image(s) even if already present
  force_rebuild: false
注册 Runner

把前面获取的 TOKEN 贴到 docker-compose.yml 的 services.runner.environment.GITEA_RUNNER_REGISTRATION_TOKEN

  runner:
    image: gitea/act_runner:latest
    container_name: runner
    environment:
      CONFIG_FILE: /config.yaml
      GITEA_INSTANCE_URL: "http://gitea:3000"
      GITEA_RUNNER_REGISTRATION_TOKEN: "hlXZDNT8eh6iCC1Vibqwc2sw56rASgqlhXIhoyPq" # 更改成自己的token
      GITEA_RUNNER_NAME: "my-runner" # 更改成自己的名字
      # GITEA_RUNNER_LABELS: "${RUNNER_LABELS}" 这里注释,让系统自动生成
    networks:
      - gitea
    volumes:
      - ./runner/config.yaml:/config.yaml
      - ./runner/data:/data
      - /var/run/docker.sock:/var/run/docker.sock
    depends_on:
      - gitea

然后执行,注册 Runner 成功

 docker-compose up -d

image.png

配置 workflow

创建仓库,设置,开启 Action,保存更新提交

image.png

image.png

创建 .gitea/workflows/demo.yaml 文件,push ,触发流水线,具体执行情况在 Action 中可查看,点击可查详细

name: Gitea Actions Demo
run-name: ${{ gitea.actor }} is testing out Gitea Actions 🚀
on: [push]

jobs:
  Explore-Gitea-Actions:
    runs-on: ubuntu-latest
    steps:
      - run: echo "🎉 The job was automatically triggered by a ${{ gitea.event_name }} event."
      - run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by Gitea!"
      - run: echo "🔎 The name of your branch is ${{ gitea.ref }} and your repository is ${{ gitea.repository }}."
      - name: Check out repository code
        uses: actions/checkout@v3
      - run: echo "💡 The ${{ gitea.repository }} repository has been cloned to the runner."
      - run: echo "🖥️ The workflow is now ready to test your code on the runner."
      - name: List files in the repository
        run: |
          ls ${{ gitea.workspace }}                    
      - run: echo "🍏 This job's status is ${{ job.status }}."

image.png

image.png