From 3888c47d176ff4cd4d96644c98ebbdbe5e500ad7 Mon Sep 17 00:00:00 2001 From: maximo tejeda Date: Tue, 3 Dec 2024 22:09:27 -0400 Subject: [PATCH] Restructured workflow added --- .gitea/workflows/demo.yaml | 19 ---- .github/workflows/image_creation.yml | 106 ++++++++++++++++++ .github/workflows/test_project.yml | 68 +++++++++++ Makefile | 2 +- cmd/api/main.go | 6 +- .../adapters}/dolar/dolar.go | 2 +- {domain => internal/domain}/history.go | 0 {handlers => internal/handlers}/api/api.go | 4 +- .../handlers}/api/constrols.go | 0 .../handlers}/api/institution.go | 4 +- {handlers => internal/handlers}/app/app.go | 0 {handlers => internal/handlers}/handlers.go | 4 +- {helpers => internal/helpers}/timehelpers.go | 0 {middlewares => internal/middlewares}/logs.go | 0 .../middlewares}/middlewares.go | 0 {ports => internal/ports}/dolar.go | 2 +- k8s/deployment.yml.template | 47 ++++---- 17 files changed, 206 insertions(+), 58 deletions(-) delete mode 100644 .gitea/workflows/demo.yaml create mode 100644 .github/workflows/image_creation.yml create mode 100644 .github/workflows/test_project.yml rename {adapters => internal/adapters}/dolar/dolar.go (98%) rename {domain => internal/domain}/history.go (100%) rename {handlers => internal/handlers}/api/api.go (92%) rename {handlers => internal/handlers}/api/constrols.go (100%) rename {handlers => internal/handlers}/api/institution.go (98%) rename {handlers => internal/handlers}/app/app.go (100%) rename {handlers => internal/handlers}/handlers.go (93%) rename {helpers => internal/helpers}/timehelpers.go (100%) rename {middlewares => internal/middlewares}/logs.go (100%) rename {middlewares => internal/middlewares}/middlewares.go (100%) rename {ports => internal/ports}/dolar.go (85%) diff --git a/.gitea/workflows/demo.yaml b/.gitea/workflows/demo.yaml deleted file mode 100644 index e942159..0000000 --- a/.gitea/workflows/demo.yaml +++ /dev/null @@ -1,19 +0,0 @@ -name: Gitea Actions Demo -run-name: ${{ gitea.actor }} is testing out Gitea Actions 🚀 -on: [push] - -jobs: - Explore-Gitea-Actions: - runs-on: ubuntu-latest - steps: - - run: echo "🎉 The job was automatically and automagically triggered by a ${{ gitea.event_name }} event." - - run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by Gitea!" - - run: echo "🔎 The name of your branch is ${{ gitea.ref }} and your repository is ${{ gitea.repository }}." - - name: Check out repository code - uses: actions/checkout@v4 - - run: echo "💡 The ${{ gitea.repository }} repository has been cloned to the runner." - - run: echo "🖥️ The workflow is now ready to test your code on the runner." - - name: List files in the repository - run: | - ls ${{ gitea.workspace }} - - run: echo "🍏 This job's status is ${{ job.status }}." diff --git a/.github/workflows/image_creation.yml b/.github/workflows/image_creation.yml new file mode 100644 index 0000000..896f256 --- /dev/null +++ b/.github/workflows/image_creation.yml @@ -0,0 +1,106 @@ +name: ${{ github.actor }} executed Build Push Prod +on: + push: + tags: + - 'v[0-9]+\.[0-9]+\.[0-9]+' +jobs: + build: + runs-on: ubuntu-latest + env: + TOKEN: ${{ secrets.TOKEN }} + USERNAME: ${{ vars.USERNAME }} + TAG_VERSION: ${{ github.ref_name }} + steps: + - name: echo repo dir + id: repo-url + run: | + echo "repourl=$(echo ${{ github.server_url }} | sed -e 's;https://;;')" >> $GITHUB_OUTPUT + + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup Quemu + uses: docker/setup-qemu-action@v3 + + - name: Set Up BuildX + uses: docker/setup-buildx-action@v3 + + - name: Login to Docker + uses: docker/login-action@v3 + with: + registry: https://git.maximotejeda.com + username: ${{ vars.USERNAME }} + password: ${{ secrets.PWD }} + + - name: calculate short sha + id: calculate-sha + run: | + echo "shortsha=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT + + - name: Build and push + uses: docker/build-push-action@v6 + with: + context: . + platforms: linux/amd64,linux/arm64 + push: true + tags: "${{ steps.repo-url.outputs.repourl }}/${{ github.repository }}:${{ github.ref_name }}" + build-args: | + version=${{ github.ref_name }} + SHORTSHA=${{ steps.calculate-sha.outputs.shortsha }} + + deploy: + runs-on: ubuntu-latest + needs: build + env: + TOKEN: ${{ secrets.TOKEN }} + TAG: ${{ github.ref_name }} + PASSWORD: $PWD + USERNAME: ${{ vars.USERNAME }} + TAG_VERSION: ${{ github.ref_name }} + NATS_SERVICE_URL: "${{ vars.NATS_SERVICE_URL }}" + ENV: "{{ vars.ENV }}" + APPLICATION_PORT: "${{ vars.APPLICATION_PORT }}" + APPLICATION_HOST: "${{ vars.APPLICATION_HOST }}" + DOLLAR_SERVICE_URL: "${{ vars.DOLLAR_SERVICE_URL }}" + steps: + - name: echo repo dir + id: repo-url + run: | + echo "repourl=$(echo ${{ github.server_url }} | sed -e 's;https://;;')" >> $GITHUB_OUTPUT + echo "repoName=$(echo ${{ github.repository }} | sed -e 's;${{ github.actor }}/;;')" >> $GITHUB_OUTPUT + echo "${{ github.repository }} | sed -e 's;${{ github.actor }}/;;')" + - name: Checkout code + uses: actions/checkout@v4 + + - name: install envsubst + run: | + apt update + apt install -y gettext-base + apt clean + + - name: create deployment files + env: + IMAGE: "${{ steps.repo-url.outputs.repourl }}/${{ github.repository }}:${{ github.ref_name }}" + REPONAME: "${{ steps.repo-url.outputs.repoName }}" + run: | + envsubst < k8s/deployment.yml.template > k8s/deployment.yml + - name: Download kubectl + run: curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" + + - name: install kubectl + run: sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl + + - name: insert kube config + run: | + mkdir ~/.kube + export IMAGE="$( echo ${{ github.server_url }} | sed -e 's;https://;;')/${{ github.repository }}:${{ github.ref_name }}" + echo "${{ secrets.KUBECONFIG }}" > ~/.kube/config + - name: check pod + run: kubectl apply -f k8s/deployment.yml + + - name: scale down deployment + continue-on-error: true + run: kubectl scale deployment ${{ steps.repo-url.outputs.repoName }} --replicas=0 + + - name: scale up deployment + run: kubectl scale deployment ${{ steps.repo-url.outputs.repoName }} --replicas=1 diff --git a/.github/workflows/test_project.yml b/.github/workflows/test_project.yml new file mode 100644 index 0000000..5c61a57 --- /dev/null +++ b/.github/workflows/test_project.yml @@ -0,0 +1,68 @@ +name: "dev test" +on: + push: + branches: + - master + - dev + - 'feature/**' + paths: + - '**.go' + - '**.yml' + - '**.yaml' +jobs: + test: + runs-on: ubuntu-latest + env: + TOKEN: ${{ secrets.TOKEN }} + USERNAME: ${{ vars.USERNAME }} + TAG_VERSION: ${{ github.ref_name }} + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup Go + uses: actions/setup-go@v5 + with: + go-version-file: 'go.mod' + - name: run tidy + run: | + go mod tidy + - name: test with go + run: go test -v ./... + + vulnCheck: + runs-on: ubuntu-latest + needs: test + env: + TOKEN: ${{ secrets.TOKEN }} + USERNAME: ${{ vars.USERNAME }} + TAG_VERSION: ${{ github.ref_name }} + steps: + - name: checkout Code + uses: actions/checkout@v4 + + - name: go vuln check + uses: golang/govulncheck-action@v1 + with: + go-version-file: 'go.mod' + go-package: ./... + + Ci-Lint: + runs-on: ubuntu-latest + needs: vulnCheck + env: + TOKEN: ${{ secrets.TOKEN }} + USERNAME: ${{ vars.USERNAME }} + TAG_VERSION: ${{ github.ref_name }} + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup Go + uses: actions/setup-go@v5 + with: + go-version-file: 'go.mod' + - name: CI lint Check + uses: golangci/golangci-lint-action@v6 + with: + only-new-issues: true diff --git a/Makefile b/Makefile index 55d0c80..44dd525 100644 --- a/Makefile +++ b/Makefile @@ -38,7 +38,7 @@ run-local:clean build @bin/$(BINAME) build: - @mkdir dolardb || true +# @mkdir dolardb || true @env GOOS=$(OS) GOARCH=$(arch) go build -o ./bin/$(BINAME) ./cmd/api/. @env GOOS=$(OS) GOARCH=arm64 go build -o ./bin/$(BINAMEARM) ./cmd/api/. diff --git a/cmd/api/main.go b/cmd/api/main.go index 7c6718b..f8e302f 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -8,10 +8,10 @@ import ( "os" "path/filepath" - "git.maximotejeda.com/maximo/us-dop-api/adapters/dolar" + "git.maximotejeda.com/maximo/us-dop-api/internal/adapters/dolar" "git.maximotejeda.com/maximo/us-dop-api/config" - "git.maximotejeda.com/maximo/us-dop-api/handlers" - "git.maximotejeda.com/maximo/us-dop-api/middlewares" + "git.maximotejeda.com/maximo/us-dop-api/internal/handlers" + "git.maximotejeda.com/maximo/us-dop-api/internal/middlewares" "git.maximotejeda.com/maximo/us-dop-api/static" "google.golang.org/grpc" diff --git a/adapters/dolar/dolar.go b/internal/adapters/dolar/dolar.go similarity index 98% rename from adapters/dolar/dolar.go rename to internal/adapters/dolar/dolar.go index af82f17..7730f03 100644 --- a/adapters/dolar/dolar.go +++ b/internal/adapters/dolar/dolar.go @@ -5,7 +5,7 @@ import ( "time" "git.maximotejeda.com/maximo/dolar/proto/golang/dolar" - "git.maximotejeda.com/maximo/us-dop-api/domain" + "git.maximotejeda.com/maximo/us-dop-api/internal/domain" "google.golang.org/grpc" ) diff --git a/domain/history.go b/internal/domain/history.go similarity index 100% rename from domain/history.go rename to internal/domain/history.go diff --git a/handlers/api/api.go b/internal/handlers/api/api.go similarity index 92% rename from handlers/api/api.go rename to internal/handlers/api/api.go index e9eb331..54a409a 100644 --- a/handlers/api/api.go +++ b/internal/handlers/api/api.go @@ -6,8 +6,8 @@ import ( "net/http" "time" - "git.maximotejeda.com/maximo/us-dop-api/domain" - "git.maximotejeda.com/maximo/us-dop-api/ports" + "git.maximotejeda.com/maximo/us-dop-api/internal/domain" + "git.maximotejeda.com/maximo/us-dop-api/internal/ports" ) diff --git a/handlers/api/constrols.go b/internal/handlers/api/constrols.go similarity index 100% rename from handlers/api/constrols.go rename to internal/handlers/api/constrols.go diff --git a/handlers/api/institution.go b/internal/handlers/api/institution.go similarity index 98% rename from handlers/api/institution.go rename to internal/handlers/api/institution.go index a8f1e49..4d563a4 100644 --- a/handlers/api/institution.go +++ b/internal/handlers/api/institution.go @@ -9,8 +9,8 @@ import ( "strings" "time" - "git.maximotejeda.com/maximo/us-dop-api/domain" - "git.maximotejeda.com/maximo/us-dop-api/helpers" + "git.maximotejeda.com/maximo/us-dop-api/internal/domain" + "git.maximotejeda.com/maximo/us-dop-api/internal/helpers" ) // handler to parse query to the api diff --git a/handlers/app/app.go b/internal/handlers/app/app.go similarity index 100% rename from handlers/app/app.go rename to internal/handlers/app/app.go diff --git a/handlers/handlers.go b/internal/handlers/handlers.go similarity index 93% rename from handlers/handlers.go rename to internal/handlers/handlers.go index a022544..47a4b0d 100644 --- a/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -7,8 +7,8 @@ import ( "strings" "time" - "git.maximotejeda.com/maximo/us-dop-api/handlers/api" - "git.maximotejeda.com/maximo/us-dop-api/ports" + "git.maximotejeda.com/maximo/us-dop-api/internal/handlers/api" + "git.maximotejeda.com/maximo/us-dop-api/internal/ports" ) var ( diff --git a/helpers/timehelpers.go b/internal/helpers/timehelpers.go similarity index 100% rename from helpers/timehelpers.go rename to internal/helpers/timehelpers.go diff --git a/middlewares/logs.go b/internal/middlewares/logs.go similarity index 100% rename from middlewares/logs.go rename to internal/middlewares/logs.go diff --git a/middlewares/middlewares.go b/internal/middlewares/middlewares.go similarity index 100% rename from middlewares/middlewares.go rename to internal/middlewares/middlewares.go diff --git a/ports/dolar.go b/internal/ports/dolar.go similarity index 85% rename from ports/dolar.go rename to internal/ports/dolar.go index f52cbff..5132e1a 100644 --- a/ports/dolar.go +++ b/internal/ports/dolar.go @@ -1,6 +1,6 @@ package ports -import "git.maximotejeda.com/maximo/us-dop-api/domain" +import "git.maximotejeda.com/maximo/us-dop-api/internal/domain" type DolarService interface { GetLatest(name string) (*domain.History, error) diff --git a/k8s/deployment.yml.template b/k8s/deployment.yml.template index 8f04a28..632f64d 100644 --- a/k8s/deployment.yml.template +++ b/k8s/deployment.yml.template @@ -1,51 +1,44 @@ apiVersion: apps/v1 kind: Deployment metadata: - name: us-dop-api + name: $REPONAME labels: - app: us-dop-api + app: $REPONAME spec: replicas: 1 selector: matchLabels: - app: us-dop-api + app: $REPONAME template: metadata: labels: - app: us-dop-api - name: us-dop-api + app: $REPONAME + name: $REPONAME spec: containers: - - name: us-dop-pi - image: localhost:32000/us-dop-api:latest + - name: $REPONAME + image: $IMAGE env: - name: DOLLAR_SERVICE_URL - value: "dolar-grpc-svc:80" + value: "$DOLLAR_SERVICE_URL" - name: HOST - value: "0.0.0.0" + value: "$HOST" - name: NATSURI - value: "nats://nats-svc:4222" + value: "$NATS_SERVICE_URL" - name: PORT - value: "8080" - volumeMounts: - - name: database - mountPath: /app/dolardb - volumes: - - name: database - persistentVolumeClaim: - claimName: bank-crawler-pvc + value: "$PORT" --- apiVersion: v1 kind: Service metadata: - name: us-dop-api-svc + name: $REPONAME-svc spec: type: LoadBalancer selector: - app: us-dop-api + app: $REPONAME ports: - port: 80 - targetPort: 8080 + targetPort: $PORT name: frontend --- apiVersion: networking.k8s.io/v1 @@ -54,7 +47,7 @@ metadata: annotations: # add an annotation indicating the issuer to use. cert-manager.io/cluster-issuer: letsencrypt - name: us-dop-api-ingress + name: $REPONAME-ingress spec: rules: - host: cambio.maximotejeda.com @@ -64,10 +57,10 @@ spec: path: "/" backend: service: - name: us-dop-api-svc + name: $REPONAME-svc port: number: 80 - tls: # < placing a host in the TLS config will determine what ends up in the cert's subjectAltNames - - hosts: - - cambio.maximotejeda.com - secretName: cambio.maximotejeda-cert # < cert-manager will store the created certificate in this secret. + #tls: # < placing a host in the TLS config will determine what ends up in the cert's subjectAltNames + #- hosts: + # - cambio.maximotejeda.com + # secretName: cambio.maximotejeda-cert # < cert-manager will store the created certificate in this secret.