Skip to content

Commit

Permalink
chore: add CI
Browse files Browse the repository at this point in the history
  • Loading branch information
eliecharra committed Feb 28, 2024
1 parent f30a843 commit 976f321
Show file tree
Hide file tree
Showing 17 changed files with 568 additions and 48 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @spacelift-io/sirius
12 changes: 12 additions & 0 deletions .github/actions/setup-go/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Setup golang
description: "setup golang"
inputs:
version:
description: "golang version"
default: 1.21.4
runs:
using: "composite"
steps:
- uses: actions/setup-go@v4
with:
go-version: ${{ inputs.version }}
33 changes: 33 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Build
on:
push:
branches: [ "main" ]
pull_request:
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: setup go
uses: ./.github/actions/setup-go/

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Build controller binary
uses: goreleaser/goreleaser-action@v5
with:
args: build --snapshot

- name: Build container image
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/amd64,linux/arm64
push: false
59 changes: 59 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Lint
on:
push:
branches: [ "main" ]
pull_request:
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true
jobs:
generate:
name: generated files
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: setup go
uses: ./.github/actions/setup-go/
- name: Make sure generated files are up to date
run: |
make generate
git diff --no-patch --exit-code
if [ $? -ne 0 ]; then
echo "Please run make generate and commit the changes."
exit 1
fi
lint-manifest:
name: Manifests
runs-on: ubuntu-latest-4-cores
steps:
- uses: actions/checkout@v4
- name: setup go
uses: ./.github/actions/setup-go/
- name: Make sure manifest are up to date
run: |
make manifests
git diff --no-patch --exit-code
if [ $? -ne 0 ]; then
echo "Please run make manifests and commit the changes."
exit 1
fi
lint-go:
name: GO code
runs-on: ubuntu-latest-4-cores
steps:
- uses: actions/checkout@v4
- name: setup go
uses: ./.github/actions/setup-go/
- name: Make sure go.mod and go.sum are up to date
run: |
go mod tidy
git diff --no-patch --exit-code go.mod go.sum
if [ $? -ne 0 ]; then
echo "Please run go mod tidy and commit the changes."
exit 1
fi
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.54.2
args: --verbose
17 changes: 17 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Test
on:
push:
branches: [ "main" ]
pull_request:
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true
jobs:
unit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: setup go
uses: ./.github/actions/setup-go/
- name: Test
run: make test
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ Dockerfile.cross
*.swp
*.swo
*~

/dist
139 changes: 139 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
# This file contains all available configuration options
# with their default values.

# options for analysis running
run:
# timeout for analysis, e.g. 30s, 5m, default is 1m
timeout: 5m

# include test files or not, default is true
tests: false

# default is true. Enables skipping of directories:
# vendor$, third_party$, testdata$, examples$, Godeps$, builtin$
skip-dirs-use-default: true


# output configuration options
output:
# colored-line-number|line-number|json|tab|checkstyle|code-climate|junit-xml|github-actions
# default is "colored-line-number"
format: colored-line-number

# print lines of code with issue, default is true
print-issued-lines: true

# print linter name in the end of issue text, default is true
print-linter-name: true

# make issues output unique by line, default is true
uniq-by-line: true

# add a prefix to the output file references; default is no prefix
path-prefix: ""

# sorts results by: filepath, line and column
sort-results: false


linters-settings:
dupl:
threshold: 100
funlen:
lines: 100
statements: 50
gci:
sections:
- standard
- default
- prefix(github.com/spacelift-io/spacelift-operator)
goconst:
min-len: 2
min-occurrences: 2
staticcheck:
go: "1.20"
checks: [ "all", "-SA1019"]
gocritic:
enabled-tags:
- diagnostic
- performance
disabled-checks:
- ifElseChain
- wrapperFunc
- hugeParam
- rangeValCopy
- appendCombine
- commentedOutCode
- sloppyReassign
- filepathJoin
- evalOrder
- equalFold
- returnAfterHttpError
- preferStringWriter
- sprintfQuotedString
- preferFprint
goimports:
local-prefixes: github.com/spacelift-io/spacelift-operator
govet:
check-shadowing: false
nolintlint:
allow-leading-space: true # don't require machine-readable nolint directives (i.e. with no leading space)
require-explanation: false # don't require an explanation for nolint directives
require-specific: false # don't require nolint directives to be specific about which linter is being skipped
depguard:
rules:
main:
deny:
- pkg: "io/ioutil"
# https://go.dev/doc/go1.16#ioutil
desc: io/ioutil package has been deprecated.

