Skip to content

Commit 6b16142

Browse files
authored
Dependency Updater: Github Action Workflow for Scheduled Updater Runs (#497)
* add github action to automatically run dependency updater * push * fix syntax * comment out time * test if push triggers action * change from on: push: to on: pull req: * change env action * change path * change path * change path to env * change path * un-ignore commit message * test env file location change * fix dependency updater build command * fix dependency runner * test if versions.json displays * change dependency updater path * check directory * check path * uncomment everything * test push to fork * test base/branch * remove pull request trigger * add workflow dispatch * test pr creation * revert dependencies yml back * set delete-branch to true * test if pr is created * fix on: pull_request * rename commit message env * fix env file name * comment author and committer * try adding token * set permissions * add read permissions * add more permissions * typo in pull request name * fix typo in ym * change checkout to main * fix syntax * change ref to pr branch * remove ref * change base to main * change base branch * remove base * remove base and set ref to main create test PR * add support for manually running updater, and change yml updater command * fix workflow typo * remove author + committer and schedule to run every hour * uncomment schedule * add versions next to commit hash * remove env echo
1 parent 56890f3 commit 6b16142

2 files changed

Lines changed: 85 additions & 17 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Update Dockerfile Dependencies
2+
on:
3+
schedule:
4+
- cron: '0 * * * *'
5+
workflow_dispatch:
6+
7+
permissions:
8+
contents: write
9+
pull-requests: write
10+
11+
jobs:
12+
update:
13+
name: update
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
17+
with:
18+
ref: main
19+
20+
- name: build dependency updater
21+
run: cd dependency_updater && go build
22+
23+
- name: run dependency updater
24+
env:
25+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
26+
run: cd dependency_updater && ./dependency_updater --repo ../ --github-action true
27+
28+
- name: load commit message/title .env file
29+
uses: aarcangeli/load-dotenv@2afd907c7bb1c0324d22a6192693213867e77443 # v1.1.0
30+
with:
31+
filenames: 'commit_message.env'
32+
33+
- name: create pull request
34+
uses: peter-evans/create-pull-request@271a8d0340265f705b14b6d32b9829c1cb33d45e # v7.0.8
35+
with:
36+
title: ${{ env.TITLE }}
37+
commit-message: ${{ env.TITLE }}
38+
body: ${{ env.DESC }}
39+
branch: run-dependency-updater
40+
delete-branch: true

dependency_updater/dependency_updater.go

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,14 @@ func main() {
5757
Usage: "Stages updater changes and creates commit message",
5858
Required: false,
5959
},
60+
&cli.BoolFlag{
61+
Name: "github-action",
62+
Usage: "Specifies whether tool is being used through github action workflow",
63+
Required: false,
64+
},
6065
},
6166
Action: func(ctx context.Context, cmd *cli.Command) error {
62-
err := updater(string(cmd.String("token")), string(cmd.String("repo")), cmd.Bool("commit"))
67+
err := updater(string(cmd.String("token")), string(cmd.String("repo")), cmd.Bool("commit"), cmd.Bool("github-action"))
6368
if err != nil {
6469
return fmt.Errorf("error running updater: %s", err)
6570
}
@@ -72,7 +77,7 @@ func main() {
7277
}
7378
}
7479

