Skip to content

Commit fe0f3af

Browse files
authored
Merge pull request k3s-io#487 from axivo/feature/workflow-improvements
feat: workflow improvements
2 parents f7fbd6f + a407273 commit fe0f3af

1 file changed

Lines changed: 46 additions & 57 deletions

File tree

.github/workflows/documentation.yml

Lines changed: 46 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ permissions:
1919
contents: write
2020

2121
jobs:
22-
build:
23-
name: Build Documentation
22+
update:
23+
name: Update Documentation
2424
runs-on: ubuntu-latest
2525
steps:
26-
- name: Checkout code
26+
- name: Checkout repository
2727
uses: actions/checkout@v4
2828

29-
- name: Install package
29+
- name: Install helm-docs package
3030
env:
3131
HELM_DOCS_URL: https://github.com/norwoodj/helm-docs/releases/download
3232
HELM_DOCS_VERSION: 1.14.2
@@ -35,36 +35,27 @@ jobs:
3535
script: |
3636
const os = require('os');
3737
const tmpDir = os.tmpdir();
38-
const helmDocsPath = [tmpDir,
38+
const packagePath = [tmpDir,
3939
'helm-docs_' + process.env.HELM_DOCS_VERSION + '_Linux_x86_64.deb'
4040
].join('/');
41-
const helmDocsUrl = [process.env.HELM_DOCS_URL, 'v' + process.env.HELM_DOCS_VERSION,
41+
const packageUrl = [process.env.HELM_DOCS_URL, 'v' + process.env.HELM_DOCS_VERSION,
4242
'helm-docs_' + process.env.HELM_DOCS_VERSION + '_Linux_x86_64.deb'
4343
].join('/');
4444
const runSudo = async (args) => (await exec.exec('sudo', args));
4545
try {
46-
await runSudo(['wget', '-qP', tmpDir, helmDocsUrl]);
47-
await runSudo(['apt-get', '-y', 'install', helmDocsPath]);
46+
await runSudo(['wget', '-qP', tmpDir, packageUrl]);
47+
await runSudo(['apt-get', '-y', 'install', packagePath]);
4848
} catch (error) {
4949
core.setFailed(error.message);
5050
}
5151
5252
- name: Update documentation
53-
id: update
5453
uses: actions/github-script@v7
5554
with:
5655
github-token: ${{ secrets.GITHUB_TOKEN }}
5756
script: |
57+
const fs = require('fs/promises');
5858
const runGit = async (args) => (await exec.getExecOutput('git', args)).stdout.trim();
59-
const excludeList = async () =>
60-
(await runGit(['ls-files', '-d', '--deduplicate', '--exclude-standard'])).trim().split('\n');
61-
const processFiles = async (flag, label, exclusions) => {
62-
const files = (await runGit(['ls-files', '-' + flag, '--deduplicate', '--exclude-standard']))
63-
.split('\n')
64-
.filter(file => file && (flag === 'om' && !exclusions.includes(file)));
65-
core.setOutput(label, files.join(','));
66-
return files.length;
67-
};
6859
try {
6960
await Promise.all([
7061
runGit(['config', 'user.name', 'github-actions[bot]']),
@@ -73,47 +64,45 @@ jobs:
7364
await runGit(['fetch', 'origin', process.env.GITHUB_HEAD_REF]);
7465
await runGit(['switch', process.env.GITHUB_HEAD_REF]);
7566
await exec.exec('helm-docs', ['-f', './defaults/main.yaml']);
76-
const exclusions = await excludeList();
77-
const [additions, deletions] = await Promise.all([
78-
processFiles('om', 'additions', exclusions),
79-
processFiles('d', 'deletions', exclusions)
80-
]);
81-
} catch (error) {
82-
core.setFailed(error.message);
83-
}
84-
85-
- name: Create commit
86-
if: steps.update.outputs.additions || steps.update.outputs.deletions
87-
uses: actions/github-script@v7
88-
with:
89-
github-token: ${{ secrets.GITHUB_TOKEN }}
90-
script: |
91-
const fs = require('fs/promises');
92-
const parseFiles = async (files, type) =>
93-
Promise.all(files.split(',').filter(Boolean).map(async file => (type === 'add')
94-
? { path: file, contents: Buffer.from(await fs.readFile(file, 'utf-8')).toString('base64') }
95-
: { path: file }
96-
));
97-
const input = {
98-
branch: {
99-
repositoryNameWithOwner: context.payload.repository.full_name,
100-
branchName: context.payload.pull_request.head.ref
101-
},
102-
expectedHeadOid: context.payload.pull_request.head.sha,
103-
fileChanges: {
104-
additions: await parseFiles('${{ steps.update.outputs.additions }}', 'add'),
105-
deletions: await parseFiles('${{ steps.update.outputs.deletions }}', 'delete')
106-
},
107-
message: { headline: 'docs(github-action): update documentation' }
108-
};
109-
const mutation = `
110-
mutation CreateCommitOnBranch($input: CreateCommitOnBranchInput!) {
111-
createCommitOnBranch(input: $input) { commit { oid } }
67+
const files = (await runGit(['diff', '--name-only'])).split('\n').filter(Boolean);
68+
if (files.length === 0) {
69+
core.info('No file changes detected');
70+
return;
11271
}
113-
`;
114-
try {
72+
const additions = await Promise.all((await runGit(['diff', '--name-only', '--diff-filter=ACMR']))
73+
.split('\n')
74+
.filter(Boolean)
75+
.map(async file => {
76+
const contents = await fs.readFile(file, 'utf-8');
77+
return {
78+
path: file,
79+
contents: Buffer.from(contents).toString('base64')
80+
};
81+
})
82+
);
83+
const deletions = (await runGit(['diff', '--name-only', '--diff-filter=D']))
84+
.split('\n')
85+
.filter(Boolean)
86+
.map(file => ({ path: file }));
87+
const input = {
88+
branch: {
89+
repositoryNameWithOwner: context.payload.repository.full_name,
90+
branchName: context.payload.pull_request.head.ref
91+
},
92+
expectedHeadOid: context.payload.pull_request.head.sha,
93+
fileChanges: {
94+
additions: additions,
95+
deletions: deletions
96+
},
97+
message: { headline: 'docs(github-action): update documentation' }
98+
};
99+
const mutation = `
100+
mutation CreateCommitOnBranch($input: CreateCommitOnBranchInput!) {
101+
createCommitOnBranch(input: $input) { commit { oid } }
102+
}
103+
`;
115104
const { createCommitOnBranch } = await github.graphql(mutation, { input });
116-
console.log('Commit created with OID:', createCommitOnBranch.commit.oid);
105+
core.info('Signed commit created with OID:', createCommitOnBranch.commit.oid);
117106
} catch (error) {
118107
core.setFailed(error.message);
119108
}

0 commit comments

Comments
 (0)