From 06d88ca10903956c79bd41d904bbfde0e23ff113 Mon Sep 17 00:00:00 2001 From: Tolya Korniltsev Date: Thu, 12 Dec 2024 09:27:06 +0700 Subject: [PATCH] chore(examples): add cron job to update examples (#3760) --- .github/workflows/update-examples-cron.yml | 27 ++++++++++++++++++ Makefile.examples | 16 ++++++++++- tools/update_examples.Dockerfile | 33 ++++++++++++++++++++++ tools/update_examples.go | 32 ++++++++++----------- 4 files changed, 90 insertions(+), 18 deletions(-) create mode 100644 .github/workflows/update-examples-cron.yml create mode 100644 tools/update_examples.Dockerfile diff --git a/.github/workflows/update-examples-cron.yml b/.github/workflows/update-examples-cron.yml new file mode 100644 index 0000000000..cf8ee4cbda --- /dev/null +++ b/.github/workflows/update-examples-cron.yml @@ -0,0 +1,27 @@ +name: Update Examples Cron + +on: + schedule: + - cron: '0 0 * * *' + workflow_dispatch: + +permissions: + contents: write + pull-requests: write + +jobs: + update-examples-cron: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + persist-credentials: false + - run: | + make tools/update_examples + if ! git diff --exit-code; + then + make tools/update_examples_pr + fi + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GITHUB_REPOSITORY: ${{ github.repository }} \ No newline at end of file diff --git a/Makefile.examples b/Makefile.examples index d51d00d00c..29e69d06f8 100644 --- a/Makefile.examples +++ b/Makefile.examples @@ -1,7 +1,21 @@ +GITHUB_REPOSITORY ?= grafana/pyroscope .PHONY: tools/update_examples tools/update_examples: - go run tools/update_examples.go + docker build -t update_pyroscope_examples -f tools/update_examples.Dockerfile tools + docker run --rm -e GITHUB_TOKEN=$(GITHUB_TOKEN) -v$(shell pwd):/pyroscope -w /pyroscope update_pyroscope_examples bash -l -c "go run tools/update_examples.go" + +.PHONY: tools/update_examples_pr +tools/update_examples_pr: + git config --local user.name 'Pyroscope Bot' + git config --local user.email 'dmitry+bot@pyroscope.io' + git checkout -b gh_bot_examples_update + git add . + git commit -m "chore(examples): update examples" + git push -f https://x-access-token:$(GITHUB_TOKEN)@github.com/$(GITHUB_REPOSITORY).git gh_bot_examples_update + gh pr create --head gh_bot_examples_update --title "chore(examples): update examples" --body "make tools/update_examples" --repo $(GITHUB_REPOSITORY) || \ + gh pr edit gh_bot_examples_update --title "chore(examples): update examples" --body "make tools/update_examples" --repo $(GITHUB_REPOSITORY) + .PHONY: rideshare/docker/push rideshare/docker/push: rideshare/docker/push-golang rideshare/docker/push-loadgen rideshare/docker/push-python rideshare/docker/push-ruby rideshare/docker/push-dotnet rideshare/docker/push-java rideshare/docker/push-rust diff --git a/tools/update_examples.Dockerfile b/tools/update_examples.Dockerfile new file mode 100644 index 0000000000..2e91cb9ef2 --- /dev/null +++ b/tools/update_examples.Dockerfile @@ -0,0 +1,33 @@ + +ARG NODE_VERSION=node:18 + +FROM ${NODE_VERSION} AS node + + +FROM ubuntu:22.04 + +RUN apt-get update && apt-get -y install wget git build-essential libssl-dev libgmp-dev libyaml-dev zlib1g-dev curl \ + gawk autoconf automake bison libffi-dev libgdbm-dev libsqlite3-dev libtool pkg-config sqlite3 libncurses5-dev \ + libreadline-dev gnupg + +ARG GO_VERSION=1.22.10 +RUN wget https://go.dev/dl/go${GO_VERSION}.linux-amd64.tar.gz && \ + tar -C /usr/local -xzf go${GO_VERSION}.linux-amd64.tar.gz +ENV PATH=$PATH:/usr/local/go/bin + + + +ARG GH_VERSION=2.63.2 +RUN wget https://github.com/cli/cli/releases/download/v${GH_VERSION}/gh_${GH_VERSION}_linux_amd64.deb && \ + dpkg -i gh_${GH_VERSION}_linux_amd64.deb + +COPY --from=node /usr/local/lib /usr/local/lib +COPY --from=node /usr/local/bin /usr/local/bin +COPY --from=node /opt /opt + + +ENV PATH=$PATH:/usr/local/bin + +ARG RUBY_VERSION=3.2.2 +RUN curl -sSL https://get.rvm.io | bash +RUN /bin/bash -l -c "rvm install ruby-${RUBY_VERSION} && rvm --default use ruby-${RUBY_VERSION}" diff --git a/tools/update_examples.go b/tools/update_examples.go index c4e1489d77..6c49ffb056 100644 --- a/tools/update_examples.go +++ b/tools/update_examples.go @@ -9,17 +9,15 @@ import ( "os" "os/exec" "path/filepath" + "regexp" "slices" "strconv" "strings" - - "github.com/grafana/regexp" ) var ghToken string -// this program requires gh cli, bundle, go to be installed -// todo run it by cron & create a PR if needed +// this program requires ruby, bundle, yarn, go to be installed func main() { getGHToken() @@ -34,11 +32,11 @@ func main() { } func getGHToken() { - ghToken, _ = s.sh("gh auth token") + ghToken = os.Getenv("GITHUB_TOKEN") } func extractNodeJSVersion(tag Tag) *version { - re := regexp.MustCompile("(v)(\\d+).(\\d+).(\\d+)") + re := regexp.MustCompile(`(v)(\d+).(\d+).(\d+)`) match := re.FindStringSubmatch(tag.Name) if match != nil { if match[1] == "v" { @@ -73,19 +71,19 @@ func updateDotnet() { last := tags[len(tags)-1] fmt.Println(last) - reDockerGlibc := regexp.MustCompile("pyroscope/pyroscope-dotnet:\\d+\\.\\d+\\.\\d+-glibc") + reDockerGlibc := regexp.MustCompile(`pyroscope/pyroscope-dotnet:\d+\.\d+\.\d+-glibc`) replDockerGlibc := fmt.Sprintf("pyroscope/pyroscope-dotnet:%s-glibc", last.version()) replaceInplace(reDockerGlibc, "examples/language-sdk-instrumentation/dotnet/fast-slow/Dockerfile", replDockerGlibc) replaceInplace(reDockerGlibc, "examples/language-sdk-instrumentation/dotnet/rideshare/Dockerfile", replDockerGlibc) replaceInplace(reDockerGlibc, "examples/language-sdk-instrumentation/dotnet/web-new/Dockerfile", replDockerGlibc) replaceInplace(reDockerGlibc, "docs/sources/configure-client/language-sdks/dotnet.md", replDockerGlibc) - reDockerMusl := regexp.MustCompile("pyroscope/pyroscope-dotnet:\\d+\\.\\d+\\.\\d+-musl") + reDockerMusl := regexp.MustCompile(`pyroscope/pyroscope-dotnet:\d+\.\d+\.\d+-musl`) replDockerMusl := fmt.Sprintf("pyroscope/pyroscope-dotnet:%s-musl", last.version()) replaceInplace(reDockerMusl, "examples/language-sdk-instrumentation/dotnet/fast-slow/musl.Dockerfile", replDockerMusl) replaceInplace(reDockerMusl, "examples/language-sdk-instrumentation/dotnet/rideshare/musl.Dockerfile", replDockerMusl) - reUrl := regexp.MustCompile("https://github\\.com/grafana/pyroscope-dotnet/releases/download/v\\d+\\.\\d+\\.\\d+-pyroscope/pyroscope.\\d+\\.\\d+\\.\\d+-glibc-x86_64.tar.gz") + reUrl := regexp.MustCompile(`https://github\.com/grafana/pyroscope-dotnet/releases/download/v\d+\.\d+\.\d+-pyroscope/pyroscope.\d+\.\d+\.\d+-glibc-x86_64.tar.gz`) replUrl := fmt.Sprintf("https://github.com/grafana/pyroscope-dotnet/releases/download/v%s-pyroscope/pyroscope.%s-glibc-x86_64.tar.gz", last.version(), last.version()) replaceInplace(reUrl, "docs/sources/configure-client/language-sdks/dotnet.md", replUrl) } @@ -95,7 +93,7 @@ func updatePython() { last := tags[len(tags)-1] fmt.Println(last) - re := regexp.MustCompile("pyroscope-io==\\d+\\.\\d+\\.\\d+") + re := regexp.MustCompile(`pyroscope-io==\d+\.\d+\.\d+`) repl := fmt.Sprintf("pyroscope-io==%s", last.version()) replaceInplace(re, "examples/language-sdk-instrumentation/python/simple/requirements.txt", repl) replaceInplace(re, "examples/language-sdk-instrumentation/python/rideshare/flask/Dockerfile", repl) @@ -109,7 +107,7 @@ func updateRuby() { last := tags[len(tags)-1] fmt.Println(last) - re := regexp.MustCompile("gem ['\"]pyroscope['\"].*") + re := regexp.MustCompile(`gem ['"]pyroscope['"].*`) repl := fmt.Sprintf("gem 'pyroscope', '= %s'", last.version()) replaceInplace(re, "examples/language-sdk-instrumentation/ruby/rideshare/Gemfile", repl) replaceInplace(re, "examples/language-sdk-instrumentation/ruby/rideshare_rails/Gemfile", repl) @@ -124,18 +122,18 @@ func updateJava() { tags := getTagsV("grafana/pyroscope-java", extractGoVersion("")) last := tags[len(tags)-1] fmt.Println(last) - reJarURL := regexp.MustCompile("https://github\\.com/grafana/pyroscope-java/releases/download/(v\\d+\\.\\d+\\.\\d+)/pyroscope\\.jar") + reJarURL := regexp.MustCompile(`https://github\.com/grafana/pyroscope-java/releases/download/(v\d+\.\d+\.\d+)/pyroscope\.jar`) lastJarURL := "https://github.com/grafana/pyroscope-java/releases/download/" + last.versionV() + "/pyroscope.jar" replaceInplace(reJarURL, "examples/language-sdk-instrumentation/java/fib/Dockerfile", lastJarURL) replaceInplace(reJarURL, "examples/language-sdk-instrumentation/java/simple/Dockerfile", lastJarURL) replaceInplace(reJarURL, "examples/language-sdk-instrumentation/java/rideshare/Dockerfile", lastJarURL) - reGradelDep := regexp.MustCompile("implementation\\(\"io\\.pyroscope:agent:\\d+\\.\\d+\\.\\d+\"\\)") + reGradelDep := regexp.MustCompile(`implementation\("io\.pyroscope:agent:\d+\.\d+\.\d+"\)`) lastGradleDep := fmt.Sprintf("implementation(\"io.pyroscope:agent:%s\")", last.version()) replaceInplace(reGradelDep, "examples/language-sdk-instrumentation/java/rideshare/build.gradle.kts", lastGradleDep) replaceInplace(reGradelDep, "docs/sources/configure-client/language-sdks/java.md", lastGradleDep) - reMaven := regexp.MustCompile("\\d+\\.\\d+\\.\\d+") + reMaven := regexp.MustCompile(`\d+\.\d+\.\d+`) replMaven := fmt.Sprintf("%s", last.version()) replaceInplace(reMaven, "docs/sources/configure-client/language-sdks/java.md", replMaven) @@ -176,7 +174,7 @@ func updateGolang() { func extractGoVersion(module string) func(tag Tag) *version { return func(tag Tag) *version { - re := regexp.MustCompile("([^/]*)/?v(\\d+).(\\d+).(\\d+)") + re := regexp.MustCompile(`([^/]*)/?v(\d+).(\d+).(\d+)`) match := re.FindStringSubmatch(tag.Name) if match != nil { //fmt.Println(len(match), match) @@ -196,7 +194,7 @@ func extractGoVersion(module string) func(tag Tag) *version { func extractRSVersion(module string) func(tag Tag) *version { return func(tag Tag) *version { - re := regexp.MustCompile("(\\S+)-(\\d+).(\\d+).(\\d+)") + re := regexp.MustCompile(`(\S+)-(\d+).(\d+).(\d+)`) match := re.FindStringSubmatch(tag.Name) if match != nil { //fmt.Println(len(match), match) @@ -216,7 +214,7 @@ func extractRSVersion(module string) func(tag Tag) *version { func extractDotnetVersion() func(tag Tag) *version { return func(tag Tag) *version { - re := regexp.MustCompile("v(\\d+).(\\d+).(\\d+)-pyroscope") + re := regexp.MustCompile(`v(\d+).(\d+).(\d+)-pyroscope`) match := re.FindStringSubmatch(tag.Name) if match != nil { fmt.Println(len(match), match)