Skip to content

Commit

Permalink
Merge pull request #1 from uselagoon/ci-automation
Browse files Browse the repository at this point in the history
CI automation and initial implementation
  • Loading branch information
smlx authored Nov 22, 2021
2 parents 4fcf622 + 1b1633a commit 32fd35c
Show file tree
Hide file tree
Showing 15 changed files with 492 additions and 39 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Build
on: pull_request
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: "^1.17"
- name: Set up environment
run: echo "GOVERSION=$(go version)" >> $GITHUB_ENV
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: build --snapshot --rm-dist
- name: Login to GHCR
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Docker metadata
id: service_api_meta
uses: docker/metadata-action@v3
with:
images: uselagoon/lagoon-ssh-portal/service-api
- name: Build and push service-api container image
id: docker_build
uses: docker/build-push-action@v2
with:
push: true
tags: ghcr.io/${{ steps.service_api_meta.outputs.tags }}
labels: ${{ steps.service_api_meta.outputs.labels }}
file: deploy/service-api/Dockerfile
context: dist/lagoon-ssh-portal_linux_amd64
22 changes: 22 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Lint
on: pull_request
jobs:
golangci-lint:
name: lint
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2.5.2
with:
version: latest
commitlint:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Lint commit messages
uses: wagoid/commitlint-github-action@v4.1.9
77 changes: 77 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: Release
on:
push:
branches:
- main
jobs:
tag:
runs-on: ubuntu-latest
outputs:
new-tag: ${{ steps.bump-tag.outputs.new }}
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Configure Git
run: |
git config --global user.name "$GITHUB_ACTOR"
git config --global user.email "$GITHUB_ACTOR@users.noreply.github.com"
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: "^1.17"
- name: Install ccv
run: >
curl -sSL https://github.com/smlx/ccv/releases/download/v0.3.2/ccv_0.3.2_linux_amd64.tar.gz
| sudo tar -xz -C /usr/local/bin ccv
- name: Bump tag if necessary
id: bump-tag
run: |
if [ -z $(git tag -l $(ccv)) ]; then
git tag $(ccv)
git push --tags
echo "::set-output name=new::true"
fi
release:
needs: tag
if: needs.tag.outputs.new-tag == 'true'
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: "^1.17"
- name: Set up environment
run: echo "GOVERSION=$(go version)" >> $GITHUB_ENV
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Login to GHCR
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Docker metadata
id: service_api_meta
uses: docker/metadata-action@v3
with:
images: uselagoon/lagoon-ssh-portal/service-api
- name: Build and push service-api container image
id: docker_build
uses: docker/build-push-action@v2
with:
push: true
tags: ghcr.io/${{ steps.service_api_meta.outputs.tags }}
labels: ${{ steps.service_api_meta.outputs.labels }}
file: deploy/service-api/Dockerfile
context: dist/lagoon-ssh-portal_linux_amd64
44 changes: 44 additions & 0 deletions .github/workflows/tag-to-release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Tag to Release
on:
push:
tags:
- v*
jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: "^1.17"
- name: Set up environment
run: echo "GOVERSION=$(go version)" >> $GITHUB_ENV
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Login to GHCR
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Docker metadata
id: service_api_meta
uses: docker/metadata-action@v3
with:
images: uselagoon/lagoon-ssh-portal/service-api
- name: Build and push service-api container image
id: docker_build
uses: docker/build-push-action@v2
with:
push: true
tags: ghcr.io/${{ steps.service_api_meta.outputs.tags }}
labels: ${{ steps.service_api_meta.outputs.labels }}
file: deploy/service-api/Dockerfile
context: dist/lagoon-ssh-portal_linux_amd64
14 changes: 14 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Test
on: pull_request
jobs:
go-test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: "^1.17"
- name: Run Tests
run: go test -v ./...
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/dist
9 changes: 9 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
builds:
- dir: cmd/service-api
binary: service-api
ldflags:
- >
-s -w -X main.date={{.Date}} -X "main.goVersion={{.Env.GOVERSION}}"
-X main.shortCommit={{.ShortCommit}} -X main.version={{.Version}}
env:
- CGO_ENABLED=0
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ test: mod-tidy generate
go test -v ./...

