Skip to content

Commit

Permalink
Merge pull request #79 from dionomusuko/refactor-all
Browse files Browse the repository at this point in the history
  • Loading branch information
peaceiris authored Nov 29, 2022
2 parents 9defb65 + 01dbb27 commit 91ae77a
Show file tree
Hide file tree
Showing 14 changed files with 247 additions and 244 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.*
*
!go.mod
!go.sum
!*.go
21 changes: 14 additions & 7 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@ name: CI
on:
push:
branches:
- master
- main
- master # to be removed
paths:
- ".github/workflows/ci.yaml"
- "go.*"
- "*.go"
- "Makefile"
- "Dockerfile"
- ".dockerignore"
pull_request:
branches:
- master
- main
- master # to be removed
paths:
- ".github/workflows/ci.yaml"
- "go.*"
- "*.go"
- "Makefile"
- "Dockerfile"
- ".dockerignore"

jobs:
go:
Expand All @@ -31,9 +39,9 @@ jobs:
- run: go mod download
- run: go mod tidy
- run: git diff --exit-code # This fails if you forget to apply tidy
- run: test -z $(gofmt -l .) # This fails if you forget to apply gofmt
- run: CGO_ENABLED=0 GOOS=linux go build
- run: go test -v ./...
- run: test -z "$(gofmt -l .)" # This fails if you forget to apply gofmt
- run: make build
- run: make test

docker:
runs-on: ubuntu-22.04
Expand All @@ -42,5 +50,4 @@ jobs:
contents: read
steps:
- uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # https://github.com/actions/checkout/releases/tag/v3.1.0
- name: Build
run: docker build .
- run: make docker-build
17 changes: 9 additions & 8 deletions .github/workflows/gh-release.yaml
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
on:
push:
branches:
- master
- main
paths:
- 'RELEASE'
pull_request:
types: [opened, synchronize]
branches:
- master
- main
paths:
- 'RELEASE'

jobs:
gh-release:
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
permissions:
contents: write
pull-requests: write
timeout-minutes: 5
steps:
- id: checkout
name: Checkout repository
uses: actions/checkout@a12a3943b4bdde767164f792f33f40b04645d846
- uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # https://github.com/actions/checkout/releases/tag/v3.1.0
with:
fetch-depth: 0
- uses: pipe-cd/[email protected].4
- uses: pipe-cd/actions-gh-release@b83d245d1b73261a6400cee9731b6af03df02f4a # https://github.com/pipe-cd/actions-gh-release/releases/tag/v2.3.6
with:
release_file: 'RELEASE'
token: ${{ secrets.GITHUB_TOKEN }}
44 changes: 24 additions & 20 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
name: Release

on:
workflow_dispatch:
inputs:
releaseFilePath:
description: 'Define the path of RELEASE file'
release_file_path:
description: "path to RELEASE file"
required: true
type: choice
options:
- RELEASE
- testdata/RELEASE
next_semver_level:
description: "semver level to bump"
required: true
default: 'RELEASE'
type: choice
options:
- 'RELEASE'
- 'testdata/RELEASE'
newTag:
description: 'new tag'
required: false
- patch
- minor
#- major

jobs:
release:
runs-on: ubuntu-latest
main:
runs-on: ubuntu-22.04
permissions:
contents: read
timeout-minutes: 5
steps:
- name: Checkout
uses: actions/checkout@v2
- uses: dionomusuko/gh-release-with-wf-dispatch@master
- uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # https://github.com/actions/checkout/releases/tag/v3.1.0
- uses: dionomusuko/gh-release-with-wf-dispatch@main
with:
"github_token": ${{ secrets.GH_PAT }}
"owner": ${{ github.event.repository.owner.login }}
"repo_full_name": ${{ github.event.repository.full_name }}
"repo": ${{ github.event.repository.name }}
"release_file_path": ${{ github.event.inputs.releaseFilePath }}
"base_branch": ${{ github.event.repository.default_branch }}
"new_tag": ${{ github.event.inputs.newTag }}
github_token: ${{ secrets.GH_PAT }}
release_file_path: ${{ github.event.inputs.release_file_path }}
next_semver_level: ${{ github.event.inputs.next_semver_level }}
7 changes: 3 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
FROM golang:1.19-buster AS builder
WORKDIR /app
COPY go.mod ./
COPY go.sum ./
RUN go mod download
COPY go.mod go.sum ./
COPY *.go ./
RUN CGO_ENABLED=0 GOOS=linux go build -a -o /bin/app
RUN go mod download
RUN CGO_ENABLED=0 GOOS="linux" GOARCH="amd64" go build -a -o /bin/app

FROM gcr.io/distroless/base
COPY --from=builder /bin/app /bin/app
Expand Down
23 changes: 23 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
DOCKER_IMAGE_NAME := ghcr.io/dionomusuko/gh-release-with-wf-dispatch
DOCKER_SCOPE := docker-latest

