-
Notifications
You must be signed in to change notification settings - Fork 583
96 lines (93 loc) · 3.48 KB
/
git-actions-go-generate.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
name: Go Generate
on:
workflow_dispatch:
inputs:
source_url:
type: string
description: "URL of the source for this workflow run"
source_author:
type: string
description: "Username of the source for this workflow run"
env:
INPUT_SOURCE_URL: ${{ github.event.inputs.source_url }}
INPUT_SOURCE_AUTHOR: ${{ github.event.inputs.source_author }}
permissions:
contents: write
pull-requests: write
id-token: write
jobs:
go-generate:
runs-on: ubuntu-latest
steps:
- name: Check out repository code
uses: actions/checkout@v2
- name: Run go-generate to fetch data.json
run: make go-generate
- name: Check for repository changes
run: |
if git diff --name-only --exit-code; then
echo "No changes found in repository after 'go generate'"
echo "changes_exist=false" >> $GITHUB_ENV
else
echo "Changes found in repository after 'go generate':"
git diff --name-only
echo "changes_exist=true" >> $GITHUB_ENV
fi
- name: Create branch, commit and push
if: ${{ env.changes_exist == 'true' }}
id: branch
run: |
BRANCH="githubaction-go-generate-$(date +%Y-%m-%d-%H-%M-%S)"
echo "::set-output name=branch::$BRANCH"
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions[bot]"
git checkout -b "$BRANCH"
git commit -a -m 'go generate'
git push origin "$BRANCH"
- name: Retrieve token from vault
uses: rancher-eio/read-vault-secrets@main
with:
secrets: |
secret/data/github/repo/${{ github.repository }}/github-token/credentials token | PAT_TOKEN ;
- name: Create Pull Request
if: ${{ env.changes_exist == 'true' }}
id: cpr
uses: actions/[email protected]
env:
SOURCE_BRANCH: ${{ steps.branch.outputs.branch }}
with:
github-token: ${{ env.PAT_TOKEN }}
script: |
let body = 'Auto-generated by GitHub Actions\n\n'
if ( `${ process.env.INPUT_SOURCE_URL }` ) {
body += `\nSource URL: ${ process.env.INPUT_SOURCE_URL }`
}
if ( `${ process.env.INPUT_SOURCE_AUTHOR }` ) {
body += `\nSource AUTHOR: @${ process.env.INPUT_SOURCE_AUTHOR}`
}
const { data: pr } = await github.rest.pulls.create({
title: "[${{ github.ref_name }}] go generate",
body: body,
owner: context.repo.owner,
repo: context.repo.repo,
base: "${{ github.ref_name }}",
head: `${ process.env.SOURCE_BRANCH }`
});
await github.rest.issues.addLabels({
...context.repo,
issue_number: pr.number,
labels: ["status/auto-created"],
});
if ( `${ process.env.INPUT_SOURCE_AUTHOR }` ) {
await github.rest.issues.addAssignees({
...context.repo,
issue_number: pr.number,
assignees: [`${ process.env.INPUT_SOURCE_AUTHOR}`],
});
}
console.log('Created new pull request');
return pr.html_url;
- name: Check outputs
if: ${{ env.changes_exist == 'true' }}
run: |
echo "Pull Request URL - ${{ steps.cpr.outputs.result }}"