Skip to content

Commit

Permalink
✨ (go/v4) Add GitHub Actions
Browse files Browse the repository at this point in the history
Within the changes of this PR new projects will become to be scaffold with GitHub actions
which will allow users be aware of how to test their changes on the ci.

Either will make clear the purpose of the test-e2e and encorage common/best practices to
ensure the quality of the properties
  • Loading branch information
camilamacedo86 committed Aug 28, 2024
1 parent a1457ac commit ba717f3
Show file tree
Hide file tree
Showing 28 changed files with 762 additions and 34 deletions.
55 changes: 55 additions & 0 deletions .github/workflows/test-sample-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: Test Sample CI

on:
push:
pull_request:

jobs:
check-ci:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Install act in project bin
run: |
mkdir -p testdata/project-v4/bin
ACT_VERSION="v0.2.65"
curl -Lo act.tgz https://github.com/nektos/act/releases/download/${ACT_VERSION}/act_Linux_x86_64.tar.gz
tar -C testdata/project-v4/bin -xzf act.tgz act
rm -f act.tgz
- name: Test act installation
run: |
echo "Testing act installation"
testdata/project-v4/bin/act --help
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: '~1.22'

- name: Prepare the environment
run: |
KUSTOMIZATION_FILE_PATH="testdata/project-v4/config/default/kustomization.yaml"
sed -i '25s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '51s/^#//' $KUSTOMIZATION_FILE_PATH
cd testdata/project-v4/
go mod tidy
make generate
make manifests
- name: Lint
run: |
echo "Running lint ci"
./testdata/project-v4/bin/act -W testdata/project-v4/.github/workflows/lint.yml --container-architecture linux/amd64 -P ubuntu-latest=nektos/act-environments-ubuntu:20.04
- name: Unit Tests
run: |
echo "Running unit tests ci"
./testdata/project-v4/bin/act -W testdata/project-v4/.github/workflows/test.yml --container-architecture linux/amd64 -P ubuntu-latest=nektos/act-environments-ubuntu:20.04
- name: E2e Tests
run: |
echo "Running e2e tests ci"
./testdata/project-v4/bin/act -W testdata/project-v4/.github/workflows/test-e2e.yml --container-architecture linux/amd64 -P ubuntu-latest=nektos/act-environments-ubuntu:20.04
33 changes: 0 additions & 33 deletions .github/workflows/test-sample-go.yml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Linter

on:
push:
pull_request:

jobs:
test:
name: Run on Ubuntu
runs-on: ubuntu-latest
steps:
- name: Clone the code
uses: actions/checkout@v4

- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: '~1.22'

- name: Run linter
uses: golangci/golangci-lint-action@v6
with:
version: v1.59
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: E2e tests

on:
push:
pull_request:

jobs:
test:
name: Run on Ubuntu
runs-on: ubuntu-latest
steps:
- name: Clone the code
uses: actions/checkout@v4

- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: '~1.22'

- name: Create kind cluster
run: kind create cluster

- name: Test e2e
run: make test-e2e
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Unit Tests

on:
push:
pull_request:

jobs:
test:
name: Run on Ubuntu
runs-on: ubuntu-latest
steps:
- name: Clone the code
uses: actions/checkout@v4

- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: '~1.22'

- name: Create kind cluster
run: kind create cluster

- name: Test e2e
run: make test
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Linter

on:
push:
pull_request:

jobs:
test:
name: Run on Ubuntu
runs-on: ubuntu-latest
steps:
- name: Clone the code
uses: actions/checkout@v4

- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: '~1.22'

- name: Run linter
uses: golangci/golangci-lint-action@v6
with:
version: v1.59
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: E2e tests

on:
push:
pull_request:

jobs:
test:
name: Run on Ubuntu
runs-on: ubuntu-latest
steps:
- name: Clone the code
uses: actions/checkout@v4

- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: '~1.22'

- name: Create kind cluster
run: kind create cluster

- name: Test e2e
run: make test-e2e
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Unit Tests

on:
push:
pull_request:

jobs:
test:
name: Run on Ubuntu
runs-on: ubuntu-latest
steps:
- name: Clone the code
uses: actions/checkout@v4

- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: '~1.22'

- name: Create kind cluster
run: kind create cluster

- name: Test e2e
run: make test
4 changes: 4 additions & 0 deletions pkg/plugins/golang/v4/scaffolds/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"sigs.k8s.io/kubebuilder/v4/pkg/plugins"
kustomizecommonv2 "sigs.k8s.io/kubebuilder/v4/pkg/plugins/common/kustomize/v2"
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang/v4/scaffolds/internal/templates"
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang/v4/scaffolds/internal/templates/github"
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang/v4/scaffolds/internal/templates/hack"
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang/v4/scaffolds/internal/templates/test/e2e"
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang/v4/scaffolds/internal/templates/test/utils"
Expand Down Expand Up @@ -161,6 +162,9 @@ func (s *initScaffolder) Scaffold() error {
&templates.Golangci{},
&e2e.Test{},
&e2e.SuiteTest{},
&github.E2eTestCi{},
&github.TestCi{},
&github.LintCi{},
&utils.Utils{},
&templates.DevContainer{},
&templates.DevContainerPostInstallScript{},
Expand Down
67 changes: 67 additions & 0 deletions pkg/plugins/golang/v4/scaffolds/internal/templates/github/lint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package github

import (
"path/filepath"

"sigs.k8s.io/kubebuilder/v4/pkg/machinery"
)

var _ machinery.Template = &TestCi{}

// LintCi scaffolds the GitHub Action to lint the project
type LintCi struct {
machinery.TemplateMixin
machinery.BoilerplateMixin
}

// SetTemplateDefaults implements file.Template
func (f *LintCi) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = filepath.Join(".github", "workflows", "lint.yml")
}

f.TemplateBody = lintCiTemplate

return nil
}

const lintCiTemplate = `name: Linter
on:
push:
pull_request:
jobs:
test:
name: Run on Ubuntu
runs-on: ubuntu-latest
steps:
- name: Clone the code
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: '~1.22'
- name: Run linter
uses: golangci/golangci-lint-action@v6
with:
version: v1.59
`
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package github

import (
"path/filepath"

"sigs.k8s.io/kubebuilder/v4/pkg/machinery"
)

var _ machinery.Template = &E2eTestCi{}

// E2eTestCi scaffolds the GitHub Action to call make test-e2e
type E2eTestCi struct {
machinery.TemplateMixin
machinery.BoilerplateMixin
}

// SetTemplateDefaults implements file.Template
func (f *E2eTestCi) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = filepath.Join(".github", "workflows", "test-e2e.yml")
}

f.TemplateBody = e2eTestCiTemplate

return nil
}

const e2eTestCiTemplate = `name: E2e tests
on:
push:
pull_request:
jobs:
test:
name: Run on Ubuntu
runs-on: ubuntu-latest
steps:
- name: Clone the code
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: '~1.22'
- name: Create kind cluster
run: kind create cluster
- name: Test e2e
run: make test-e2e
`
Loading

0 comments on commit ba717f3

Please sign in to comment.