Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Commit

Permalink
feat(make): build stable images for test deps
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbes committed Aug 23, 2024
1 parent 411e5f1 commit 785ab01
Show file tree
Hide file tree
Showing 14 changed files with 233 additions and 335 deletions.
43 changes: 38 additions & 5 deletions .github/workflows/build-and-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,42 @@ jobs:
version: "latest"
install-go: false

test-containers:
permissions:
contents: read
id-token: write
runs-on: ubuntu-latest
steps:
- id: auth
name: Authenticate with Google Cloud
uses: google-github-actions/auth@v2
with:
token_format: access_token
workload_identity_provider: projects/1007056531311/locations/global/workloadIdentityPools/ci-nada-prod/providers/ci-nada-prod
service_account: [email protected]

- uses: docker/login-action@v3
name: Login to Google Artifact Registry
with:
registry: europe-north1-docker.pkg.dev
username: oauth2accesstoken
password: ${{ steps.auth.outputs.access_token }}

- name: Build and push all images
run: make build-push-all

tests:
permissions:
contents: read
runs-on: ubuntu-latest
needs:
- test-containers
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
- run: CGO_ENABLED=1 CXX=clang++ CC=clang CGO_CXXFLAGS=-Wno-everything CGO_LDFLAGS=-Wno-everything go test -timeout 20m -race -coverprofile=coverage.txt -covermode=atomic -v ./...
- run: make test
- name: Upload coverage reports to Codecov
uses: codecov/[email protected]
with:
Expand All @@ -81,7 +107,7 @@ jobs:
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
- run: make linux-build
- run: make release
- uses: actions/upload-artifact@v4
with:
name: nada-backend
Expand All @@ -92,7 +118,11 @@ jobs:
contents: write
id-token: write
runs-on: ubuntu-latest
needs: [build,golangci,tests,staticchecks]
needs:
- build
- golangci
- tests
- staticchecks
if: github.actor != 'dependabot[bot]'
outputs:
image: ${{ steps.docker-build-push.outputs.image }}
Expand All @@ -113,7 +143,8 @@ jobs:
deploy-dev:
name: Deploy dev gcp
runs-on: ubuntu-20.04
needs: [push]
needs:
- push
if: github.actor != 'dependabot[bot]'
steps:
- uses: actions/checkout@v4
Expand All @@ -128,7 +159,9 @@ jobs:
deploy-prod:
name: Deploy prod gcp
runs-on: ubuntu-20.04
needs: [push, deploy-dev]
needs:
- push
- deploy-dev
if: github.ref == 'refs/heads/main' && inputs.environment != 'dev'|| inputs.environment == 'prod'
steps:
- uses: actions/checkout@v4
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ hack/.ipynb_checkpoints/
.tool-versions
.local/
/build-output
/coverage.txt
!resources/metabase/fake/fake-metabase-sa.json
!resources/naisconsole/fake-response.json
!resources/teamkatalogen/fake-data.json
59 changes: 0 additions & 59 deletions Dockerfile-metabase-local

This file was deleted.

7 changes: 0 additions & 7 deletions Dockerfile-metabase-orig

This file was deleted.

180 changes: 110 additions & 70 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ DATE = $(shell date "+%Y-%m-%d")
LAST_COMMIT = $(shell git --no-pager log -1 --pretty=%h)
VERSION ?= $(DATE)-$(LAST_COMMIT)
LDFLAGS := -X github.com/navikt/nada-backend/backend/version.Revision=$(shell git rev-parse --short HEAD) -X github.com/navikt/nada-backend/backend/version.Version=$(VERSION)

METABASE_VERSION := v1.50.20
MOCKS_VERSION := v0.0.1

TARGET_ARCH := amd64
TARGET_OS := linux

IMAGE_URL := europe-north1-docker.pkg.dev
IMAGE_REPOSITORY := nada-prod-6977/nada-north

COMPOSE_DEPS_FULLY_LOCAL := db adminer gcs metabase-patched bq tk nc
COMPOS_DEPS_ONLINE_LOCAL := db adminer gcs metabase

APP = nada-backend

# A template function for installing binaries
Expand Down Expand Up @@ -74,19 +87,43 @@ endif
-include .env

