diff --git a/.github/workflows/update-dependencies.yml b/.github/workflows/update-dependencies.yml new file mode 100644 index 000000000..5d650d1d9 --- /dev/null +++ b/.github/workflows/update-dependencies.yml @@ -0,0 +1,40 @@ +name: Update Dockerfile Dependencies +on: + schedule: + - cron: '0 * * * *' + workflow_dispatch: + +permissions: + contents: write + pull-requests: write + +jobs: + update: + name: update + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + ref: main + + - name: build dependency updater + run: cd dependency_updater && go build + + - name: run dependency updater + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: cd dependency_updater && ./dependency_updater --repo ../ --github-action true + + - name: load commit message/title .env file + uses: aarcangeli/load-dotenv@2afd907c7bb1c0324d22a6192693213867e77443 # v1.1.0 + with: + filenames: 'commit_message.env' + + - name: create pull request + uses: peter-evans/create-pull-request@271a8d0340265f705b14b6d32b9829c1cb33d45e # v7.0.8 + with: + title: ${{ env.TITLE }} + commit-message: ${{ env.TITLE }} + body: ${{ env.DESC }} + branch: run-dependency-updater + delete-branch: true \ No newline at end of file diff --git a/dependency_updater/dependency_updater.go b/dependency_updater/dependency_updater.go index 015627891..d465adfb2 100644 --- a/dependency_updater/dependency_updater.go +++ b/dependency_updater/dependency_updater.go @@ -57,9 +57,14 @@ func main() { Usage: "Stages updater changes and creates commit message", Required: false, }, + &cli.BoolFlag{ + Name: "github-action", + Usage: "Specifies whether tool is being used through github action workflow", + Required: false, + }, }, Action: func(ctx context.Context, cmd *cli.Command) error { - err := updater(string(cmd.String("token")), string(cmd.String("repo")), cmd.Bool("commit")) + err := updater(string(cmd.String("token")), string(cmd.String("repo")), cmd.Bool("commit"), cmd.Bool("github-action")) if err != nil { return fmt.Errorf("error running updater: %s", err) } @@ -72,7 +77,7 @@ func main() { } } -func updater(token string, repoPath string, commit bool) error { +func updater(token string, repoPath string, commit bool, githubAction bool) error { var err error var dependencies Dependencies var updatedDependencies []VersionUpdateInfo @@ -111,8 +116,8 @@ func updater(token string, repoPath string, commit bool) error { } } - if commit && updatedDependencies != nil { - err := createCommitMessage(updatedDependencies) + if (commit && updatedDependencies != nil) || (githubAction && updatedDependencies != nil) { + err := createCommitMessage(updatedDependencies, repoPath, githubAction) if err != nil { return fmt.Errorf("error creating commit message: %s", err) } @@ -126,7 +131,7 @@ func updater(token string, repoPath string, commit bool) error { return nil } -func createCommitMessage(updatedDependencies []VersionUpdateInfo) error { +func createCommitMessage(updatedDependencies []VersionUpdateInfo, repoPath string, githubAction bool) error { var repos []string commitTitle := "chore: updated " commitDescription := "Updated dependencies for: \n" @@ -136,12 +141,20 @@ func createCommitMessage(updatedDependencies []VersionUpdateInfo) error { commitDescription += repo + " => " + tag + " (" + dependency.DiffUrl + ")" + "\n" repos = append(repos, repo) } + commitDescription = strings.TrimSuffix(commitDescription, "\n") - commitTitle += strings.Join(repos, ", ") - cmd := exec.Command("git", "commit", "-am", commitTitle, "-m", commitDescription) - - if err := cmd.Run(); err != nil { - return fmt.Errorf("error running git commit -m: %s", err) + if githubAction { + commitTitle += strings.Join(repos, ", ") + commitDescription = "\"" + commitDescription + "\"" + err := createGitMessageEnv(commitTitle, commitDescription, repoPath) + if err != nil { + return fmt.Errorf("error creating git commit message: %s", err) + } + } else if !githubAction { + cmd := exec.Command("git", "commit", "-am", commitTitle, "-m", commitDescription) + if err := cmd.Run(); err != nil { + return fmt.Errorf("error running git commit -m: %s", err) + } } return nil } @@ -220,11 +233,11 @@ func getVersionAndCommit(ctx context.Context, client *github.Client, dependencie if dependencies[dependencyType].Tracking == "tag" { versionCommit, _, err := client.Repositories.GetCommit( - ctx, - dependencies[dependencyType].Owner, - dependencies[dependencyType].Repo, - "refs/tags/"+*version.TagName, - &github.ListOptions{}) + ctx, + dependencies[dependencyType].Owner, + dependencies[dependencyType].Repo, + "refs/tags/"+*version.TagName, + &github.ListOptions{}) if err != nil { return "", "", VersionUpdateInfo{}, fmt.Errorf("error getting commit for "+dependencyType+": %s", err) } @@ -264,7 +277,7 @@ func updateVersionTagAndCommit( if err != nil { return fmt.Errorf("error writing to versions "+dependencyType+": %s", err) } - + return nil } @@ -318,6 +331,21 @@ func createVersionsEnv(repoPath string, dependencies Dependencies) error { return nil } +func createGitMessageEnv(title string, description string, repoPath string) error { + file, err := os.Create(repoPath + "/commit_message.env") + if err != nil { + return fmt.Errorf("error creating git_commit_message.env file: %s", err) + } + defer file.Close() + + envString := "export TITLE=" + title + "\nexport DESC=" + description + _, err = file.WriteString(envString) + if err != nil { + return fmt.Errorf("error writing to git_commit_message.env file: %s", err) + } + return nil +} + func generateGithubRepoUrl(dependencies Dependencies, dependencyType string) string { return "https://github.com/" + dependencies[dependencyType].Owner + "/" + dependencies[dependencyType].Repo -} \ No newline at end of file +}