Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: version and CI release #17

Merged
merged 3 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.github
docs
.goreleaser.yaml
*.md
LICENSE.md
66 changes: 66 additions & 0 deletions .github/workflows/publish-ghcr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: Container

on:
push:
branches:
- 'main'
tags:
- 'v*'
workflow_run:
workflows: [ Releaser ]
types:
- completed
pull_request:

jobs:
prepare-checkout:
if: github.event_name != 'workflow_run' || github.event.workflow_run.conclusion == 'success'
name: Prepare ref
runs-on: ubuntu-latest
outputs:
ref: ${{ github.event_name != 'workflow_run' && github.ref || steps.releaser.outputs.version }}
steps:
- name: Get Ref from releaser
id: releaser
if: github.event_name == 'workflow_run'
uses: ipdxco/unified-github-workflows/.github/actions/[email protected]
with:
artifacts-url: ${{ github.event.workflow_run.artifacts_url }}
publish:
name: Publish
needs: [ prepare-checkout ]
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout
uses: actions/checkout@v3
with:
ref: ${{ needs.prepare-checkout.outputs.ref }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Log in to the Container registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ github.token }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v4
with:
images: ghcr.io/${{ github.repository }}
tags: |
type=semver,pattern={{raw}}
type=ref,event=branch
type=raw,value=${{ needs.prepare-checkout.outputs.ref }}
- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
context: .
cache-from: type=gha
cache-to: type=gha,mode=max
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
29 changes: 29 additions & 0 deletions .github/workflows/release-binaries.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: GoReleaser
on:
release:
types: [ published ]
workflow_dispatch:
workflow_run:
workflows: [Releaser]
types: [completed]
jobs:
bin-releaser:
name: Release Binaries
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: "1.20.x"
- name: Release Binaries
uses: goreleaser/goreleaser-action@v5
with:
distribution: goreleaser
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
19 changes: 19 additions & 0 deletions .github/workflows/release-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Release Checker

on:
pull_request_target:
paths: [ 'version.json' ]
types: [ opened, synchronize, reopened, labeled, unlabeled ]
workflow_dispatch:

permissions:
contents: write
pull-requests: write

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
release-check:
uses: ipdxco/unified-github-workflows/.github/workflows/[email protected]
17 changes: 17 additions & 0 deletions .github/workflows/releaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Releaser

on:
push:
paths: [ 'version.json' ]
workflow_dispatch:

permissions:
contents: write

concurrency:
group: ${{ github.workflow }}-${{ github.sha }}
cancel-in-progress: true

jobs:
releaser:
uses: ipdxco/unified-github-workflows/.github/workflows/[email protected]
18 changes: 18 additions & 0 deletions .github/workflows/tagpush.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Tag Push Checker

on:
push:
tags:
- v*

permissions:
contents: read
issues: write

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
releaser:
uses: ipdxco/unified-github-workflows/.github/workflows/[email protected]
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/cmd/storage
/storage
/build
34 changes: 34 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
builds:
- main: ./cmd
binary: storage
ldflags:
# Sets the version variable in the build package to the build version prefixed with a 'v'
# Sets the main.date to a static date for checksum verification. See https://goreleaser.com/customization/builds/#reproducible-builds.
- -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.CommitDate}} -X main.builtBy=goreleaser -X github.com/storacha/storage/pkg/build.version=v{{.Version}}
goos:
- linux
- windows
- darwin
goarch:
- 'amd64'
- 'arm64'
# Change to a static date for checksum verification. See https://goreleaser.com/customization/builds/#reproducible-builds.
mod_timestamp: '{{.CommitTimestamp}}'
env:
- CGO_ENABLED=0
universal_binaries:
- replace: true
archives:
- format_overrides:
- goos: windows
format: zip
- goos: darwin
format: zip
name_template: >-
{{ .ProjectName }}_{{ .Version }}_
{{- if eq .Os "darwin" }}mac_os
{{- else }}{{ .Os }}{{ end }}_{{ .Arch }}
release:
mode: keep-existing
changelog:
skip: true
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM golang:1.23-bullseye as build

