Skip to content

Commit

Permalink
feat: deploy Hello World App (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
kumachan-mis authored Jan 7, 2024
1 parent 693c8df commit 05e549b
Show file tree
Hide file tree
Showing 19 changed files with 269 additions and 82 deletions.
3 changes: 2 additions & 1 deletion .env.development
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
APP_PORT=8080
ALLOW_ORIGIN="http://localhost:8080"
TRUSTED_PROXY="localhost"
10 changes: 10 additions & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: Pull Request

on:
pull_request:
branches: ["*"]
jobs:
test:
uses: ./.github/workflows/reusable-test.yml
docstest:
uses: ./.github/workflows/reusable-docstest.yml
13 changes: 13 additions & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Push

on:
push:
branches: [main]
jobs:
test:
uses: ./.github/workflows/reusable-test.yml
docstest:
uses: ./.github/workflows/reusable-docstest.yml
release:
needs: [test, docstest]
uses: ./.github/workflows/reusable-release.yml
25 changes: 25 additions & 0 deletions .github/workflows/reusable-docstest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Docs Test

on:
workflow_call:

jobs:
docstest:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 20.x

- name: Display Node.js version
run: node --version

- name: Install dependencies
run: yarn global add @redocly/cli

- name: Run docs build
run: redocly build-docs docs/openapi/_index.yaml --output tmp/openapi-docs.html
21 changes: 21 additions & 0 deletions .github/workflows/reusable-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Release

on:
workflow_call:

jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Release new version
uses: cycjimmy/semantic-release-action@v4
permissions:
contents: write
issues: write
pull-requests: write
id-token: write
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
31 changes: 31 additions & 0 deletions .github/workflows/reusable-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Test

on:
workflow_call:

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: 1.21.5

- name: Setup dependencies
run: make setup

- name: Run go format
run: make format

- name: Run go lint
run: make lint

- name: Run go build
run: make build

- name: Run go test
run: make test
63 changes: 0 additions & 63 deletions .github/workflows/test.yaml

This file was deleted.

42 changes: 42 additions & 0 deletions .releaserc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"branches": [
{
"name": "release",
"prerelease": false
},
{
"name": "main",
"prerelease": true
}
],
"plugins": [
[
"@semantic-release/commit-analyzer",
{
"releaseRules": [
{
"type": "docs",
"scope": "README",
"release": "patch"
},
{
"type": "refactor",
"release": "patch"
},
{
"type": "style",
"release": "patch"
}
],
"parserOpts": {
"noteKeywords": [
"BREAKING CHANGE",
"BREAKING CHANGES"
]
}
}
],
"@semantic-release/release-notes-generator",
"@semantic-release/github"
]
}
27 changes: 27 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
FROM golang:1.21-alpine AS builder

WORKDIR /app
COPY cmd/ cmd/
COPY internal/ internal/
COPY go.sum go.sum
COPY go.mod go.mod

RUN go build -o app cmd/app/main.go


FROM scratch AS runner

ARG ENVIRONMENT
ARG ALLOW_ORIGIN
ARG TRUSTED_PROXY

ENV GIN_MODE=release \
ENVIRONMENT=$ENVIRONMENT \
ALLOW_ORIGIN=$ALLOW_ORIGIN \
TRUSTED_PROXY=$TRUSTED_PROXY

COPY --from=builder /app/app /app/app

EXPOSE 8080

ENTRYPOINT ["/app/app"]
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ OPEN_API_GO_GENERATOR := go-gin-server
OPEN_API_GO_PACKAGE := model
OPEN_API_GO_DST := interal/${OPEN_API_GO_PACKAGE}

OPEN_API_NODE_GENERATOR := typescript-fetch
OPEN_API_NODE_DST := src/openapi
OPEN_API_NODE_GENERATOR := typescript-fetch
OPEN_API_NODE_DST := src/openapi

setup:
cp .pre-commit .git/hooks/pre-commit
Expand Down
38 changes: 38 additions & 0 deletions cloudbuild.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
steps:
- name: "gcr.io/cloud-builders/docker"
args:
[
"build",
"-t",
"gcr.io/$PROJECT_ID/$_SERVICE_NAME:$TAG_NAME",
"--build-arg",
"ALLOW_ORIGIN=$_ALLOW_ORIGIN",
"--build-arg",
"TRUSTED_PROXY=$_TRUSTED_PROXY",
"--build-arg",
"ENVIRONMENT=$_ENVIRONMENT",
".",
]

- name: "gcr.io/cloud-builders/docker"
args: ["push", "gcr.io/$PROJECT_ID/$_SERVICE_NAME:$TAG_NAME"]

- name: "gcr.io/google.com/cloudsdktool/cloud-sdk"
entrypoint: gcloud
args:
[
"run",
"deploy",
"$_SERVICE_NAME",
"--image",
"gcr.io/$PROJECT_ID/$_SERVICE_NAME:$TAG_NAME",
"--region",
"$_REGION",
]

images:
- "gcr.io/$PROJECT_ID/$_SERVICE_NAME:$TAG_NAME"

substitutions:
_SERVICE_NAME: knodeledge-api
_REGION: us-central1
11 changes: 9 additions & 2 deletions cmd/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"fmt"
"os"

"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/kumachan-mis/knodeledge-api/interal/api"
"github.com/kumachan-mis/knodeledge-api/internal/api"
"github.com/subosito/gotenv"
)

Expand All @@ -18,6 +19,12 @@ func main() {
gotenv.Load(fmt.Sprintf(".env.%s", env))

router := gin.Default()
router.SetTrustedProxies([]string{os.Getenv("TRUSTED_PROXY")})

config := cors.DefaultConfig()
config.AllowOrigins = []string{os.Getenv("ALLOW_ORIGIN")}
router.Use(cors.New(config))

router.GET("/", func(cxt *gin.Context) {
cxt.JSON(200, gin.H{
"environment": env,
Expand All @@ -28,5 +35,5 @@ func main() {

router.POST("/api/hello-world", api.HelloWorldHandler)

router.Run(fmt.Sprintf(":%s", os.Getenv("APP_PORT")))
router.Run(":8080")
}
26 changes: 14 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,37 @@ go 1.21.5

require (
github.com/gin-gonic/gin v1.9.1
github.com/stretchr/testify v1.8.3
github.com/stretchr/testify v1.8.4
github.com/subosito/gotenv v1.6.0
)

require (
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/bytedance/sonic v1.10.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/cors v1.5.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/go-playground/validator/v10 v10.15.5 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.12.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
golang.org/x/arch v0.5.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/net v0.16.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 05e549b

Please sign in to comment.