Skip to content

Commit

Permalink
Add tagging Github action
Browse files Browse the repository at this point in the history
This commit generally improves CICD in regards to automated tagging.
Instead of manually adding and pushing a tag to trigger a release,
it's controlled through the `mockery-tools.env` config. Furthermore,
doc updates no longer require tags to update the docs page. It too
will key off of the `VERSION` parameter to determine the correct version
to push.
  • Loading branch information
LandonTClipp committed Nov 21, 2024
1 parent a27cb03 commit 63a14b5
Show file tree
Hide file tree
Showing 12 changed files with 713 additions and 509 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/documentation.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
name: documentation
on:
push:
tags:
- 'v*.*.*'
branches: [ master ]
permissions:
contents: write
jobs:
Expand All @@ -27,6 +26,6 @@ jobs:
git config --global user.email [email protected]
git fetch origin gh-pages --depth=1
- name: Deploy docs
run: "mike deploy --push --update-aliases $(echo ${{ github.ref_name }} | cut -d'.' -f1-2 ) latest"
run: "mike deploy --push --update-aliases $(grep VERSION mockery-tools.env | cut -d'=' -f 2 | cut -d'.' -f1-2) latest"
env:
GOOGLE_ANALYTICS_KEY: ${{ secrets.GOOGLE_ANALYTICS_KEY }}
43 changes: 0 additions & 43 deletions .github/workflows/release.yml

This file was deleted.

84 changes: 84 additions & 0 deletions .github/workflows/tag-and-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
name: Create new Git tag

on:
push:
branches:
- master
permissions:
contents: write
jobs:
tag:
runs-on: ubuntu-latest
outputs:
tag_result: ${{ steps.tag.outputs.tag_result }}
steps:
- run: sudo apt update && sudo apt install -y git && git --version
- uses: actions/checkout@v2
with:
# We need entire history of tags
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: ./tools/go.mod
check-latest: true
cache-dependency-path: "**/*.sum"

- name: Download Go dependencies
run: cd tools/ && go run github.com/go-task/task/v3/cmd/task install-deps && echo "~/go/bin" >> $GITHUB_PATH

- name: Run tagging commands
id: tag
run: |
set +e
task -x tag
tag_result="$?"
echo "tag_result=$tag_result" >> $GITHUB_OUTPUT
# The range between 8 and 63 inclusive is reserved for custom
# error codes that contain specific meaning.
if [ $tag_result -lt 8 -o $tag_result -gt 63 ]; then
exit $tag_result
fi
exit 0
- name: Push tags
run: task tag.push
if: steps.tag.outputs.tag_result == 0

release:
needs: tag
if: needs.tag.outputs.tag_result == 0
runs-on: ubuntu-latest
env:
DOCKER_CLI_EXPERIMENTAL: "enabled"

steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.21'

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

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

- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: GoReleaser
uses: goreleaser/[email protected]
with:
args: release --rm-dist
version: "<2"
env:
GITHUB_TOKEN: ${{ secrets.GORELEASER_GITHUB_TOKEN }}
HOMEBREW_TAP_TOKEN: ${{ secrets.GORELEASER_HOMEBREW_TAP_TOKEN }}
16 changes: 16 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,19 @@ tasks:

default:
deps: [test.ci]

build-tools:
desc: Build tools directory
cmds:
- cd ./tools && go build -o tools .

tag:
desc: Tag the git repo with the version specified.
deps: [build-tools]
cmds:
- ./tools/tools tag --dry-run=false

tag.push:
desc: Push tags to origin
cmds:
- git push origin --tags
157 changes: 4 additions & 153 deletions go.work.sum

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions mockery-tools.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VERSION=v2.49.0
71 changes: 71 additions & 0 deletions tools/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package cmd

import (
"fmt"
"os"

"github.com/go-errors/errors"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var logger zerolog.Logger

type timestampHook struct{}

func (t timestampHook) Run(e *zerolog.Event, level zerolog.Level, message string) {
e.Timestamp()
}

func maybeExit(err error) {
printStack(err)
if err != nil {
os.Exit(1)
}
}

func newViper() *viper.Viper {
v := viper.New()
v.SetConfigType("env")
v.SetConfigName("mockery-tools")
v.AddConfigPath(".")
v.AddConfigPath("../")
v.SetEnvPrefix("MOCKERYTOOLS")
maybeExit(v.ReadInConfig())
return v
}

func NewRootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "mockery_tools [command]",
}

logger = zerolog.New(zerolog.ConsoleWriter{
Out: os.Stdout,
}).Hook(timestampHook{})

subCommands := []func(v *viper.Viper) (*cobra.Command, error){
NewTagCmd,
}
for _, CommandFunc := range subCommands {
subCmd, err := CommandFunc(newViper())
if err != nil {
panic(err)
}
cmd.AddCommand(subCmd)
}
return cmd
}

func printStack(err error) {
if err == nil {
return
}
newErr, ok := err.(*errors.Error)
if ok {
fmt.Printf("%v\n", newErr.ErrorStack())
} else {
fmt.Printf("%v\n", err)
}
}
Loading

0 comments on commit 63a14b5

Please sign in to comment.