WORKDIR /go/src/storage

COPY go.* .
RUN go mod download
COPY . .

RUN CGO_ENABLED=0 go build -o /go/bin/storage ./cmd

FROM gcr.io/distroless/static-debian12
COPY --from=build /go/bin/storage /usr/bin/

ENTRYPOINT ["/usr/bin/storage"]
68 changes: 68 additions & 0 deletions pkg/build/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package build

import (
_ "embed"
"encoding/json"
"fmt"
"os"

"github.com/storacha/storage/pkg/internal/revision"
)

var (
// version is the built version.
// Set with ldflags in .goreleaser.yaml via -ldflags="-X github.com/storacha/storage/pkg/build.version=v{{.Version}}".
version string
// Version returns the current version of the application
Version string
// UserAgent is the user agent used for HTTP requests
UserAgent string
)

const (
defaultVersion string = "v0.0.0" // Default version if not set by ldflags
versionFile string = "version.json" // Version file path
)

func init() {
if version == "" {
// This is being ran in development, try to grab the latest known version from the version.json file
var err error
version, err = readVersionFromFile()
if err != nil {
// Use the default version
version = defaultVersion
}
}

Version = fmt.Sprintf("%s-%s", version, revision.Revision)
UserAgent = fmt.Sprintf("storage/%s", Version)
}

// versionJson is used to read the local version.json file
type versionJSON struct {
Version string `json:"version"`
}

// readVersionFromFile reads the version from the version.json file.
// Reading this should be fine in development since the version.json file
// should be present in the project, I hope :)
func readVersionFromFile() (string, error) {
// Open file
file, err := os.Open(versionFile)
if err != nil {
return "", err
}
defer file.Close()

// Decode json into struct
decoder := json.NewDecoder(file)
var vJSON versionJSON
err = decoder.Decode(&vJSON)
if err != nil {
return "", err
}

// Read version from json
return vJSON.Version, nil
}
43 changes: 43 additions & 0 deletions pkg/internal/revision/revision.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Package revision provides the vsc revision, embedded by the compiler, as a
// global variable.
package revision

import (
"runtime/debug"
)

// Revision returns the revision embedded by the compiler during build.
// Suffixed with "-dirty" if modified.
var Revision string

func init() {
revision := "unknown"
var dirty bool

// Get the revision from the build info
bi, ok := debug.ReadBuildInfo()
if !ok {
Revision = revision
return
}

for _, bs := range bi.Settings {
switch bs.Key {
case "vcs.revision":
revision = bs.Value
if len(bs.Value) > 7 {
revision = bs.Value[:7]
}
case "vcs.modified":
if bs.Value == "true" {
dirty = true
}
}
}

if dirty {
revision += "-dirty"
}

Revision = revision
}
3 changes: 2 additions & 1 deletion pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
logging "github.com/ipfs/go-log/v2"
"github.com/storacha/go-ucanto/principal"
"github.com/storacha/ipni-publisher/pkg/store"
"github.com/storacha/storage/pkg/build"
"github.com/storacha/storage/pkg/service/blobs"
"github.com/storacha/storage/pkg/service/claims"
"github.com/storacha/storage/pkg/service/publisher"
Expand Down Expand Up @@ -75,7 +76,7 @@ func NewServer(service storage.Service) (*http.ServeMux, error) {
// NewHandler displays version info.
func NewHandler(id principal.Signer) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("🔥 storage v0.0.0\n"))
w.Write([]byte(fmt.Sprintf("🔥 storage %s\n", build.Version)))
w.Write([]byte("- https://github.com/storacha/storage\n"))
w.Write([]byte(fmt.Sprintf("- %s", id.DID())))
}
Expand Down
3 changes: 3 additions & 0 deletions version.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"version": "v0.0.0"
}
Loading