-
Notifications
You must be signed in to change notification settings - Fork 2
121 lines (114 loc) · 4.53 KB
/
fetch-releases.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
name: Fetch releases
on:
# schedule:
# - cron: '0 * * * *'
workflow_dispatch:
env:
upstream_repo_owner: 'navermaps'
upstream_repo_name: 'NMapsMap'
function_semver: |
function(tag) {
try {
const regExp = /\d*\.\d*\.\d*.*/;
const prefix = "v";
return prefix + regExp.exec(tag)[0];
} catch {
return tag;
}
}
jobs:
check_pat:
name: Check secrets.PERSONAL_ACCESS_TOKEN
runs-on: macos-latest
steps:
- name: Check if secrets.PERSONAL_ACCESS_TOKEN exists
uses: actions/github-script@v3
with:
script: |
const patIsNotNull = ${{ secrets.PERSONAL_ACCESS_TOKEN != null }};
if (patIsNotNull) {
console.log("secrets.PERSONAL_ACCESS_TOKEN exists.")
} else {
// TODO: 오류 메시지에 해결 방법 제공하기
core.setFailed(
`secret.PERSONAL_ACCESS_TOKEN doesn't exist.
https://github.com/${context.repo.owner}/${context.repo.repo}/settings/secrets/actions/new`
);
}
fetch_releases:
name: Fetch releases
needs: check_pat
runs-on: macos-latest
outputs:
removedTags: ${{ steps.diff_tags.outputs.removedTags }}
addedTags: ${{ steps.diff_tags.outputs.addedTags }}
steps:
- name: List tags on ${{ github.repository }}
id: list_tags_working
uses: actions/github-script@v3
with:
github-token: ${{ github.token }}
script: |
const workingRepoTags = (await github.repos.listTags({
owner: context.repo.owner,
repo: context.repo.repo
})).data.reverse().map(tag => tag.name);
core.setOutput("workingRepoTags", workingRepoTags);
console.log("workingRepoTags: ", workingRepoTags);
- name: List tags on ${{ env.upstream_repo_owner }}/${{ env.upstream_repo_name }}
id: list_tags_upstream
uses: actions/github-script@v3
with:
github-token: ${{ github.token }}
script: |
const upstreamRepoTags = (await github.repos.listTags({
owner: '${{ env.upstream_repo_owner }}',
repo: '${{ env.upstream_repo_name }}'
})).data.reverse().map(tag => tag.name);
core.setOutput("upstreamRepoTags", upstreamRepoTags);
console.log("upstreamRepoTags: ", upstreamRepoTags);
- name: Diff tags between ${{ github.repository }} and ${{ env.upstream_repo_owner }}/${{ env.upstream_repo_name }}
id: diff_tags
uses: actions/github-script@v3
with:
script: |
const semver = ${{ env.function_semver }};
const workingRepoTags = ${{ steps.list_tags_working.outputs.workingRepoTags }};
const upstreamRepoTags = ${{ steps.list_tags_upstream.outputs.upstreamRepoTags }};
const removedTags = workingRepoTags.filter(tag => !upstreamRepoTags.map(semver).includes(tag));
const addedTags = upstreamRepoTags.filter(tag => !workingRepoTags.includes(semver(tag))).map(tag => ({ src: tag, dest: semver(tag) }));
core.setOutput("removedTags", removedTags);
core.setOutput("addedTags", addedTags);
console.log("removedTags: ", removedTags);
console.log("addedTags: ", addedTags);
trigger_workflow:
name: Trigger ${{ matrix.workflow.id }}
strategy:
max-parallel: 1
matrix:
workflow:
- id: 'delete-releases.yml'
tags: ${{ needs.fetch_releases.outputs.removedTags }}
- id: 'create-releases.yml'
tags: ${{ needs.fetch_releases.outputs.addedTags }}
needs: fetch_releases
# if: ${{ matrix.workflow.tags != '[]' }}
runs-on: macos-latest
steps:
- name: Create a workflow dispatch event with ${{ matrix.workflow.tags }}
if: ${{ matrix.workflow.tags != '[]' }}
uses: actions/github-script@v3
with:
github-token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
script: |
const response = await github.request("POST /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches", {
owner: context.repo.owner,
repo: context.repo.repo,
ref: context.ref,
workflow_id: '${{ matrix.workflow.id }}',
inputs: {
tags: '${{ matrix.workflow.tags }}'
}
});
const result = { status: response.status, url: response.url };
console.log(result);