Skip to content

Commit

Permalink
add sha to build (#66)
Browse files Browse the repository at this point in the history
* add sha to build

* swagger
  • Loading branch information
decentralgabe authored Dec 5, 2023
1 parent 6d2aec3 commit 9a5a67a
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 12 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/image-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
uses: actions/checkout@v3

- name: Log in to the Container registry
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
Expand All @@ -53,10 +53,12 @@ jobs:
type=sha
- name: Build and push Docker image
uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
uses: docker/build-push-action@v5
with:
context: impl
file: impl/build/Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
GIT_COMMIT_HASH=${{ github.sha }}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ gateway server in Go.
To build and run the gateway server, from the `impl` directory run:

```
docker build . -t did-dht -f build/Dockerfile
docker build --build-arg GIT_COMMIT_HASH=$(git rev-parse HEAD) . -t did-dht -f build/Dockerfile
```

and then
Expand Down
2 changes: 1 addition & 1 deletion impl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ How it works:
Run:

```
docker build . -t did-dht -f build/Dockerfile
docker build --build-arg GIT_COMMIT_HASH=$(git rev-parse HEAD) . -t did-dht -f build/Dockerfile
```

and then
Expand Down
10 changes: 8 additions & 2 deletions impl/build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ RUN go mod download
# Copy code /to the container image.
COPY . ./

# Build the binary and call it "docker-ssi-service"
RUN go build -tags jwx_es256k -o /did-dht ./cmd
# Use ARG to declare the variable
ARG GIT_COMMIT_HASH

# Use ENV to set the environment variable
ENV GIT_COMMIT_HASH=$GIT_COMMIT_HASH

# Build using the environment variable
RUN go build -ldflags="-X main.commitHash=$GIT_COMMIT_HASH" -tags jwx_es256k -o /did-dht ./cmd

EXPOSE 8305

Expand Down
6 changes: 6 additions & 0 deletions impl/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/TBD54566975/did-dht-method/pkg/server"
)

var commitHash string

// main godoc
//
// @title The DID DHT Service
Expand All @@ -26,6 +28,10 @@ import (
func main() {
logrus.Info("Starting up...")

if commitHash != "" {
logrus.Infof("With commit: %s", commitHash)
}

if err := run(); err != nil {
logrus.Fatalf("main: error: %s", err.Error())
}
Expand Down
24 changes: 18 additions & 6 deletions impl/pkg/service/pkarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,20 @@ func (s *PkarrService) GetPkarr(ctx context.Context, id string) (*GetPkarrRespon
got, err := s.dht.GetFull(ctx, id)
if err != nil {
// try to resolve from storage before returning and error
// if we detect this and have the record we should republish to the DHT
logrus.WithError(err).Warnf("failed to get pkarr record[%s] from dht, attempting to resolve from storage", id)
record, err := s.db.ReadRecord(id)
if err != nil || record == nil {
logrus.WithError(err).Errorf("failed to resolve pkarr record[%s] from storage", id)
return nil, err
}
logrus.Debugf("resolved pkarr record[%s] from storage", id)
return fromPkarrRecord(*record)
resp, err := fromPkarrRecord(*record)
if err == nil {
if err = s.addRecordToCache(id, *resp); err != nil {
logrus.WithError(err).Errorf("failed to set pkarr record[%s] in cache", id)
}
}
return resp, nil
}

// prepare the record for return
Expand All @@ -200,15 +205,22 @@ func (s *PkarrService) GetPkarr(ctx context.Context, id string) (*GetPkarrRespon
}

// add the record to cache, do it here to avoid duplicate calculations
if err = s.addRecordToCache(id, resp); err != nil {
logrus.WithError(err).Errorf("failed to set pkarr record[%s] in cache", id)
}

return &resp, nil
}

func (s *PkarrService) addRecordToCache(id string, resp GetPkarrResponse) error {
recordBytes, err := json.Marshal(resp)
if err != nil {
return nil, util.LoggingErrorMsgf(err, "failed to marshal pkarr record[%s] for cache", id)
return err
}
if err = s.cache.Set(id, recordBytes); err != nil {
return nil, util.LoggingErrorMsgf(err, "failed to set pkarr record[%s] in cache", id)
return err
}

return &resp, nil
return nil
}

// TODO(gabe) make this more efficient. create a publish schedule based on each individual record, not all records
Expand Down

0 comments on commit 9a5a67a

Please sign in to comment.