errorlint:
errorf: false
errorf-multi: false
asserts: false
comparison: true

linters:
disable-all: true
enable:
- depguard
- exportloopref
- gci
- gocritic
- gofmt
- goimports
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- nolintlint
- staticcheck
- typecheck
- errorlint

severity:
# Default value is empty string.
# Set the default severity for issues. If severity rules are defined and the issues
# do not match or no severity is provided to the rule this will be the default
# severity applied. Severities should match the supported severity names of the
# selected out format.
# - Code climate: https://docs.codeclimate.com/docs/issues#issue-severity
# - Checkstyle: https://checkstyle.sourceforge.io/property_types.html#severity
# - GitHub: https://help.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-error-message
default-severity: error

# The default value is false.
# If set to true severity-rules regular expressions become case sensitive.
case-sensitive: false

# Default value is empty list.
# When a list of severity rules are provided, severity information will be added to lint
# issues. Severity rules have the same filtering capability as exclude rules except you
# are allowed to specify one matcher per severity rule.
# Only affects out formats that support setting severity information.
rules:
- linters:
- dupl
severity: info
15 changes: 15 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: 1

project_name: spacelift-operator

builds:
- main: ./cmd
env:
- CGO_ENABLED=0
goos:
- linux
goarch:
- amd64
- arm64
ldflags:
- -s -w -X github.com/spacelift-io/spacelift-operator/internal/build.Version={{.Version}}
33 changes: 6 additions & 27 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,33 +1,12 @@
# Build the manager binary
FROM golang:1.20 as builder
ARG TARGETOS
ARG TARGETARCH

WORKDIR /workspace
# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum
# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer
RUN go mod download

# Copy the go source
COPY cmd/main.go cmd/main.go
COPY api/ api/
COPY internal/controller/ internal/controller/

# Build
# the GOARCH has not a default value to allow the binary be built according to the host where the command
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
FROM gcr.io/distroless/static:nonroot

ARG TARGETARCH
ARG TARGETOS

WORKDIR /
COPY --from=builder /workspace/manager .
COPY dist/spacelift-operator_${TARGETOS}_${TARGETARCH}*/spacelift-operator .
USER 65532:65532

ENTRYPOINT ["/manager"]
ENTRYPOINT ["/spacelift-operator"]
17 changes: 15 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,14 @@ test: manifests generate fmt vet envtest ## Run tests.

.PHONY: build
build: manifests generate fmt vet ## Build manager binary.
go build -o bin/manager cmd/main.go
goreleaser build --snapshot --clean

.PHONY: run
run: manifests generate fmt vet ## Run a controller from your host.
go run ./cmd/main.go
go run ./cmd --zap-devel --zap-log-level=4

# The docker-push and docker-buildx targets aren't used as part of our release process, but are just
# left for convenience when working locally.

# If you wish to build the manager image targeting other platforms you can use the --platform flag.
# (i.e. docker build --platform linux/arm64). However, you must enable docker buildKit for it.
Expand Down Expand Up @@ -161,3 +164,13 @@ $(CONTROLLER_GEN): $(LOCALBIN)
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
$(ENVTEST): $(LOCALBIN)
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest


## Below are targets added on top of kubebuilder built in ones
.PHONY: lint
lint: ## Run golangci-lint
golangci-lint run

.PHONY: install-go-tools ## Install dev tools
install-go-tools:
go generate -tags tools
Loading

0 comments on commit 976f321

Please sign in to comment.