Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Configure Go linting #11

Merged
merged 10 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# Only allow access to cost-manager binary
*
!/bin/cost-manager
.dockerignore
Dockerfile

.git/
.gitignore

bin/

LICENSE
README.md

charts/
64 changes: 36 additions & 28 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,38 +1,48 @@
name: ci
on: push
# https://github.com/golangci/golangci-lint-action?tab=readme-ov-file#comments-and-annotations
permissions:
contents: read
pull-requests: read
checks: write
jobs:
# https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
build:
# https://github.com/golangci/golangci-lint-action?tab=readme-ov-file#how-to-use
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v4
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version: '1.21'
- name: Download dependencies
run: go mod download
- name: Test
run: make test
- name: Build
run: make build
- name: Upload binary
uses: actions/upload-artifact@v3
cache: false
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
name: cost-manager
path: ./bin/cost-manager
version: v1.54
args: --timeout=10m
# https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version: '1.21'
- run: go mod download
- run: make test
- run: make build
# https://docs.docker.com/build/ci/github-actions/multi-platform/
release:
# Only build Docker image for repository PRs since secrets are not available to forks:
# https://github.com/orgs/community/discussions/25217#discussioncomment-3246904
build:
# Do not build Docker images for forked repositories since Docker Hub secrets are not available:
# https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions#using-secrets-in-a-workflow
if: ${{ ! github.event.pull_request.head.repo.fork }}
# We require the cost-manager binary artifact from the build job
needs: build
# Make sure the tests have passed before building
needs:
- lint
- test
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
Expand All @@ -42,11 +52,6 @@ jobs:
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Download binary
uses: actions/download-artifact@v3
with:
name: cost-manager
path: ./bin/cost-manager
- name: Build and push
uses: docker/build-push-action@v5
# https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#using-an-intermediate-environment-variable
Expand All @@ -58,3 +63,6 @@ jobs:
platforms: linux/amd64
push: true
tags: ${{ secrets.DOCKERHUB_USERNAME }}/cost-manager:${{ env.BRANCH == 'main' && 'latest' || env.BRANCH }}
# https://docs.docker.com/build/ci/github-actions/cache/#registry-cache
cache-from: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/cost-manager:buildcache
cache-to: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/cost-manager:buildcache,mode=max
14 changes: 13 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
FROM golang:1.21 as build

WORKDIR /go/src/cost-manager

COPY go.mod go.sum ./
RUN go mod download

COPY . .

# Build static cost-manager binary
RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o /go/bin/cost-manager

FROM gcr.io/distroless/static-debian12:nonroot

COPY ./bin/cost-manager /
COPY --from=build /go/bin/cost-manager /
ENTRYPOINT ["/cost-manager"]
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ test:
go test -race ./...

build:
CGO_ENABLED=0 go build -tags netgo -ldflags="-s -w" -o ./bin/cost-manager
go build -o ./bin/cost-manager

run: build
./bin/cost-manager

image: build
image:
docker build -t cost-manager .