From 9a5a67a8027e3c5155cc3e0648e93040a78401b2 Mon Sep 17 00:00:00 2001 From: Gabe <7622243+decentralgabe@users.noreply.github.com> Date: Tue, 5 Dec 2023 23:40:46 +0100 Subject: [PATCH] add sha to build (#66) * add sha to build * swagger --- .github/workflows/image-publish.yml | 6 ++++-- README.md | 2 +- impl/README.md | 2 +- impl/build/Dockerfile | 10 ++++++++-- impl/cmd/main.go | 6 ++++++ impl/pkg/service/pkarr.go | 24 ++++++++++++++++++------ 6 files changed, 38 insertions(+), 12 deletions(-) diff --git a/.github/workflows/image-publish.yml b/.github/workflows/image-publish.yml index aa6f2377..87c1c75c 100644 --- a/.github/workflows/image-publish.yml +++ b/.github/workflows/image-publish.yml @@ -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 }} @@ -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 }} \ No newline at end of file diff --git a/README.md b/README.md index 84820b50..35c38bc6 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/impl/README.md b/impl/README.md index 37d9be56..65534b8e 100644 --- a/impl/README.md +++ b/impl/README.md @@ -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 diff --git a/impl/build/Dockerfile b/impl/build/Dockerfile index b64c01d5..e1299965 100644 --- a/impl/build/Dockerfile +++ b/impl/build/Dockerfile @@ -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 diff --git a/impl/cmd/main.go b/impl/cmd/main.go index 8a37320a..2f996430 100644 --- a/impl/cmd/main.go +++ b/impl/cmd/main.go @@ -14,6 +14,8 @@ import ( "github.com/TBD54566975/did-dht-method/pkg/server" ) +var commitHash string + // main godoc // // @title The DID DHT Service @@ -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()) } diff --git a/impl/pkg/service/pkarr.go b/impl/pkg/service/pkarr.go index 16e79e2a..b3c07cb0 100644 --- a/impl/pkg/service/pkarr.go +++ b/impl/pkg/service/pkarr.go @@ -173,7 +173,6 @@ 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 { @@ -181,7 +180,13 @@ func (s *PkarrService) GetPkarr(ctx context.Context, id string) (*GetPkarrRespon 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 @@ -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