1+ name : org.update
2+
3+ on :
4+ workflow_dispatch :
5+ inputs :
6+ etc :
7+ description : ' base64 encoded json string'
8+ required : true
9+
10+ jobs :
11+ update :
12+ runs-on : ubuntu-latest
13+
14+ permissions :
15+ actions : read
16+ contents : write
17+
18+ steps :
19+ - name : init / checkout
20+ uses : actions/checkout@85e6279cec87321a52edac9c87bce653a07cf6c2
21+ with :
22+ ref : ' master'
23+ fetch-depth : 0
24+
25+ - name : update / setup node
26+ uses : actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020
27+ with :
28+ node-version : ' 20'
29+ - run : npm i semver
30+
31+ - name : update / compare latest with current version
32+ uses : actions/github-script@62c3794a3eb6788d9a2a72b219504732c0c9a298
33+ with :
34+ script : |
35+ (async()=>{
36+ const { Buffer } = require('node:buffer');
37+ const { inspect } = require('node:util');
38+ const { existsSync, readFileSync, writeFileSync } = require('node:fs');
39+ const { resolve } = require('node:path');
40+ const semver = require('semver')
41+
42+ // defaults
43+ const json = `${{ toJSON(github.event.inputs) }}`;
44+ const job = {inputs:{}, json:{}};
45+
46+ // check if inputs is valid base64 encoded json
47+ try{
48+ if(json.length > 0){
49+ const n = JSON.parse(json);
50+ if(n?.etc){
51+ try{
52+ job.inputs = JSON.parse(Buffer.from(n.etc, 'base64').toString('ascii'));
53+ if(!job.inputs?.version){
54+ core.setFailed(`input does not contain valid semver version: ${inspect(job.inputs, {showHidden:false, depth:null, colors:true})}`);
55+ }else if(!job.inputs?.tag){
56+ core.setFailed(`input does not contain valid git tag: ${inspect(job.inputs, {showHidden:false, depth:null, colors:true})}`);
57+ }
58+ }catch(e){
59+ core.setFailed(`could not parse github.event.inputs.etc: ${n.etc} (${Buffer.from(n.etc, 'base64').toString('ascii')})`);
60+ }
61+ }
62+ }
63+ }catch(e){
64+ core.setFailed(`could not parse github.event.inputs: ${json}`);
65+ }
66+
67+ // check if .json exists
68+ try{
69+ const path = resolve('.json');
70+ if(existsSync(path)){
71+ try{
72+ job.json = JSON.parse(readFileSync(path).toString());
73+ }catch(e){
74+ throw new Error('could not parse .json');
75+ }
76+ }else{
77+ throw new Error('.json does not exist!');
78+ }
79+ }catch(e){
80+ core.setFailed(e);
81+ }
82+
83+ // semver
84+ const latest = semver.valid(semver.coerce(job.inputs.version));
85+ const current = semver.valid(semver.coerce(job.json.semver.version));
86+ const tag = semver.valid(semver.coerce(job.inputs.tag));
87+ const checks = {latestTagExists:true};
88+
89+ try{
90+ const tag = await fetch(`https://hub.docker.com/v2/repositories/${job.json.image}/tags/${latest}`);
91+ if(tag.status === 404){
92+ checks.latestTagExists = false;
93+ }
94+ }catch(e){
95+ core.warning(e);
96+ }
97+
98+ // compare
99+ if((latest && latest !== current) || !checks.latestTagExists){
100+ core.info(`new ${semver.diff(current, latest)} release found (${latest}), updating ...`)
101+ job.json.semver.version = latest;
102+
103+ // update .json
104+ try{
105+ writeFileSync(resolve('.json'), JSON.stringify(job.json, null, 2));
106+
107+ // export variables
108+ core.exportVariable('WORKFLOW_UPDATE', true);
109+ core.exportVariable('LATEST_TAG', semver.inc(tag, semver.diff(current, latest)));
110+ core.exportVariable('LATEST_VERSION', latest);
111+ }catch(e){
112+ core.setFailed(e);
113+ }
114+ }else{
115+ core.info('no update required')
116+ }
117+
118+ core.info(inspect(job, {showHidden:false, depth:null, colors:true}));
119+ })();
120+
121+
122+
123+ - name : update / checkout
124+ id : checkout
125+ if : env.WORKFLOW_UPDATE == 'true'
126+ run : |
127+ git config user.name "github-actions[bot]"
128+ git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
129+ git add .json
130+ git commit -m "chore: auto upgrade to v${{ env.LATEST_VERSION }}"
131+ git push origin HEAD:master
132+
133+ - name : update / tag
134+ if : env.WORKFLOW_UPDATE == 'true' && steps.checkout.outcome == 'success'
135+ run : |
136+ SHA256=$(git rev-list --branches --max-count=1)
137+ git tag -a v${{ env.LATEST_TAG }} -m "v${{ env.LATEST_TAG }}" ${SHA256}
138+ git push --follow-tags
139+
140+ - name : update / build container image
141+ if : env.WORKFLOW_UPDATE == 'true' && steps.checkout.outcome == 'success'
142+ uses : the-actions-org/workflow-dispatch@3133c5d135c7dbe4be4f9793872b6ef331b53bc7
143+ with :
144+ workflow : docker.yml
145+ wait-for-completion : false
146+ token : " ${{ secrets.REPOSITORY_TOKEN }}"
147+ inputs : ' { "release":"true", "readme":"true" }'
148+ ref : " v${{ env.LATEST_TAG }}"
0 commit comments