From 17f7ed47da9a442841b120ba24490e91e3aabbec Mon Sep 17 00:00:00 2001 From: Master Engineer <104206186+mastereng12@users.noreply.github.com> Date: Mon, 29 Apr 2024 21:44:10 +0100 Subject: [PATCH] feat: setup the release CICD and github workflow (#34) * github workflow * minor update --- .github/CODEOWNERS | 7 +++ .github/dependabot.yml | 7 +++ .github/pull_request_template.md | 12 +++++ .github/workflows/lint.yml | 27 ++++++++++ .github/workflows/push-docker.yml | 28 +++++++++++ .github/workflows/tag-release.yml | 82 +++++++++++++++++++++++++++++++ .github/workflows/test.yml | 24 +++++++++ Dockerfile | 19 +++++++ Makefile | 19 +++++++ 9 files changed, 225 insertions(+) create mode 100644 .github/CODEOWNERS create mode 100644 .github/dependabot.yml create mode 100644 .github/pull_request_template.md create mode 100644 .github/workflows/lint.yml create mode 100644 .github/workflows/push-docker.yml create mode 100644 .github/workflows/tag-release.yml create mode 100644 .github/workflows/test.yml create mode 100644 Dockerfile diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000..873bab1 --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1,7 @@ +# CODEOWNERS: https://help.github.com/articles/about-codeowners/ + +# NOTE: Order is important; the last matching pattern takes the most precedence + +# Primary repo maintainers + +* @Lagrange-Labs/lsc-core-dev diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..a48452c --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,7 @@ +--- +version: 2 +updates: + - package-ecosystem: "gomod" + directory: "/" + schedule: + interval: "daily" diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md new file mode 100644 index 0000000..e7e6b04 --- /dev/null +++ b/.github/pull_request_template.md @@ -0,0 +1,12 @@ +## Context + +Closes: #XXXX + + + +--- + +## Reviewers + +- @XXX +- @YYY \ No newline at end of file diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..d89acac --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,27 @@ +name: Lint +# Lint runs golangci-lint over the entire repository +# This workflow is run on every pull request and push to master +# The `golangci` will pass without running if no *.{go, mod, sum} files have been changed. +on: + pull_request: + push: + branches: + - develop + - main + tags: + - "v[0-9]+.[0-9]+.[0-9]+*" # Push events to matching tags v1.0.0, v1.0.1, v1.1.0, v2.0.0, etc. + +jobs: + golangci: + name: golangci-lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-go@v4 + with: + go-version: 1.21 + - name: golangci-lint + uses: golangci/golangci-lint-action@v3 + with: + version: v1.55.2 + args: --timeout=10m diff --git a/.github/workflows/push-docker.yml b/.github/workflows/push-docker.yml new file mode 100644 index 0000000..c5247f8 --- /dev/null +++ b/.github/workflows/push-docker.yml @@ -0,0 +1,28 @@ +name: Push Docker Image +on: + push: + branches: + - develop + - main + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - name: Login to DockerHub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + - name: Build and push + id: docker_build + uses: docker/build-push-action@v5 + with: + context: . + platforms: linux/amd64,linux/arm64 + push: true + tags: | + lagrangelabs/lagrange-cli:${GITHUB_REF##*/} diff --git a/.github/workflows/tag-release.yml b/.github/workflows/tag-release.yml new file mode 100644 index 0000000..236a997 --- /dev/null +++ b/.github/workflows/tag-release.yml @@ -0,0 +1,82 @@ +name: Auto Tag and Push Docker Release Image + +on: + pull_request: + types: [closed] + branches: + - release/v* + +jobs: + auto-tag-and-push: + if: github.event.pull_request.merged == true + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Identify and Create New Tag + id: new_tag + run: | + # Extract major and minor version from the branch name + BRANCH_NAME="${GITHUB_REF#refs/heads/}" + VERSION_PREFIX=$(echo $BRANCH_NAME | sed -E 's/release\/v([0-9]+\.[0-9]+).*/\1/') + # Fetch tags and find the latest tag with the same major and minor version + git fetch --tags + LATEST_TAG=$(git tag -l "v$VERSION_PREFIX.*" | sort -V | tail -n1) + echo "Latest tag for version $VERSION_PREFIX: $LATEST_TAG" + # Check if a tag was found, if not, start with x.0 + if [ -z "$LATEST_TAG" ]; then + NEW_TAG="v${VERSION_PREFIX}.0" + else + # Increment the patch version + NEW_TAG=$(echo $LATEST_TAG | awk -F. '{print $1"."$2"."$3+1}') + fi + + echo "New tag: $NEW_TAG" + + # Create and push the new tag + git tag $NEW_TAG + git push origin $NEW_TAG + echo "::set-output name=tag::$NEW_TAG" + + - name: Get Title For Release + id: get_pr_details + run: | + # Fetch the full commit message of the most recent commit + FULL_COMMIT_MESSAGE=$(git log -1 --pretty=format:%B) + echo "Full commit message: $FULL_COMMIT_MESSAGE" + + # Extract the main title from the commit message + # This regex captures the string before the first "(" and removes trailing spaces + PR_TITLE=$(echo "$FULL_COMMIT_MESSAGE" | sed -n '1p' | sed 's/ *([^()]*) *//g') + echo "::set-output name=title::$PR_TITLE" + + - name: Create Release + uses: actions/create-release@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + tag_name: ${{ steps.new_tag.outputs.tag }} + release_name: ${{ steps.get_pr_details.outputs.title }} + draft: false + prerelease: false + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Login to DockerHub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Build and push + id: docker_build + uses: docker/build-push-action@v5 + with: + context: . + platforms: linux/amd64,linux/arm64 + push: true + tags: | + lagrangelabs/lagrange-cli:${{ steps.new_tag.outputs.tag }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..f744e4a --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,24 @@ +name: Test +on: + push: + branches: + - develop + - main + tags: + - "v[0-9]+.[0-9]+.[0-9]+*" # Push events to matching tags v1.0.0, v1.0.1, v1.1.0, v2.0.0, etc. + pull_request: + +jobs: + Test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-go@v4 + with: + go-version: "1.21" # The Go version to download (if necessary) and use. + - name: test & coverage report creation + run: | + make test + env: + CGO_CFLAGS: "-O -D__BLST_PORTABLE__" + CGO_CFLAGS_ALLOW: "-O -D__BLST_PORTABLE__" diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..8540337 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +# CONTAINER FOR BUILDING BINARY +FROM golang:1.21-alpine AS build + +RUN apk add --no-cache --update gcc g++ make + +ENV CGO_CFLAGS="-O -D__BLST_PORTABLE__" +ENV CGO_CFLAGS_ALLOW="-O -D__BLST_PORTABLE__" + +# INSTALL DEPENDENCIES +COPY go.mod go.sum /src/ +RUN cd /src && go mod download + +# BUILD BINARY +COPY . /src +RUN cd /src && make build + +FROM alpine:edge +COPY --from=build /src/dist/lagrange-cli /app/lagrange-cli +CMD ["/bin/sh", "-c", "/app/lagrange-cli version"] diff --git a/Makefile b/Makefile index 975ce26..f9a7a6e 100644 --- a/Makefile +++ b/Makefile @@ -26,3 +26,22 @@ build: ## Builds the binary locally into ./dist scgen: # Generate the go bindings for the smart contracts @ cd scinterface && sh generator.sh +# Linting, Teseting, Benchmarking +golangci_lint_cmd=github.com/golangci/golangci-lint/cmd/golangci-lint@v1.55.2 + +install-linter: + @echo "--> Installing linter" + @go install $(golangci_lint_cmd) + +lint: + @echo "--> Running linter" + @ $$(go env GOPATH)/bin/golangci-lint run --timeout=10m +.PHONY: lint install-linter + +test: + trap '$(STOP)' EXIT; go test ./... --timeout=10m +.PHONY: test + +docker-build: ## Builds a docker image with the cli binary + docker build -t lagrange-cli -f ./Dockerfile . +.PHONY: docker-build \ No newline at end of file