From e9ff350f757c67d6b501094bfddc857861b5cf4a Mon Sep 17 00:00:00 2001 From: Nate Meyer <672246+notnmeyer@users.noreply.github.com> Date: Wed, 18 Oct 2023 16:50:29 -0700 Subject: [PATCH] feat: ci and container images (#1) --- .github/workflows/release.yml | 29 +++++++++++++++++++++++++++++ Dockerfile | 12 ++++++++++++ README.md | 17 +++++++++++++++++ main.go | 4 +++- 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/release.yml create mode 100644 Dockerfile diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..329aba9 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,29 @@ +name: release + +on: + workflow_dispatch: + push: + branches: main + +jobs: + login: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Login to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Build and push + uses: docker/build-push-action@v5 + with: + push: false + tags: ghcr.io/notnmeyer/mockpi:latest diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2753746 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM docker.io/golang:1.21 as builder +ENV CGO_ENABLED=0 \ + GOOS=linux \ + GOARCH=amd64 +WORKDIR /build +COPY *.go . +COPY go.mod . +RUN go build -o dist/mockpi ./main.go + +FROM scratch +COPY --from=builder /build/dist/mockpi /usr/local/bin/ +CMD ["/usr/local/bin/mockpi"] diff --git a/README.md b/README.md index 4f776c8..802af4e 100644 --- a/README.md +++ b/README.md @@ -9,4 +9,21 @@ mockpi is a tool for faking APIs. it responds to all methods and endpoints, opti "id": 0, "name": "nate" } +``` + + +## Run + +`go run main.go` + +## Build container image + +```shell + docker buildx create --use + + # local + docker buildx build --platform linux/amd64 -t ghcr.io/mockpi:latest . --load + + # push + docker buildx build --platform linux/amd64 -t ghcr.io/mockpi:latest . --push ``` \ No newline at end of file diff --git a/main.go b/main.go index a359f45..839ba13 100644 --- a/main.go +++ b/main.go @@ -27,7 +27,9 @@ func main() { w.Write([]byte(responseBody)) }) - if err := http.ListenAndServe(":8080", nil); err != nil { + listenAddr := ":8080" + fmt.Printf("Starting server on %s...", listenAddr) + if err := http.ListenAndServe(listenAddr, nil); err != nil { fmt.Println("server error: ", err) os.Exit(1) }