test:
CGO_ENABLED=1 CXX=clang++ CC=clang CGO_CXXFLAGS=-Wno-everything CGO_LDFLAGS=-Wno-everything $(GO) test -timeout 20m -race ./...
METABASE_VERSION=$(METABASE_VERSION) CGO_ENABLED=1 CXX=clang++ CC=clang \
CGO_CXXFLAGS=-Wno-everything CGO_LDFLAGS=-Wno-everything \
go test -timeout 20m -race -coverprofile=coverage.txt -covermode=atomic -v ./...
.PHONY: test

build: $(RELEASE_DIR)
@echo "Building cmd applications..."
staticcheck: $(STATICCHECK)
$(STATICCHECK) ./...

gofumpt: $(GOFUMPT)
$(GOFUMPT) -w .

lint: $(GOLANGCILINT)
$(GOLANGCILINT) run
.PHONY: lint

check: | gofumpt lint staticcheck test
.PHONY: check

compile: $(RELEASE_DIR)
@echo "Compiling cmd applications..."
@CGO_ENABLED=1 CXX=clang++ CC=clang $(GO) mod tidy
@for d in cmd/*; do \
app=$$(basename $$d); \
echo "Building $$app..."; \
echo "Compiling $$app..."; \
CGO_ENABLED=1 CXX=clang++ CC=clang CGO_CXXFLAGS=-Wno-everything CGO_LDFLAGS=-Wno-everything $(GO) build -o $(RELEASE_DIR)/$$app ./$$d; \
done
@echo "Build complete. Binaries are located in $(RELEASE_DIR)"
.PHONY: build
@echo "Compile complete. Binaries are located in $(RELEASE_DIR)"
.PHONY: compile

generate: $(SQLC)
cd pkg && $(SQLC) generate
.PHONY: generate

release:
GOOS=linux GOARCH=amd64 CGO_EMABLED=0 $(GO) build -o $(APP) \
-ldflags '-linkmode "external" -extldflags "-static" -w -s $(LDFLAGS)' ./cmd/nada-backend/main.go
.PHONY: release

env:
@echo "Re-creating .env file..."
Expand All @@ -111,78 +148,81 @@ test-sa:
metabase-sa:
@echo "Fetching metabase service account credentials..."
$(shell kubectl get --context=dev-gcp --namespace=nada secret/metabase-google-sa -o json | jq -r '.data."meta_creds.json"' | base64 -d > test-metabase-sa.json)
.PHONY: test-sa
.PHONY: metabase-sa

setup-metabase:
./resources/scripts/configure_metabase.sh
.PHONY: setup-metabase

local-with-auth: | env test-sa metabase-sa docker-build-metabase docker-compose-up setup-metabase
run-online: | env test-sa metabase-sa docker-build-metabase docker-compose-up setup-metabase
@echo "Sourcing environment variables..."
set -a && source ./.env && set +a && \
STORAGE_EMULATOR_HOST=http://localhost:8082/storage/v1/ $(GO) run ./cmd/nada-backend --config ./config-local-online.yaml
.PHONY: local-with-auth
.PHONY: run-online

local: | env test-sa setup-metabase
start-run-online-deps: | docker-login
@echo "Starting dependencies with docker compose... (online)"
@echo "Mocks version: $(MOCKS_VERSION)"
@echo "Metabase version: $(METABASE_VERSION)"
MOCKS_VERSION=$(MOCKS_VERSION) METABASE_VERSION=$(METABASE_VERSION) $(DOCKER_COMPOSE ) up -d $(COMPOS_DEPS_ONLINE_LOCAL)
.PHONY: start-run-online-deps

run: | start-run-deps env test-sa setup-metabase
@echo "Sourcing environment variables..."
set -a && source ./.env && set +a && \
GOOGLE_CLOUD_PROJECT=test STORAGE_EMULATOR_HOST=http://localhost:8082/storage/v1/ $(GO) run ./cmd/nada-backend --config ./config-local.yaml
.PHONY: local

local-deps: | docker-build-metabase-local-bq docker-build-apps docker-compose-up-fg
.PHONY: local-deps

docker-compose-up-fg:
@echo "Starting dependencies with docker compose..."
$(DOCKER_COMPOSE) up

docker-compose-up:
@echo "Starting dependencies with docker compose..."
$(DOCKER_COMPOSE ) up -d

migrate:
$(GO) run github.com/pressly/goose/v3/cmd/goose -dir ./pkg/database/migrations postgres "user=nada-backend dbname=nada sslmode=disable password=postgres" up
.PHONY: migrate

generate-sql: $(SQLC)
cd pkg && $(SQLC) generate
.PHONY: generate-sql

generate: generate-sql
.PHONY: generate

linux-build:
GOOS=linux GOARCH=amd64 CGO_EMABLED=0 $(GO) build -o $(APP) -ldflags '-linkmode "external" -extldflags "-static" -w -s $(LDFLAGS)' ./cmd/nada-backend/main.go
.PHONY: linux-build

docker-build-metabase:
@echo "Building metabase docker image..."
docker image build -t metabase-nada-backend:latest -f Dockerfile-metabase-orig .

docker-build-metabase-local-bq:
@echo "Building metabase docker image with local BigQuery..."
docker image build -t metabase-nada-backend:latest -f Dockerfile-metabase-local .

docker-build-apps:
docker image build -t nada-apps:latest -f Dockerfile-build .

docker-build:
docker image build -t ghcr.io/navikt/$(APP):$(VERSION) -t ghcr.io/navikt/$(APP):latest .
.PHONY: docker-build

docker-push:
docker image push ghcr.io/navikt/$(APP):$(VERSION)
docker image push ghcr.io/navikt/$(APP):latest
.PHONY: docker-push

staticcheck: $(STATICCHECK)
$(STATICCHECK) ./...

gofumpt: $(GOFUMPT)
$(GOFUMPT) -w .

lint: $(GOLANGCILINT)
$(GOLANGCILINT) run
.PHONY: lint

check: | gofumpt lint staticcheck test
.PHONY: check
.PHONY: run

start-run-deps: | docker-login
@echo "Starting dependencies with docker compose... (fully local)"
@echo "Mocks version: $(MOCKS_VERSION)"
@echo "Metabase version: $(METABASE_VERSION)"
MOCKS_VERSION=$(MOCKS_VERSION) METABASE_VERSION=$(METABASE_VERSION) $(DOCKER_COMPOSE ) up -d $(COMPOSE_DEPS_FULLY_LOCAL)
.PHONY: start-run-deps

docker-login:
@echo "Logging in to Google Cloud..."
gcloud auth configure-docker $(IMAGE_URL)
.PHONY: docker-login

build-push-all: | build-all push-all
.PHONY: build-push-all

build-all: | build-metabase build-metabase-patched build-deps
.PHONY: build-all

build-metabase:
@echo "Building original metabase docker image, for version: $(METABASE_VERSION)"
docker image build --platform $(TARGET_OS)/$(TARGET_ARCH) --tag $(IMAGE_URL)/$(IMAGE_REPOSITORY)/metabase:$(METABASE_VERSION) \
--build-arg METABASE_VERSION=$(METABASE_VERSION) --file resources/images/metabase/Dockerfile .
.PHONY: build-metabase

build-metabase-patched:
@echo "Building patched metabase docker image, for version: $(METABASE_VERSION)"
docker image build --platform $(TARGET_OS)/$(TARGET_ARCH) --tag $(IMAGE_URL)/$(IMAGE_REPOSITORY)/metabase-patched:$(METABASE_VERSION) \
--build-arg METABASE_VERSION=$(METABASE_VERSION) --file resources/images/metabase/Dockerfile-bq-patch .
.PHONY: build-metabase-patched

build-deps: build-metabase-patched
@echo "Building nada-backend mocks..."
docker image build --platform $(TARGET_OS)/$(TARGET_ARCH) --tag $(IMAGE_URL)/$(IMAGE_REPOSITORY)/nada-backend-mocks:$(MOCKS_VERSION) \
--file resources/images/nada-backend/Dockerfile-mocks .
.PHONY: build-deps

push-all: | docker-login build-all push-metabase push-metabase-patched push-deps
.PHONY: push-all

push-metabase:
@echo "Pushing metabase docker image to registry..."
docker push $(IMAGE_URL)/$(IMAGE_REPOSITORY)/metabase:$(METABASE_VERSION)
.PHONY: push-metabase

push-metabase-patched:
@echo "Pushing patched metabase docker image to registry..."
docker push $(IMAGE_URL)/$(IMAGE_REPOSITORY)/metabase-patched:$(METABASE_VERSION)
.PHONY: push-metabase-patched

push-deps:
@echo "Pushing nada-backend mocks docker image to registry..."
docker push $(IMAGE_URL)/$(IMAGE_REPOSITORY)/nada-backend-mocks:latest
.PHONY: push-deps
Loading

0 comments on commit 785ab01

Please sign in to comment.