Skip to content

Commit 75e5b2e

Browse files
authored
feat: version and CI release (#17)
Copy pasta from indexing service.
1 parent 17af5b8 commit 75e5b2e

13 files changed

+321
-1
lines changed

Diff for: .dockerignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.github
2+
docs
3+
.goreleaser.yaml
4+
*.md
5+
LICENSE.md

Diff for: .github/workflows/publish-ghcr.yml

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
name: Container
2+
3+
on:
4+
push:
5+
branches:
6+
- 'main'
7+
tags:
8+
- 'v*'
9+
workflow_run:
10+
workflows: [ Releaser ]
11+
types:
12+
- completed
13+
pull_request:
14+
15+
jobs:
16+
prepare-checkout:
17+
if: github.event_name != 'workflow_run' || github.event.workflow_run.conclusion == 'success'
18+
name: Prepare ref
19+
runs-on: ubuntu-latest
20+
outputs:
21+
ref: ${{ github.event_name != 'workflow_run' && github.ref || steps.releaser.outputs.version }}
22+
steps:
23+
- name: Get Ref from releaser
24+
id: releaser
25+
if: github.event_name == 'workflow_run'
26+
uses: ipdxco/unified-github-workflows/.github/actions/[email protected]
27+
with:
28+
artifacts-url: ${{ github.event.workflow_run.artifacts_url }}
29+
publish:
30+
name: Publish
31+
needs: [ prepare-checkout ]
32+
runs-on: ubuntu-latest
33+
permissions:
34+
contents: read
35+
packages: write
36+
steps:
37+
- name: Checkout
38+
uses: actions/checkout@v3
39+
with:
40+
ref: ${{ needs.prepare-checkout.outputs.ref }}
41+
- name: Set up Docker Buildx
42+
uses: docker/setup-buildx-action@v2
43+
- name: Log in to the Container registry
44+
uses: docker/login-action@v2
45+
with:
46+
registry: ghcr.io
47+
username: ${{ github.actor }}
48+
password: ${{ github.token }}
49+
- name: Extract metadata
50+
id: meta
51+
uses: docker/metadata-action@v4
52+
with:
53+
images: ghcr.io/${{ github.repository }}
54+
tags: |
55+
type=semver,pattern={{raw}}
56+
type=ref,event=branch
57+
type=raw,value=${{ needs.prepare-checkout.outputs.ref }}
58+
- name: Build and push Docker image
59+
uses: docker/build-push-action@v4
60+
with:
61+
context: .
62+
cache-from: type=gha
63+
cache-to: type=gha,mode=max
64+
push: ${{ github.event_name != 'pull_request' }}
65+
tags: ${{ steps.meta.outputs.tags }}
66+
labels: ${{ steps.meta.outputs.labels }}

Diff for: .github/workflows/release-binaries.yml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: GoReleaser
2+
on:
3+
release:
4+
types: [ published ]
5+
workflow_dispatch:
6+
workflow_run:
7+
workflows: [Releaser]
8+
types: [completed]
9+
jobs:
10+
bin-releaser:
11+
name: Release Binaries
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout
15+
uses: actions/checkout@v4
16+
with:
17+
fetch-depth: 0
18+
- name: Set up Go
19+
uses: actions/setup-go@v5
20+
with:
21+
go-version: "1.20.x"
22+
- name: Release Binaries
23+
uses: goreleaser/goreleaser-action@v5
24+
with:
25+
distribution: goreleaser
26+
version: latest
27+
args: release --clean
28+
env:
29+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Diff for: .github/workflows/release-check.yml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Release Checker
2+
3+
on:
4+
pull_request_target:
5+
paths: [ 'version.json' ]
6+
types: [ opened, synchronize, reopened, labeled, unlabeled ]
7+
workflow_dispatch:
8+
9+
permissions:
10+
contents: write
11+
pull-requests: write
12+
13+
concurrency:
14+
group: ${{ github.workflow }}-${{ github.ref }}
15+
cancel-in-progress: true
16+
17+
jobs:
18+
release-check:
19+
uses: ipdxco/unified-github-workflows/.github/workflows/[email protected]

Diff for: .github/workflows/releaser.yml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: Releaser
2+
3+
on:
4+
push:
5+
paths: [ 'version.json' ]
6+
workflow_dispatch:
7+
8+
permissions:
9+
contents: write
10+
11+
concurrency:
12+
group: ${{ github.workflow }}-${{ github.sha }}
13+
cancel-in-progress: true
14+
15+
jobs:
16+
releaser:
17+
uses: ipdxco/unified-github-workflows/.github/workflows/[email protected]

Diff for: .github/workflows/tagpush.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: Tag Push Checker
2+
3+
on:
4+
push:
5+
tags:
6+
- v*
7+
8+
permissions:
9+
contents: read
10+
issues: write
11+
12+
concurrency:
13+
group: ${{ github.workflow }}-${{ github.ref }}
14+
cancel-in-progress: true
15+
16+
jobs:
17+
releaser:
18+
uses: ipdxco/unified-github-workflows/.github/workflows/[email protected]

Diff for: .gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/cmd/storage
2+
/storage
3+
/build

Diff for: .goreleaser.yaml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
builds:
2+
- main: ./cmd
3+
binary: storage
4+
ldflags:
5+
# Sets the version variable in the build package to the build version prefixed with a 'v'
6+
# Sets the main.date to a static date for checksum verification. See https://goreleaser.com/customization/builds/#reproducible-builds.
7+
- -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}}
8+
goos:
9+
- linux
10+
- windows
11+
- darwin
12+
goarch:
13+
- 'amd64'
14+
- 'arm64'
15+
# Change to a static date for checksum verification. See https://goreleaser.com/customization/builds/#reproducible-builds.
16+
mod_timestamp: '{{.CommitTimestamp}}'
17+
env:
18+
- CGO_ENABLED=0
19+
universal_binaries:
20+
- replace: true
21+
archives:
22+
- format_overrides:
23+
- goos: windows
24+
format: zip
25+
- goos: darwin
26+
format: zip
27+
name_template: >-
28+
{{ .ProjectName }}_{{ .Version }}_
29+
{{- if eq .Os "darwin" }}mac_os
30+
{{- else }}{{ .Os }}{{ end }}_{{ .Arch }}
31+
release:
32+
mode: keep-existing
33+
changelog:
34+
skip: true