75-
func updater(token string, repoPath string, commit bool) error {
80+
func updater(token string, repoPath string, commit bool, githubAction bool) error {
7681
var err error
7782
var dependencies Dependencies
7883
var updatedDependencies []VersionUpdateInfo
@@ -111,8 +116,8 @@ func updater(token string, repoPath string, commit bool) error {
111116
}
112117
}
113118

114-
if commit && updatedDependencies != nil {
115-
err := createCommitMessage(updatedDependencies)
119+
if (commit && updatedDependencies != nil) || (githubAction && updatedDependencies != nil) {
120+
err := createCommitMessage(updatedDependencies, repoPath, githubAction)
116121
if err != nil {
117122
return fmt.Errorf("error creating commit message: %s", err)
118123
}
@@ -126,7 +131,7 @@ func updater(token string, repoPath string, commit bool) error {
126131
return nil
127132
}
128133

129-
func createCommitMessage(updatedDependencies []VersionUpdateInfo) error {
134+
func createCommitMessage(updatedDependencies []VersionUpdateInfo, repoPath string, githubAction bool) error {
130135
var repos []string
131136
commitTitle := "chore: updated "
132137
commitDescription := "Updated dependencies for: \n"
@@ -136,12 +141,20 @@ func createCommitMessage(updatedDependencies []VersionUpdateInfo) error {
136141
commitDescription += repo + " => " + tag + " (" + dependency.DiffUrl + ")" + "\n"
137142
repos = append(repos, repo)
138143
}
144+
commitDescription = strings.TrimSuffix(commitDescription, "\n")
139145

140-
commitTitle += strings.Join(repos, ", ")
141-
cmd := exec.Command("git", "commit", "-am", commitTitle, "-m", commitDescription)
142-
143-
if err := cmd.Run(); err != nil {
144-
return fmt.Errorf("error running git commit -m: %s", err)
146+
if githubAction {
147+
commitTitle += strings.Join(repos, ", ")
148+
commitDescription = "\"" + commitDescription + "\""
149+
err := createGitMessageEnv(commitTitle, commitDescription, repoPath)
150+
if err != nil {
151+
return fmt.Errorf("error creating git commit message: %s", err)
152+
}
153+
} else if !githubAction {
154+
cmd := exec.Command("git", "commit", "-am", commitTitle, "-m", commitDescription)
155+
if err := cmd.Run(); err != nil {
156+
return fmt.Errorf("error running git commit -m: %s", err)
157+
}
145158
}
146159
return nil
147160
}
@@ -220,11 +233,11 @@ func getVersionAndCommit(ctx context.Context, client *github.Client, dependencie
220233

221234
if dependencies[dependencyType].Tracking == "tag" {
222235
versionCommit, _, err := client.Repositories.GetCommit(
223-
ctx,
224-
dependencies[dependencyType].Owner,
225-
dependencies[dependencyType].Repo,
226-
"refs/tags/"+*version.TagName,
227-
&github.ListOptions{})
236+
ctx,
237+
dependencies[dependencyType].Owner,
238+
dependencies[dependencyType].Repo,
239+
"refs/tags/"+*version.TagName,
240+
&github.ListOptions{})
228241
if err != nil {
229242
return "", "", VersionUpdateInfo{}, fmt.Errorf("error getting commit for "+dependencyType+": %s", err)
230243
}
@@ -264,7 +277,7 @@ func updateVersionTagAndCommit(
264277
if err != nil {
265278
return fmt.Errorf("error writing to versions "+dependencyType+": %s", err)
266279
}
267-
280+
268281
return nil
269282
}
270283

@@ -318,6 +331,21 @@ func createVersionsEnv(repoPath string, dependencies Dependencies) error {
318331
return nil
319332
}
320333

334+
func createGitMessageEnv(title string, description string, repoPath string) error {
335+
file, err := os.Create(repoPath + "/commit_message.env")
336+
if err != nil {
337+
return fmt.Errorf("error creating git_commit_message.env file: %s", err)
338+
}
339+
defer file.Close()
340+
341+
envString := "export TITLE=" + title + "\nexport DESC=" + description
342+
_, err = file.WriteString(envString)
343+
if err != nil {
344+
return fmt.Errorf("error writing to git_commit_message.env file: %s", err)
345+
}
346+
return nil
347+
}
348+
321349
func generateGithubRepoUrl(dependencies Dependencies, dependencyType string) string {
322350
return "https://github.com/" + dependencies[dependencyType].Owner + "/" + dependencies[dependencyType].Repo
323-
}
351+
}

0 commit comments

Comments
 (0)