Skip to content

Commit

Permalink
Major/GitHub actions setup (#7)
Browse files Browse the repository at this point in the history
* Base workflows files created, need to be filled

* Add build stage

* Fix build stage

* Added test CI, makefile test cmd

* Modified release stage.

* Output go version

* Fix tests step

* Fix typos

* Edit test stage with codecov.

* Fix test codecov step

* Added coverage badge.

* Fix release stage.

* Add goreleaser config

* Add quay readme badge

* Add version command, edit CI

* Fixed dockerfile ldflags.

* Fix missing docker args

* Fix ldflags dockerfile.

* Update readme
  • Loading branch information
cguertin14 authored Mar 8, 2022
1 parent 3432345 commit 30e96a0
Show file tree
Hide file tree
Showing 10 changed files with 235 additions and 6 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Build app

on: [push, pull_request]

jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: '^1.17.8'

- name: Show Go version
run: go version

- name: Check out source code
uses: actions/checkout@v1

- name: Build
run: make build
35 changes: 35 additions & 0 deletions .github/workflows/goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Release binaries

on:
push:
tags:
- '*'

permissions:
contents: write

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Fetch all tags
run: git fetch --force --tags

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: '^1.17.8'

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
distribution: goreleaser
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
57 changes: 57 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: Release app

on:
push:
tags:
- '*'

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Prepare
id: prep
run: |
VERSION=$(git describe --tags $(git rev-list --tags --max-count=1))
GIT_COMMIT=$(git rev-parse --short HEAD)
BUILD_DATE=$(date '+%FT%TZ')
echo ::set-output name=version::${VERSION}
echo ::set-output name=git_commit::${GIT_COMMIT}
echo ::set-output name=build_date::${BUILD_DATE}
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
with:
platforms: all

- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v1
with:
install: true
version: latest

- name: Login to Quay
uses: docker/login-action@v1
with:
registry: quay.io
username: ${{ secrets.QUAY_USERNAME }}
password: ${{ secrets.QUAY_ROBOT_TOKEN }}

- name: Build and Push
uses: docker/build-push-action@v2
with:
build-args: |
VERSION=${{ steps.prep.outputs.version }}
GIT_COMMIT=${{ steps.prep.outputs.git_commit }}
BUILD_DATE=${{ steps.prep.outputs.build_date }}
context: ./
platforms: linux/amd64,linux/arm/v7,linux/arm64
push: true
tags: |
quay.io/cguertin14/k3supdater:latest
quay.io/cguertin14/k3supdater:${{ github.sha }}
quay.io/cguertin14/k3supdater:${{ steps.prep.outputs.version }}
32 changes: 32 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Test app

on: [push, pull_request]

jobs:
build:
name: Test
runs-on: ubuntu-latest
steps:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: '^1.17.8'

- name: Show Go version
run: go version

- name: Check out source code
uses: actions/checkout@v1

- name: Run tests
run: make test

- name: Upload Artifact
uses: actions/upload-artifact@v3
with:
name: tests-results
path: coverage.html
retention-days: 5

- name: Upload coverage to Codecov
run: bash <(curl -s https://codecov.io/bash)
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
# Dependency directories (remove the comment below to include it)
# vendor/

k3supdater
k3supdater
coverage.*
30 changes: 30 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
project_name: k3supdater

before:
hooks:
- go mod download

builds:
- env:
- CGO_ENABLED=0

goos:
- linux
- windows
- darwin

binary: "{{ .ProjectName }}"

ldflags:
- -X github.com/cguertin14/k3supdater/cmd.BuildDate="{{ .Date }}"
- -X github.com/cguertin14/k3supdater/cmd.GitCommit="{{ .Commit }}"
- -X github.com/cguertin14/k3supdater/cmd.Version="{{ .Version }}"


changelog:
sort: asc

release:
github:
owner: cguertin14
name: k3supdater
12 changes: 10 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ LABEL maintainer="Charles Guertin <[email protected]>"
ARG TARGETOS
ARG TARGETARCH
ARG TARGETVARIANT=""
ARG BUILD_DATE
ARG VERSION
ARG GIT_COMMIT

ENV CGO_ENABLED=0 \
GOOS=${TARGETOS} \
GOARCH=${TARGETARCH} \
GOARM=${TARGETVARIANT}
GOARM=${TARGETVARIANT} \
BUILD_DATE=${BUILD_DATE} \
VERSION=${VERSION} \
GIT_COMMIT=${GIT_COMMIT}

RUN apk add --no-cache --update ca-certificates make

Expand All @@ -20,7 +26,9 @@ COPY go.* ./
RUN go mod download

COPY . ./
RUN make build
RUN go build \
-ldflags "-X github.com/cguertin14/k3supdater/cmd.Version=${VERSION} -X github.com/cguertin14/k3supdater/cmd.BuildDate=${BUILD_DATE} -X github.com/cguertin14/k3supdater/cmd.GitCommit=${GIT_COMMIT}" \
-o ./k3supdater .

# Add user & group
RUN addgroup -S updater-group && \
Expand Down
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ BIN_NAME = k3supdater
default: setup

setup:
go install github.com/golang/mock/mockgen@latest
@go install github.com/golang/mock/mockgen@latest

build:
go build -o ./${BIN_NAME} .
@go build -o ./${BIN_NAME} .

test:
@go test -v ./... -coverprofile coverage.out
go tool cover -html=coverage.out -o coverage.html

generate-mock:
@go generate -v ./...
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# k3supdater

[![Go Report Card](https://goreportcard.com/badge/github.com/cguertin14/k3supdater)](https://goreportcard.com/report/github.com/cguertin14/k3supdater)
[![codecov](https://codecov.io/gh/cguertin14/k3supdater/branch/main/graph/badge.svg?token=BUUUB7F5HX)](https://codecov.io/gh/cguertin14/k3supdater)
[![Docker Repository on Quay](https://quay.io/repository/cguertin14/k3supdater/status "Docker Repository on Quay")](https://quay.io/repository/cguertin14/k3supdater)


Updater (similar to Renovate Bot) for k3s ansible playbook versions.
Expand All @@ -12,7 +14,7 @@ First, you need to create a Github access token with write access to the reposit

## Usage

To use `k3supdater`, you'll need to download the binary (TODO: Add download link here) or compile it locally.
To use `k3supdater`, you'll need to [download the appropriate binary for your machine](https://github.com/cguertin14/k3supdater/releases) or compile it locally. It can also be used via docker, using the `quay.io/cguertin14/k3supdater` image.

Then, you'll be able to run update commands like so:
```bash
Expand Down
38 changes: 38 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

var (
// GitCommit
// The latest git commit hash
GitCommit string

// BuildDate
// The date when the build was made
BuildDate string

// Version
// The version of the app
Version string
)

var (
versionCmd = &cobra.Command{
Use: "version",
Short: "Show k3supdater's installed version",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf(
"Version: %s, GitCommit: %s, BuildDate: %s\n",
Version, GitCommit, BuildDate,
)
},
}
)

func init() {
rootCmd.AddCommand(versionCmd)
}

0 comments on commit 30e96a0

Please sign in to comment.