Diff for: Dockerfile

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM golang:1.23-bullseye as build
2+
3+
WORKDIR /go/src/storage
4+
5+
COPY go.* .
6+
RUN go mod download
7+
COPY . .
8+
9+
RUN CGO_ENABLED=0 go build -o /go/bin/storage ./cmd
10+
11+
FROM gcr.io/distroless/static-debian12
12+
COPY --from=build /go/bin/storage /usr/bin/
13+
14+
ENTRYPOINT ["/usr/bin/storage"]

Diff for: pkg/build/version.go

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package build
2+
3+
import (
4+
_ "embed"
5+
"encoding/json"
6+
"fmt"
7+
"os"
8+
9+
"github.com/storacha/storage/pkg/internal/revision"
10+
)
11+
12+
var (
13+
// version is the built version.
14+
// Set with ldflags in .goreleaser.yaml via -ldflags="-X github.com/storacha/storage/pkg/build.version=v{{.Version}}".
15+
version string
16+
// Version returns the current version of the application
17+
Version string
18+
// UserAgent is the user agent used for HTTP requests
19+
UserAgent string
20+
)
21+
22+
const (
23+
defaultVersion string = "v0.0.0" // Default version if not set by ldflags
24+
versionFile string = "version.json" // Version file path
25+
)
26+
27+
func init() {
28+
if version == "" {
29+
// This is being ran in development, try to grab the latest known version from the version.json file
30+
var err error
31+
version, err = readVersionFromFile()
32+
if err != nil {
33+
// Use the default version
34+
version = defaultVersion
35+
}
36+
}
37+
38+
Version = fmt.Sprintf("%s-%s", version, revision.Revision)
39+
UserAgent = fmt.Sprintf("storage/%s", Version)
40+
}
41+
42+
// versionJson is used to read the local version.json file
43+
type versionJSON struct {
44+
Version string `json:"version"`
45+
}
46+
47+
// readVersionFromFile reads the version from the version.json file.
48+
// Reading this should be fine in development since the version.json file
49+
// should be present in the project, I hope :)
50+
func readVersionFromFile() (string, error) {
51+
// Open file
52+
file, err := os.Open(versionFile)
53+
if err != nil {
54+
return "", err
55+
}
56+
defer file.Close()
57+
58+
// Decode json into struct
59+
decoder := json.NewDecoder(file)
60+
var vJSON versionJSON
61+
err = decoder.Decode(&vJSON)
62+
if err != nil {
63+
return "", err
64+
}
65+
66+
// Read version from json
67+
return vJSON.Version, nil
68+
}

Diff for: pkg/internal/revision/revision.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Package revision provides the vsc revision, embedded by the compiler, as a
2+
// global variable.
3+
package revision
4+
5+
import (
6+
"runtime/debug"
7+
)
8+
9+
// Revision returns the revision embedded by the compiler during build.
10+
// Suffixed with "-dirty" if modified.
11+
var Revision string
12+
13+
func init() {
14+
revision := "unknown"
15+
var dirty bool
16+
17+
// Get the revision from the build info
18+
bi, ok := debug.ReadBuildInfo()
19+
if !ok {
20+
Revision = revision
21+
return
22+
}
23+
24+
for _, bs := range bi.Settings {
25+
switch bs.Key {
26+
case "vcs.revision":
27+
revision = bs.Value
28+
if len(bs.Value) > 7 {
29+
revision = bs.Value[:7]
30+
}
31+
case "vcs.modified":
32+
if bs.Value == "true" {
33+
dirty = true
34+
}
35+
}
36+
}
37+
38+
if dirty {
39+
revision += "-dirty"
40+
}
41+
42+
Revision = revision
43+
}

Diff for: pkg/server/server.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
logging "github.com/ipfs/go-log/v2"
99
"github.com/storacha/go-ucanto/principal"
1010
"github.com/storacha/ipni-publisher/pkg/store"
11+
"github.com/storacha/storage/pkg/build"
1112
"github.com/storacha/storage/pkg/service/blobs"
1213
"github.com/storacha/storage/pkg/service/claims"
1314
"github.com/storacha/storage/pkg/service/publisher"
@@ -75,7 +76,7 @@ func NewServer(service storage.Service) (*http.ServeMux, error) {
7576
// NewHandler displays version info.
7677
func NewHandler(id principal.Signer) func(http.ResponseWriter, *http.Request) {
7778
return func(w http.ResponseWriter, r *http.Request) {
78-
w.Write([]byte("🔥 storage v0.0.0\n"))
79+
w.Write([]byte(fmt.Sprintf("🔥 storage %s\n", build.Version)))
7980
w.Write([]byte("- https://github.com/storacha/storage\n"))
8081
w.Write([]byte(fmt.Sprintf("- %s", id.DID())))
8182
}

Diff for: version.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"version": "v0.0.0"
3+
}

0 commit comments

Comments
 (0)