From 81c2a7a2b2dc712eb67f148efc807e29fd1b56d7 Mon Sep 17 00:00:00 2001 From: Robert Fratto Date: Fri, 13 Oct 2023 08:55:51 -0400 Subject: [PATCH] add update-file.yml --- .github/actions/next-release-cycle/Dockerfile | 8 +++ .github/actions/next-release-cycle/action.yml | 14 ++++ .github/actions/next-release-cycle/go.mod | 10 +++ .github/actions/next-release-cycle/go.sum | 10 +++ .github/actions/next-release-cycle/main.go | 64 +++++++++++++++++++ .github/workflows/start-new-release-cycle.yml | 48 ++++++++++++++ 6 files changed, 154 insertions(+) create mode 100644 .github/actions/next-release-cycle/Dockerfile create mode 100644 .github/actions/next-release-cycle/action.yml create mode 100644 .github/actions/next-release-cycle/go.mod create mode 100644 .github/actions/next-release-cycle/go.sum create mode 100644 .github/actions/next-release-cycle/main.go create mode 100644 .github/workflows/start-new-release-cycle.yml diff --git a/.github/actions/next-release-cycle/Dockerfile b/.github/actions/next-release-cycle/Dockerfile new file mode 100644 index 000000000000..c15b0361366c --- /dev/null +++ b/.github/actions/next-release-cycle/Dockerfile @@ -0,0 +1,8 @@ +FROM golang:1.21-alpine3.18 + +COPY . /src +WORKDIR /src + +RUN go build -o /bin/next-release-cycle . + +ENTRYPOINT ["/bin/next-release-cycle"] diff --git a/.github/actions/next-release-cycle/action.yml b/.github/actions/next-release-cycle/action.yml new file mode 100644 index 000000000000..83c923cc79b0 --- /dev/null +++ b/.github/actions/next-release-cycle/action.yml @@ -0,0 +1,14 @@ +name: Next release cycle +description: Determine the next release cycle given an input. +inputs: + branch-name: + description: Branch name of current release cycle. + required: true +outputs: + next-cycle: + description: Next release cycle (formatted as a version number). +runs: + using: 'docker' + image: Dockerfile + args: + - ${{ inputs.branch-name }} diff --git a/.github/actions/next-release-cycle/go.mod b/.github/actions/next-release-cycle/go.mod new file mode 100644 index 000000000000..445e02731b34 --- /dev/null +++ b/.github/actions/next-release-cycle/go.mod @@ -0,0 +1,10 @@ +module github.com/grafana/agent/.github/actions/next-release-cycle + +go 1.21.0 + +require ( + github.com/Masterminds/semver/v3 v3.2.1 // indirect + github.com/blang/semver/v4 v4.0.0 // indirect + github.com/coreos/go-semver v0.3.1 // indirect + golang.org/x/mod v0.13.0 // indirect +) diff --git a/.github/actions/next-release-cycle/go.sum b/.github/actions/next-release-cycle/go.sum new file mode 100644 index 000000000000..be73539077a6 --- /dev/null +++ b/.github/actions/next-release-cycle/go.sum @@ -0,0 +1,10 @@ +github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0= +github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ= +github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM= +github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ= +github.com/coreos/go-semver v0.3.1 h1:yi21YpKnrx1gt5R+la8n5WgS0kCrsPp33dmEyHReZr4= +github.com/coreos/go-semver v0.3.1/go.mod h1:irMmmIw/7yzSRPWryHsK7EYSg09caPQL03VsM8rvUec= +golang.org/x/mod v0.13.0 h1:I/DsJXRlw/8l/0c24sM9yb0T4z9liZTduXvdAWYiysY= +golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/.github/actions/next-release-cycle/main.go b/.github/actions/next-release-cycle/main.go new file mode 100644 index 000000000000..b59bcda444df --- /dev/null +++ b/.github/actions/next-release-cycle/main.go @@ -0,0 +1,64 @@ +package main + +import ( + "fmt" + "log" + "os" + "strings" + + "github.com/Masterminds/semver/v3" +) + +func main() { + if err := run(); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } +} + +func run() error { + if len(os.Args) != 2 { + return fmt.Errorf("usage: next-release-cycle ") + } + + branchName := strings.TrimPrefix(os.Args[1], "release-") + log.Println("Determing release cycle after", branchName) + + currentVersion, err := semver.NewVersion(branchName) + if err != nil { + return err + } + + nextVersion := semver.New( + currentVersion.Major(), + currentVersion.Minor()+1, + 0, + "", + "", + ) + nextVersionText := "v" + nextVersion.String() + + // Set the next cycle as a variable in the GitHub Actions environment. + if githubOutputFile := os.Getenv("GITHUB_OUTPUT"); githubOutputFile != "" { + setVariable := fmt.Sprintf("next-cycle=%s\n", nextVersionText) + if err := appendFile(githubOutputFile, []byte(setVariable)); err != nil { + return err + } + } + + fmt.Fprintln(os.Stdout, nextVersionText) + return nil +} + +// appendFile appends data to the file named name. If the file doesn't exist, +// an error is returned. +func appendFile(name string, data []byte) error { + f, err := os.OpenFile(name, os.O_APPEND|os.O_WRONLY, 0644) + if err != nil { + return err + } + defer f.Close() + + _, err = f.Write(data) + return err +} diff --git a/.github/workflows/start-new-release-cycle.yml b/.github/workflows/start-new-release-cycle.yml new file mode 100644 index 000000000000..474fc5fffe4d --- /dev/null +++ b/.github/workflows/start-new-release-cycle.yml @@ -0,0 +1,48 @@ +name: Start new release cycle +on: + workflow_dispatch: {} + create: + branches: + - 'release-v*' +permissions: + contents: write + pull-requests: write +jobs: + update-file: + if: ${{ startsWith(github.ref, 'refs/heads/release-v')}} + runs-on: ubuntu-latest + steps: + - name: "Check out main branch" + uses: "actions/checkout@v4" + with: + ref: main + - name: "Determine new release cycle" + id: next-release-cycle + uses: "./.github/actions/next-release-cycle" + with: + branch-name: ${{ github.ref_name }} + - name: "Update version files" + run: | + echo -n "${{ steps.next-release-cycle.outputs.next-cycle }}" > tools/gen-versioned-files/agent-version.txt + make generate-versioned-files + git add . + + - name: "Commit and push" + run: | + git config --local user.name "${GITHUB_ACTOR}" + git config --local user.email "${GITHUB_ACTOR}@users.noreply.github.com" + + BRANCH=update-date-$(date +%s) + git checkout -b $BRANCH + + git commit -F - <