Skip to content

Commit

Permalink
.github: add tagged release workflow (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryandotsmith authored Nov 13, 2023
1 parent 9a9ffa0 commit 192e4b0
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 13 deletions.
79 changes: 79 additions & 0 deletions .github/workflows/release-tag.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
name: release
on:
push:
tags:
- v*
jobs:
docker:
name: docker
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v3
-
name: Set up QEMU
uses: docker/setup-qemu-action@v2
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
-
name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Set Tag
uses: actions/github-script@v4
id: set_tag
with:
script: |
const tag = context.ref.replace('refs/tags/v', '')
core.setOutput('tag', tag)
-
name: Build and push
uses: docker/build-push-action@v4
with:
platforms: linux/amd64,linux/arm64
context: .
file: ./cmd/shovel/Dockerfile
push: true
tags: indexsupply/shovel:${{ steps.set_tag.outputs.tag }}
bins:
name: binaries
runs-on: ubuntu-latest
strategy:
matrix:
cmd: [shovel]
steps:
- name: Checkout
uses: actions/checkout@v3
- name: set up Go
uses: actions/setup-go@v3
with:
go-version: "1.21"
- uses: actions/cache@v3
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Set Tag
uses: actions/github-script@v4
id: set_tag
with:
script: |
const tag = context.ref.replace('refs/tags/v', '')
core.setOutput('tag', tag)
- uses: ./.github/actions/release
id: go-run-release
with:
cmd: ${{ matrix.cmd }} -tag ${{ steps.set_tag.outputs.tag }}
dist: ${{ secrets.AWS_DISTRIBUTION_ID }}
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: 'us-west-2'
32 changes: 20 additions & 12 deletions cmd/release/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@ func check(err error) {

func main() {
var (
distID string
bucket string
tmpdir string
cmd string
awss = session.Must(session.NewSession())
buildOnly bool
distID string
bucket string
tmpdir string
cmd string
tag string
awss = session.Must(session.NewSession())
)
flag.BoolVar(&buildOnly, "b", false, "build only. no upload")
flag.StringVar(&bucket, "bucket", "bin.indexsupply.net", "s3 bucket")
flag.StringVar(&cmd, "cmd", "", "command to build")
flag.StringVar(&tag, "tag", "main", "version tag")
flag.StringVar(&tmpdir, "dir", "/tmp", "tmp binary storage")
flag.StringVar(&distID, "dist", "", "aws cloudfront dist to invalidate")
flag.Parse()
Expand All @@ -58,15 +62,18 @@ func main() {
goarch = platforms[i][1]
binPath = fmt.Sprintf("%s/%s/%s/%s", tmpdir, goos, goarch, cmd)
cmdPath = fmt.Sprintf("./cmd/%s", cmd)
s3key = fmt.Sprintf("bin/main/%s/%s/%s", goos, goarch, cmd)
s3key = fmt.Sprintf("bin/%s/%s/%s/%s", tag, goos, goarch, cmd)
)
check(build(goos, goarch, binPath, cmdPath))
check(build(tag, goos, goarch, binPath, cmdPath))
if buildOnly {
continue
}
f, err := os.Open(binPath)
check(err)
check(putFile(awss, f, bucket, s3key))
f.Close()
if distID != "" {
check(invalidateMain(awss, distID))
check(invalidate(tag, awss, distID))
}
fmt.Printf("- %s/%s\n", bucket, s3key)
}
Expand All @@ -79,9 +86,10 @@ func hash(f *os.File) string {
return fmt.Sprintf("%x", h.Sum(nil))
}

func build(goos, goarch, binPath, cmdPath string) error {
func build(tag, goos, goarch, binPath, cmdPath string) error {
var out strings.Builder
c := exec.Command("go", "build", "-o", binPath, cmdPath)
v := fmt.Sprintf("-ldflags=-X main.Version=%s", tag)
c := exec.Command("go", "build", v, "-o", binPath, cmdPath)
c.Stdout = &out
c.Stderr = &out
c.Env = append(os.Environ(),
Expand Down Expand Up @@ -113,8 +121,8 @@ func randstr() string {

// since we overwrite the binaries in the 'main' dir
// we need to inform cloudfront's cache of the change
func invalidateMain(s *session.Session, distID string) error {
var paths = []*string{aws.String("/bin/main/*")}
func invalidate(tag string, s *session.Session, distID string) error {
var paths = []*string{aws.String(fmt.Sprintf("/bin/%s/*", tag))}
_, err := cloudfront.New(s).CreateInvalidation(&cloudfront.CreateInvalidationInput{
DistributionId: &distID,
InvalidationBatch: &cloudfront.InvalidationBatch{
Expand Down
2 changes: 1 addition & 1 deletion cmd/shovel/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func main() {
})))

if version {
fmt.Printf("v%s-%s\n", Version, Commit)
fmt.Printf("v%s %s\n", Version, Commit)
os.Exit(0)
}

Expand Down

0 comments on commit 192e4b0

Please sign in to comment.