diff --git a/.gitea/workflow/ci.yaml b/.gitea/workflow/ci.yaml new file mode 100644 index 0000000..2f53e20 --- /dev/null +++ b/.gitea/workflow/ci.yaml @@ -0,0 +1,31 @@ +name: ci + +on: + push: + branches: + - "main" + +jobs: + docker: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v5 + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - name: Login to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + - name: Build and push + uses: docker/build-push-action@v5 + with: + context: . + push: true + platforms: linux/amd64 + tags: | + esinio/gitea-runner-ci-demo:latest + \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..abcf14d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +FROM golang:alpine as build + +WORKDIR /go/src + +ADD . . + +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories \ + && apk --no-cache add ca-certificates \ + && go env -w GOPROXY='https://goproxy.cn,direct' \ + && go mod tidy \ + && go build -o /go/bin/app . + +FROM scratch +# copy the ca-certificate.crt from the build stage +COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ +COPY --from=build /go/bin/app /app + +EXPOSE 80 + +ENTRYPOINT ["/app"] \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..450e775 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/gitea/runner-ci-demo + +go 1.18 + +require github.com/gorilla/mux v1.8.1 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..7128337 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= +github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= diff --git a/main.go b/main.go new file mode 100644 index 0000000..522b5ef --- /dev/null +++ b/main.go @@ -0,0 +1,37 @@ +package main + +import ( + "fmt" + "log" + "net/http" + "time" + + "github.com/gorilla/mux" +) + +func main() { + r := mux.NewRouter() + r.HandleFunc("/", HomeHandler) + r.HandleFunc("/now", TimeHandler) + http.Handle("/", r) + + srv := &http.Server{ + Handler: r, + Addr: "0.0.0.0:80", + // Good practice: enforce timeouts for servers you create! + WriteTimeout: 15 * time.Second, + ReadTimeout: 15 * time.Second, + } + + log.Fatal(srv.ListenAndServe()) +} + +func HomeHandler(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, "Hello world") +} + +func TimeHandler(w http.ResponseWriter, r *http.Request) { + now := time.Now() + res := now.GoString() + fmt.Fprint(w, res) +}