diff --git a/.github/actions/setup-db/Dockerfile b/.github/actions/setup-db/Dockerfile new file mode 100644 index 00000000..227218cb --- /dev/null +++ b/.github/actions/setup-db/Dockerfile @@ -0,0 +1,6 @@ +FROM docker:stable + + +COPY entrypoint.sh /entrypoint.sh +RUN chmod +x /entrypoint.sh +ENTRYPOINT ["/entrypoint.sh"] \ No newline at end of file diff --git a/.github/actions/setup-db/action.yml b/.github/actions/setup-db/action.yml new file mode 100644 index 00000000..aa1f2f76 --- /dev/null +++ b/.github/actions/setup-db/action.yml @@ -0,0 +1,19 @@ +inputs: + kind: + description: "Chose the kind of database (MySQL8.0, MySQL5.7, Postgres9.6, Postgres14.0, SQLite3)" + required: true + db: + description: "The name of database" + required: false + default: "github" + user: + description: "The user of database" + required: false + default: "github" + password: + description: "The passowrd of database" + required: false + default: "123456" +runs: + using: "docker" + image: "Dockerfile" diff --git a/.github/actions/setup-db/entrypoint.sh b/.github/actions/setup-db/entrypoint.sh new file mode 100755 index 00000000..06be1993 --- /dev/null +++ b/.github/actions/setup-db/entrypoint.sh @@ -0,0 +1,79 @@ +#!/bin/sh + +docker_run="docker run" + +startMySQL() { + VERSION=$1 + echo "Start MySQL $VERSION" + docker_run="$docker_run -e MYSQL_RANDOM_ROOT_PASSWORD=true -e MYSQL_USER=$INPUT_USER -e MYSQL_PASSWORD=$INPUT_PASSWORD" + docker_run="$docker_run -e MYSQL_DATABASE=$INPUT_DB" + docker_run="$docker_run -d -p 3306:3306 mysql:$VERSION --port=3306" + + if [ "$VERSION" = "5.6" ]; then + docker_run="$docker_run --character-set-server=utf8 --collation-server=utf8_general_ci" + else + docker_run="$docker_run --character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci" + fi + + sh -c "$docker_run" + + DB_HOST="tcp(127.0.0.1:3306)/$INPUT_DB?charset=utf8mb4&parseTime=True&loc=Local" + DB_USER=$INPUT_USER + echo "DB_HOST=$DB_HOST" >> $GITHUB_ENV + echo "DB_USER=$DB_USER" >> $GITHUB_ENV + echo "DB_DRIVER=mysql" >> $GITHUB_ENV + echo "DB_NAME=mysql$VERSION" >> $GITHUB_ENV + echo "$DB_HOST" +} + +startPostgres() { + VERSION=$1 + echo "Start Postgres $VERSION" + docker_run="$docker_run --name postgres_$VERSION" + docker_run="$docker_run -e POSTGRES_DB=$INPUT_DB" + docker_run="$docker_run -e POSTGRES_USER=$INPUT_USER" + docker_run="$docker_run -e POSTGRES_PASSWORD=$INPUT_PASSWORD" + docker_run="$docker_run -d -p 5432:5432 postgres:$VERSION" + sh -c "$docker_run" + + # waiting for postgres ready + timeout 90s sh -c "until docker exec postgres_$VERSION pg_isready ; do sleep 5 ; done" + + DB_HOST="127.0.0.1/$INPUT_DB?sslmode=disable" + DB_USER=$INPUT_USER + echo "DB_HOST=$DB_HOST" >> $GITHUB_ENV + echo "DB_USER=$DB_USER" >> $GITHUB_ENV + echo "DB_DRIVER=postgres" >> $GITHUB_ENV + echo "DB_NAME=postgres$VERSION" >> $GITHUB_ENV + echo "$DB_HOST" +} + +startSQLite3() { + echo "Start SQLite3" + echo "DB_HOST=file:$INPUT_DB.db?cache=shared&mode=memory" >> $GITHUB_ENV + echo "DB_DRIVER=sqlite3" >> $GITHUB_ENV + echo "DB_NAME=sqlite3" >> $GITHUB_ENV + echo "$DB_HOST" +} + +# MySQL8.0, MySQL5.7, Postgres9.6, Postgres14, SQLite3 +case $INPUT_KIND in +MySQL8.0) + startMySQL 8.0 + ;; +MySQL5.7) + startMySQL 5.7 + ;; +MySQL5.6) + startMySQL 5.6 + ;; +Postgres9.6) + startPostgres 9.6 + ;; +Postgres14.0) + startPostgres 14.0 + ;; +SQLite3) + startSQLite3 + ;; +esac diff --git a/.github/workflows/unit-test.yml b/.github/workflows/unit-test.yml new file mode 100644 index 00000000..92df21e1 --- /dev/null +++ b/.github/workflows/unit-test.yml @@ -0,0 +1,114 @@ +name: UnitTest + +on: + push: + branches: [main] + pull_request: + branches: [main] +env: + # 象传服务配置 + XIANG_MODE: debug + XIANG_SOURCE: ${{ env.GITHUB_WORKSPACE }} + XIANG_ROOT: fs:///${{ env.GITHUB_WORKSPACE }}/app + + # 服务配置 + XIANG_SERVICE_DEBUG: true + XIANG_SERVICE_ALLOW: "xiang.yao.run:3000|xiang.yao.run:3001" + XIANG_SERVICE_HOST: 0.0.0.0 + XIANG_SERVICE_PORT: 5099 + + # 数据库配置 + XIANG_DB_DEBUG: true + XIANG_DB_DRIVER: "mysql" + XIANG_DB_PRIMARY: "root:123456@tcp(db-server:3308)/xun?charset=utf8mb4&parseTime=True&loc=Local" + XIANG_DB_SECONDARY: "root:123456@tcp(db-server:3308)/xun?charset=utf8mb4&parseTime=True&loc=Local" + XIANG_DB_AESKEY: "ZLX:XzT&f6refeCh-ro*r@" + + # JWT配置 + XIANG_JWT_DEBUG: true + XIANG_JWT_SECRET: "bLp@bi!oqo-2U+hoTRUG" + + # 存储配置 + XIANG_STOR_DEBUG: true + XIANG_STOR_PATH: "fs:///${{ env.GITHUB_WORKSPACE }}/upload" + + # 日志配置 + XIANG_LOG_ACCESS: "fs:///${{ env.GITHUB_WORKSPACE }}/logs/access.log" + XIANG_LOG_ERROR: "fs:///${{ env.GITHUB_WORKSPACE }}/logs/error.log" + XIANG_LOG_DB: "fs:///${{ env.GITHUB_WORKSPACE }}/logs/db.log" + XIANG_LOG_PLUGIN: "fs:///${{ env.GITHUB_WORKSPACE }}/logs/plugin.log" + +jobs: + unit-test: + runs-on: ubuntu-latest + strategy: + matrix: + go: [1.17] + db: [SQLite3] + steps: + - name: Checkout Kun + uses: actions/checkout@v2 + with: + repository: yaoapp/kun + path: kun + + - name: Checkout Xun + uses: actions/checkout@v2 + with: + repository: yaoapp/xun + path: xun + + - name: Checkout Gou + uses: actions/checkout@v2 + with: + repository: yaoapp/gou + path: gou + + - name: Checkout Xiang + uses: actions/checkout@v2 + + - name: Setup ${{ matrix.db }} + uses: ./.github/actions/setup-db + with: + kind: "${{ matrix.db }}" + db: "xun" + user: "xun" + password: ${{ secrets.UNIT_PASS }} + + - name: Setup Go ${{ matrix.go }} + uses: actions/setup-go@v2 + with: + go-version: ${{ matrix.go }} + + - name: Setup Go Tools + run: | + if [[ "${GO111MODULE}" = "on" ]]; then go mod download; fi + if [[ "${GO111MODULE}" = "on" ]]; then export PATH="${GOPATH}/bin:${GOROOT}/bin:${PATH}"; fi + if [[ "${GO111MODULE}" = "on" ]]; then make tools; fi + + - name: Setup ENV & Host + run: | + sudo echo "127.0.0.1 local.iqka.com" | sudo tee -a /etc/hosts + echo "XUN_UNIT_NAME=$DB_NAME" >> $GITHUB_ENV + echo "XUN_UNIT_DRIVER=$DB_DRIVER" >> $GITHUB_ENV + echo "XUN_UNIT_LOG=$HOME/${{ matrix.db }}-${{ matrix.go }}.log" >> $GITHUB_ENV + echo "ls -l ." + + # - name: Run Test + # env: + # PASSWORD: ${{ secrets.UNIT_PASS }} + # run: | + # make vet + # make fmt-check + # make misspell-check + + # if [ "$DB_DRIVER" = "mysql" ]; then + # XUN_UNIT_SOURCE="$DB_USER:$PASSWORD@$DB_HOST" make test + # elif [ "$DB_DRIVER" = "postgres" ]; then + # XUN_UNIT_SOURCE="postgres://$DB_USER:$PASSWORD@$DB_HOST" make test + # else + # XUN_UNIT_SOURCE="$DB_HOST" make test + # fi + + # - name: Codecov Report + # uses: codecov/codecov-action@v2