From 67529ef453dc87e3c899aa975c192d0e8bafd7b2 Mon Sep 17 00:00:00 2001 From: pschork <354473+pschork@users.noreply.github.com> Date: Mon, 3 Jun 2024 16:54:33 -0700 Subject: [PATCH] Automatic semantic versioning (#593) --- .../docker-publish-opr-node-images.yaml | 99 ------------------- .github/workflows/docker-publish-release.yaml | 78 +++++++++++++++ GitVersion.yml | 2 +- Makefile | 26 +++++ docker-compose-release.yaml | 12 +++ node/Makefile | 20 ++-- 6 files changed, 131 insertions(+), 106 deletions(-) delete mode 100644 .github/workflows/docker-publish-opr-node-images.yaml create mode 100644 .github/workflows/docker-publish-release.yaml create mode 100644 docker-compose-release.yaml diff --git a/.github/workflows/docker-publish-opr-node-images.yaml b/.github/workflows/docker-publish-opr-node-images.yaml deleted file mode 100644 index ed3aed9cc..000000000 --- a/.github/workflows/docker-publish-opr-node-images.yaml +++ /dev/null @@ -1,99 +0,0 @@ -name: Push Public Docker images to GHCR with Caching - -on: - workflow_dispatch: - inputs: - commit_sha: - description: 'Specific Commit SHA (Required)' - required: true - version: - description: 'Version (Required)' - required: true - gitcommit: - description: 'GitCommit (Required)' - required: true - release_tag: - description: 'Release Tag (Optional)' - required: false - default: '' - -env: - REGISTRY: ghcr.io - CACHE-FROM: /tmp/.buildx-cache - CACHE-TO: /tmp/.buildx-cache-new - -jobs: - build-and-push: - runs-on: ubuntu-latest - permissions: - contents: read - packages: write - steps: - - name: Checkout repository at specified commit - uses: actions/checkout@v3 - with: - ref: ${{ github.event.inputs.commit_sha }} - - name: Get Commit Date - id: get_date - run: | - GIT_DATE=$(git log -1 --format=%cd --date=format:'%Y-%m-%d' ${{ github.event.inputs.gitcommit }} || date '+%Y-%m-%d') - echo "GIT_DATE=$GIT_DATE" >> $GITHUB_ENV - echo "::set-output name=gitDate::$GIT_DATE" - - - name: Setup Buildx - uses: docker/setup-buildx-action@v1 - with: - install: true - driver-opts: image=moby/buildkit:master - - - name: Cache Docker layers - uses: actions/cache@v2 - with: - path: /tmp/.buildx-cache - key: ${{ runner.os }}-buildx-${{ github.event.inputs.commit_sha }} - restore-keys: | - ${{ runner.os }}-buildx- - if: ${{ success() }} - - - name: Log into registry ${{ env.REGISTRY }} - uses: docker/login-action@v2 - with: - registry: ${{ env.REGISTRY }} - username: ${{ github.actor }} - password: ${{ secrets.GITHUB_TOKEN }} - if: ${{ success() }} - - - name: Set Tag Name - id: set_tag - run: echo "tag=${{ github.event.inputs.release_tag || github.event.inputs.commit_sha }}" >> $GITHUB_OUTPUT - if: ${{ success() }} - - - name: Build and Push Opr Node Image - uses: docker/build-push-action@v2 - with: - file: ./node/cmd/Dockerfile - push: true - tags: ${{ env.REGISTRY }}/layr-labs/eigenda/opr-node:${{ steps.set_tag.outputs.tag }} - cache-from: type=local,src=/tmp/.buildx-cache - cache-to: type=local,dest=/tmp/.buildx-cache-new - build-args: SEMVER=${{ github.event.inputs.version }},GITCOMMIT=${{ github.event.inputs.gitcommit }},GITDATE=${{ steps.get_date.outputs.gitDate }} - if: ${{ success() }} - - - name: Build and Push NodePlugin Image - uses: docker/build-push-action@v2 - with: - context: . - file: ./node/plugin/cmd/Dockerfile - push: true - tags: ${{ env.REGISTRY }}/layr-labs/eigenda/opr-nodeplugin:${{ steps.set_tag.outputs.tag }} - cache-from: type=local,src=${{ env.CACHE-FROM }} - cache-to: type=local,dest=${{ env.CACHE-TO }} - if: ${{ success() }} - - - name: Update cache - uses: actions/cache@v2 - with: - path: ${{ env.CACHE-TO }} - key: ${{ runner.os }}-buildx-${{ steps.set_tag.outputs.tag }} - restore-keys: | - ${{ runner.os }}-buildx- \ No newline at end of file diff --git a/.github/workflows/docker-publish-release.yaml b/.github/workflows/docker-publish-release.yaml new file mode 100644 index 000000000..60f19ed02 --- /dev/null +++ b/.github/workflows/docker-publish-release.yaml @@ -0,0 +1,78 @@ +name: docker-publish-release + +on: + push: + tags: + - v* + pull_request: + workflow_dispatch: + inputs: + force: + description: "Force untagged release (expert mode)" + required: false + default: false + type: boolean + +env: + REGISTRY: ghcr.io + CACHE-FROM: /tmp/.buildx-cache + CACHE-TO: /tmp/.buildx-cache-new + +jobs: + build-and-push: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Install GitVersion + uses: gittools/actions/gitversion/setup@v1.1.1 + with: + versionSpec: '5.x' + + - name: Determine SemVer + uses: gittools/actions/gitversion/execute@v1.1.1 + with: + useConfigFile: true + + - run: | + echo "SemVer ${{ env.fullSemVer }} Forced ${{ github.event.inputs.force }}" + name: Display SemVer + + - name: Setup Buildx + uses: docker/setup-buildx-action@v1 + with: + install: true + driver-opts: image=moby/buildkit:master + + - name: Cache docker layers + uses: actions/cache@v2 + with: + path: /tmp/.buildx-cache + key: ${{ runner.os }}-buildx-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-buildx- + if: ${{ success() }} + + - name: Log into registry ${{ env.REGISTRY }} + uses: docker/login-action@v2 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + if: ${{ success() }} + + # Build And Push Image + - name: Build docker image release + run: make docker-release-build + if: ${{ success() }} + + # Publish if release is tagged or force == true + - name: Push docker image release + run: make docker-release-push + if: startsWith(github.ref, 'refs/tags/v') || github.event.inputs.force == 'true' diff --git a/GitVersion.yml b/GitVersion.yml index e596af732..52981c4ac 100644 --- a/GitVersion.yml +++ b/GitVersion.yml @@ -19,7 +19,7 @@ branches: increment: None prevent-increment-of-merged-branch-version: true track-merge-target: false - regex: ^releases?[/-] + regex: ^v*|^releases?[/-] source-branches: - main - release diff --git a/Makefile b/Makefile index 97420a3ee..c40ad271c 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,22 @@ .PHONY: compile-el compile-dl clean protoc lint build unit-tests integration-tests-churner integration-tests-indexer integration-tests-inabox integration-tests-inabox-nochurner integration-tests-graph-indexer +ifeq ($(wildcard .git/*),) +$(warning semver disabled - building from release zip) +GITCOMMIT := "" +GITDATE := "" +SEMVER := $(shell basename $(CURDIR)) +else +GITCOMMIT := $(shell git rev-parse --short HEAD) +GITDATE := $(shell git log -1 --format=%cd --date=unix) +SEMVER := $(shell docker run --rm --volume "$(PWD):/repo" gittools/gitversion:5.12.0 /repo -output json -showvariable SemVer) +ifeq ($(SEMVER), ) +$(warning semver disabled - docker not installed) +SEMVER := "0.0.0" +endif +endif + +RELEASE_TAG := $(or $(RELEASE_TAG),latest) + PROTOS := ./api/proto PROTOS_DISPERSER := ./disperser/api/proto PROTO_GEN := ./api/grpc @@ -75,3 +92,12 @@ integration-tests-graph-indexer: integration-tests-dataapi: make dataapi-build go test -v ./disperser/dataapi + +docker-release-build: + RELEASE_TAG=${SEMVER} docker compose -f docker-compose-release.yaml build --build-arg SEMVER=${SEMVER} --build-arg GITCOMMIT=${GITCOMMIT} --build-arg GITDATE=${GITDATE} + +docker-release-push: + RELEASE_TAG=${SEMVER} docker compose -f docker-compose-release.yaml push + +semver: + echo "${SEMVER}" diff --git a/docker-compose-release.yaml b/docker-compose-release.yaml new file mode 100644 index 000000000..a6e74e4ec --- /dev/null +++ b/docker-compose-release.yaml @@ -0,0 +1,12 @@ +# This file is used for building and pushing images +services: + node: + build: + context: . + dockerfile: node/cmd/Dockerfile + image: ghcr.io/layr-labs/eigenda/opr-node:${RELEASE_TAG} + nodeplugin: + build: + context: . + dockerfile: node/plugin/cmd/Dockerfile + image: ghcr.io/layr-labs/eigenda/opr-nodeplugin:${RELEASE_TAG} diff --git a/node/Makefile b/node/Makefile index 5b91ed151..6b2715211 100644 --- a/node/Makefile +++ b/node/Makefile @@ -1,12 +1,20 @@ +ifeq ($(wildcard ../.git/*),) +$(warning semver disabled - building from release zip) +GITCOMMIT := "" +GITDATE := "" +SEMVER := $(shell basename $(CURDIR)) +else GITCOMMIT := $(shell git rev-parse --short HEAD) GITDATE := $(shell git log -1 --format=%cd --date=unix) - -# GitVersion provides the semantic versioning for the project. -SEMVER := $(shell docker run --rm --volume "${PWD}/../:/repo" gittools/gitversion:5.12.0 /repo -output json -showvariable SemVer) +SEMVER := $(shell docker run --rm --volume "$(PWD)/../:/repo" gittools/gitversion:5.12.0 /repo -output json -showvariable SemVer) ifeq ($(SEMVER), ) -SEMVER = "0.0.0" # Fallback if docker is not installed or gitversion fails +$(warning semver disabled - docker not installed) +SEMVER := "0.0.0" +endif endif +RELEASE_TAG := $(or $(RELEASE_TAG),latest) + build: clean go mod tidy go build -o ./bin/node ./cmd @@ -17,10 +25,10 @@ clean: docker: docker-node docker-plugin docker-node: - cd ../ && docker build --build-arg SEMVER=${SEMVER} --build-arg GITCOMMIT=${GITCOMMIT} --build-arg GITDATE=${GITDATE} . -t opr-node:${SEMVER} -t opr-node:latest -f node/cmd/Dockerfile + cd ../ && docker build --build-arg SEMVER=${SEMVER} --build-arg GITCOMMIT=${GITCOMMIT} --build-arg GITDATE=${GITDATE} . -t opr-node:${SEMVER} -t opr-node:${RELEASE_TAG} -f node/cmd/Dockerfile docker-plugin: - cd ../ && docker build --build-arg SEMVER=${SEMVER} --build-arg GITCOMMIT=${GITCOMMIT} --build-arg GITDATE=${GITDATE} . -t opr-nodeplugin:${SEMVER} -t opr-nodeplugin:latest -f node/plugin/cmd/Dockerfile + cd ../ && docker build --build-arg SEMVER=${SEMVER} --build-arg GITCOMMIT=${GITCOMMIT} --build-arg GITDATE=${GITDATE} . -t opr-nodeplugin:${SEMVER} -t opr-nodeplugin:${RELEASE_TAG} -f node/plugin/cmd/Dockerfile semver: echo "${SEMVER}"