diff --git a/.dockerignore b/.dockerignore index 2a11556..bfecd47 100644 --- a/.dockerignore +++ b/.dockerignore @@ -2,12 +2,12 @@ .dockerignore .env -.env.example +.env.* .git .gitignore Dockerfile -Dockerfile.dev +Dockerfile.* docker-compose.yml log/* @@ -17,3 +17,8 @@ tmp/* !tmp/.keep .air.conf + +.github +.vscode + +README.md diff --git a/.github/workflows/essential_checks.yml b/.github/workflows/ci.yml similarity index 53% rename from .github/workflows/essential_checks.yml rename to .github/workflows/ci.yml index 839f539..f2166f7 100644 --- a/.github/workflows/essential_checks.yml +++ b/.github/workflows/ci.yml @@ -1,4 +1,4 @@ -name: Essential checks +name: CI on: push: branches: @@ -15,24 +15,31 @@ jobs: - name: Run GolangCI-Lint Action uses: actions-contrib/golangci-lint@v1 with: - args: run --exclude-use-default + args: run ./... --exclude-use-default test: runs-on: ubuntu-latest steps: - name: Checkout current branch uses: actions/checkout@master - - name: docker-compose version - run: docker-compose --version - - name: Build docker image + - uses: whoan/docker-build-with-cache-action@v5 + with: + username: vaihtovirta + password: "${{ secrets.GITHUB_TOKEN }}" + registry: docker.pkg.github.com + image_name: timecodes-api_base + push_image_and_stages: false + pull_image_and_stages: false + - name: Build docker image for tests run: | cp .env.example .env - docker-compose build app_test + echo ${{ secrets.GITHUB_TOKEN }} | docker login docker.pkg.github.com -u vaihtovirta --password-stdin + docker-compose build --pull app_test - name: Run tests - run: docker-compose run app_test go test ./... -covermode=count -coverprofile=coverage.out + run: docker-compose run --rm app_test go test ./... -covermode=count -coverprofile=tmp/coverage.out - name: Convert coverage to lcov uses: jandelgado/gcov2lcov-action@v1.0.2 with: - infile: coverage.out + infile: tmp/coverage.out outfile: coverage.lcov - name: Coveralls uses: coverallsapp/github-action@master diff --git a/.github/workflows/push_image.yml b/.github/workflows/push_image.yml new file mode 100644 index 0000000..f4be72c --- /dev/null +++ b/.github/workflows/push_image.yml @@ -0,0 +1,19 @@ +name: Build and push base image +on: + push: + branches: + - master +jobs: + push_image: + runs-on: ubuntu-latest + steps: + - name: Checkout current branch + uses: actions/checkout@master + - name: Build base docker image + run: | + cp .env.example .env + docker login docker.pkg.github.com -u vaihtovirta -p ${{ secrets.GITHUB_TOKEN }} + docker-compose build --pull app_base + - name: Push docker image to Github registry + run: | + docker push docker.pkg.github.com/letscode-io/timecodes-api/timecodes-api_base:latest diff --git a/Dockerfile b/Dockerfile index 4d79753..d06d3d1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:alpine AS builder +FROM docker.pkg.github.com/letscode-io/timecodes-api/timecodes-api_base:latest AS base ENV APP_HOME=/usr/src/app @@ -25,12 +25,8 @@ FROM alpine AS final ENV APP_HOME=/usr/src/app -RUN apk update --purge \ - && apk upgrade --purge \ - && rm -rf /var/cache/apk/* - WORKDIR $APP_HOME -COPY --from=builder $APP_HOME/application $APP_HOME/ +COPY --from=base $APP_HOME/application $APP_HOME/ ENTRYPOINT ["./application"] diff --git a/Dockerfile.base b/Dockerfile.base new file mode 100644 index 0000000..fd8d9ad --- /dev/null +++ b/Dockerfile.base @@ -0,0 +1,18 @@ +FROM golang:alpine + +ENV APP_HOME=/usr/src/app + +RUN apk update \ + && apk upgrade \ + && apk add --upgrade git \ + && rm -rf /var/cache/apk/* + +RUN mkdir -p $APP_HOME + +WORKDIR $APP_HOME + +COPY go.mod go.sum $APP_HOME/ + +RUN go mod download + +COPY . $APP_HOME diff --git a/docker-compose.yml b/docker-compose.yml index cd6afd3..9a676c7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,21 +1,24 @@ version: "3.5" services: + app_base: + image: docker.pkg.github.com/letscode-io/timecodes-api/timecodes-api_base:latest + build: + context: . + dockerfile: Dockerfile.base app_test: build: context: . dockerfile: Dockerfile - target: builder + target: base depends_on: - db environment: - CGO_ENABLED=0 env_file: - .env.test - ports: - - "8080:8080" volumes: - - .:/usr/src/app + - ./tmp:/usr/src/app/tmp app_dev: build: context: . diff --git a/go.mod b/go.mod index ce61a54..7e47db3 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/jinzhu/gorm v1.9.12 github.com/khaiql/dbcleaner v2.3.0+incompatible github.com/rs/cors v1.7.0 - github.com/stretchr/testify v1.6.0 + github.com/stretchr/testify v1.6.1 google.golang.org/api v0.25.0 gopkg.in/khaiql/dbcleaner.v2 v2.3.0 ) diff --git a/go.sum b/go.sum index f247baf..1b5d15d 100644 --- a/go.sum +++ b/go.sum @@ -124,10 +124,8 @@ github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= -github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= -github.com/stretchr/testify v1.6.0 h1:jlIyCplCJFULU/01vCkhKuTyc3OorI3bJFuw6obfgho= -github.com/stretchr/testify v1.6.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= go.opencensus.io v0.21.0 h1:mU6zScU4U1YAFPHEHYk+3JC4SY7JxgkqS10ZOSyksNg= go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= @@ -280,10 +278,10 @@ google.golang.org/api v0.15.0/go.mod h1:iLdEw5Ide6rF15KTC1Kkl0iskquN2gFfn9o9XIsb google.golang.org/api v0.17.0/go.mod h1:BwFmGc8tA3vsd7r/7kR8DY7iEEGSU04BFxCo5jP/sfE= google.golang.org/api v0.18.0/go.mod h1:BwFmGc8tA3vsd7r/7kR8DY7iEEGSU04BFxCo5jP/sfE= google.golang.org/api v0.20.0/go.mod h1:BwFmGc8tA3vsd7r/7kR8DY7iEEGSU04BFxCo5jP/sfE= -google.golang.org/api v0.24.0 h1:cG03eaksBzhfSIk7JRGctfp3lanklcOM/mTGvow7BbQ= -google.golang.org/api v0.24.0/go.mod h1:lIXQywCXRcnZPGlsd8NbLnOjtAoL6em04bJ9+z0MncE= google.golang.org/api v0.25.0 h1:LodzhlzZEUfhXzNUMIfVlf9Gr6Ua5MMtoFWh7+f47qA= google.golang.org/api v0.25.0/go.mod h1:lIXQywCXRcnZPGlsd8NbLnOjtAoL6em04bJ9+z0MncE= +google.golang.org/api v0.26.0 h1:VJZ8h6E8ip82FRpQl848c5vAadxlTXrUh8RzQzSRm08= +google.golang.org/api v0.26.0/go.mod h1:lIXQywCXRcnZPGlsd8NbLnOjtAoL6em04bJ9+z0MncE= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=