.PHONY: test
test:
go test -v ./...

.PHONY: build
build:
CGO_ENABLED=0 go build

.PHONY: docker-setup-buildx
docker-setup-buildx:
docker buildx create --use --driver docker-container

.PHONY: docker-build
docker-build: docker-setup-buildx
docker buildx build . \
--tag "${DOCKER_IMAGE_NAME}:latest" \
--platform "linux/amd64" \
--output "type=docker" \
--cache-from "type=gha,scope=${DOCKER_SCOPE}" \
--cache-to "type=gha,mode=max,scope=${DOCKER_SCOPE}"
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,12 @@ jobs:
timeout-minutes: 5
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: release
uses: peaceiris/gh-release-with-wf-dispatch@feat-semver-input
- uses: dionomusuko/gh-release-with-wf-dispatch@main
with:
github_token: ${{ secrets.GH_PAT }}
release_file_path: ${{ github.event.inputs.release_file_path }}
next_semver_level: ${{ github.event.inputs.next_semver_level }}
base_branch: "master"
base_branch: "develop"
user_name: "username"
user_email: "[email protected]"
```
6 changes: 5 additions & 1 deletion action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ inputs:
base_branch:
description: 'base branch'
required: true
default: 'master'
default: 'main'
user_name:
description: 'git config user.name'
required: false
Expand All @@ -34,3 +34,7 @@ inputs:
next_semver_level:
description: 'semver level to bump'
required: true
assignees:
description: 'comma separated assignees for pull-request'
required: false
default: ${{ github.actor }}
71 changes: 41 additions & 30 deletions git.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"context"
"fmt"
"log"
"os"
"time"

Expand All @@ -12,7 +11,6 @@ import (
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/go-git/go-git/v5/storage/memory"
)

Expand All @@ -29,67 +27,80 @@ type gitConfig struct {
userEmail string
}

func newGitClient(ctx context.Context, token, repository string, config gitConfig) *gitClient {
func newGitClient(ctx context.Context, token, repository string, config gitConfig) (*gitClient, error) {
fs := memfs.New()

// https://<GITHUB_TOKEN>@github.com/<REPO>.git
repo, err := git.CloneContext(ctx, memory.NewStorage(), fs, &git.CloneOptions{
URL: fmt.Sprintf("https://%[email protected]/%s.git", token, repository),
})
if err != nil {
log.Fatalf("falied to clone repository: %s", err.Error())
fmt.Printf("failed to clone repository %s\n", repository)
return nil, err
}
w, err := repo.Worktree()
if err != nil {
log.Fatalf("falied to get worktree: %v", err)
fmt.Println("failed to get worktree")
return nil, err
}
return &gitClient{
repository: repo,
worktree: w,
file: fs,
token: token,
config: config,
}
}, nil
}

// Checkout new branch
func (g *gitClient) Checkout(newTag string) string {
branch := plumbing.ReferenceName("refs/heads/release-" + newTag)
func (g *gitClient) Checkout(refName string) (string, error) {
branch := plumbing.ReferenceName(refName)
if err := g.worktree.Checkout(&git.CheckoutOptions{
Create: true,
Branch: branch,
}); err != nil {
log.Fatalf("falied to chckout repository: %v", err)
return "", nil
}
return branch.String()
return branch.String(), nil
}

func (g *gitClient) Commit(filePath, newTag string) {
// Create Commit
func (g *gitClient) Add(filePath string) error {
if _, err := g.worktree.Add(filePath); err != nil {
log.Fatalf("falied to add")
fmt.Printf("failed to git add file %s\n", filePath)
return err
}
_, err := g.worktree.Commit("chore: release-"+newTag, &git.CommitOptions{
Author: &object.Signature{
Name: g.config.userName,
Email: g.config.userEmail,
When: time.Now(),
}})
return nil
}

func (g *gitClient) Commit(msg string) error {
commitHash, err := g.worktree.Commit(
msg,
&git.CommitOptions{
Author: &object.Signature{
Name: g.config.userName,
Email: g.config.userEmail,
When: time.Now(),
},
},
)
if err != nil {
log.Fatalf("falied to commit")
fmt.Println("failed to git commit")
return err
}
fmt.Printf("commit %s has been created\n", commitHash.String())
return nil
}

func (g *gitClient) Push(ctx context.Context, owner string) {
if err := g.repository.PushContext(ctx, &git.PushOptions{
Progress: os.Stdout,
Auth: &http.BasicAuth{
Username: owner,
Password: g.token,
func (g *gitClient) Push(ctx context.Context) error {
if err := g.repository.PushContext(
ctx,
&git.PushOptions{
Progress: os.Stdout,
RemoteName: "origin",
},
RemoteName: "origin",
}); err != nil {
log.Fatalf("failed to push: %v", err)
); err != nil {
fmt.Println("failed to git push")
return err
}

return nil
}
Loading

0 comments on commit 91ae77a

Please sign in to comment.