.PHONY: mod-tidy
mod-tidy: generate
mod-tidy:
go mod tidy

.PHONY: generate
generate:
generate: mod-tidy
go generate ./...
2 changes: 1 addition & 1 deletion cmd/service-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var (

// CLI represents the command-line interface.
type CLI struct {
Debug bool `kong:"help='Enable debug logging'"`
Debug bool `kong:"env='DEBUG',help='Enable debug logging'"`
Serve ServeCmd `kong:"cmd,default=1,help='(default) Serve service-api requests'"`
Version VersionCmd `kong:"cmd,help='Print version information'"`
}
Expand Down
27 changes: 17 additions & 10 deletions cmd/service-api/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,23 @@ import (
"os"
"os/signal"

"github.com/go-sql-driver/mysql"
"github.com/uselagoon/ssh-portal/internal/keycloak"
"github.com/uselagoon/ssh-portal/internal/lagoondb"
"github.com/uselagoon/ssh-portal/internal/serviceapi"
"go.uber.org/zap"

_ "github.com/go-sql-driver/mysql"
)

// ServeCmd represents the serve command.
type ServeCmd struct {
NATSServer string `kong:"required,help='NATS server URL (nats://... or tls://...)'"`
APIDB string `kong:"required,help='Lagoon API Database DSN (https://github.com/go-sql-driver/mysql#dsn-data-source-name)'"`
JWTSecret string `kong:"required,help='JWT Symmetric Secret'"`
KeycloakBaseURL string `kong:"required,help='Keycloak Base URL'"`
KeycloakClientID string `kong:"default='service-api',help='Keycloak OAuth2 Client ID'"`
KeycloakClientSecret string `kong:"required,help='Keycloak OAuth2 Client Secret'"`
APIDBAddress string `kong:"required,env='API_DB_ADDRESS',help='Lagoon API DB Address (host[:port])'"`
APIDBDatabase string `kong:"default='infrastructure',env='API_DB_DATABASE',help='Lagoon API DB Database Name'"`
APIDBPassword string `kong:"required,env='API_DB_PASSWORD',help='Lagoon API DB Password'"`
APIDBUsername string `kong:"default='api',env='API_DB_USERNAME',help='Lagoon API DB Username'"`
KeycloakBaseURL string `kong:"required,env='KEYCLOAK_BASE_URL',help='Keycloak Base URL'"`
KeycloakClientID string `kong:"default='service-api',env='KEYCLOAK_SERVICE_API_CLIENT_ID',help='Keycloak OAuth2 Client ID'"`
KeycloakClientSecret string `kong:"required,env='KEYCLOAK_SERVICE_API_CLIENT_SECRET',help='Keycloak OAuth2 Client Secret'"`
NATSServer string `kong:"required,env='NATS_URL',help='NATS server URL (nats://... or tls://...)'"`
}

// getContext starts a goroutine to handle ^C gracefully, and returns a context
Expand All @@ -48,13 +49,19 @@ func (cmd *ServeCmd) Run(log *zap.Logger) error {
ctx, cancel := getContext()
defer cancel()
// init lagoon DB client
l, err := lagoondb.NewClient(ctx, cmd.APIDB)
dbConf := mysql.NewConfig()
dbConf.Addr = cmd.APIDBAddress
dbConf.DBName = cmd.APIDBDatabase
dbConf.Net = "tcp"
dbConf.Passwd = cmd.APIDBPassword
dbConf.User = cmd.APIDBUsername
l, err := lagoondb.NewClient(ctx, dbConf.FormatDSN())
if err != nil {
return fmt.Errorf("couldn't init lagoon DBClient: %v", err)
}
// init keycloak client
k, err := keycloak.NewClient(ctx, log, cmd.KeycloakBaseURL,
cmd.KeycloakClientID, cmd.KeycloakClientSecret, cmd.JWTSecret)
cmd.KeycloakClientID, cmd.KeycloakClientSecret)
if err != nil {
return fmt.Errorf("couldn't init keycloak Client: %v", err)
}
Expand Down
Loading

0 comments on commit 32fd35c

Please sign in to comment.