-
Notifications
You must be signed in to change notification settings - Fork 0
84 lines (80 loc) · 3.12 KB
/
cd-tag-release.yaml
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
on:
workflow_call:
inputs:
service:
required: true
type: string
env:
required: true
type: string
team:
required: true
type: string
jobs:
deploy-each:
runs-on: ubuntu-latest
steps:
- name: 🔥 Prepare 🔥
run: |
echo "TODAY=$(date --date='+9 hours' +%y.%m.%d)" >> $GITHUB_ENV
- name: ✨ Deploy tag and release ✨
uses: actions/github-script@v7
with:
script: |
const checkBranch = () => {
switch ("${{ inputs.team }}") {
case "backend":
return ("${{ inputs.env }}" == "prod" && context.ref == "refs/heads/main") || ("${{ inputs.env }}" == "dev" && context.ref == "refs/heads/develop")
case "frontend":
return context.ref == "refs/heads/main"
case "devops":
return context.ref == "refs/heads/master"
case "datateam":
return ("${{ inputs.env }}" == "prod" && context.ref == "refs/heads/master") || ("${{ inputs.env }}" == "dev" && context.ref == "refs/heads/develop")
}
return false
}
if (checkBranch()) {
const tagPrefix = "${{ inputs.service }}/${{ inputs.env }}/"
const query = `query($owner:String!, $name:String!) {
repository(owner: $owner, name: $name) {
refs(refPrefix: "refs/tags/${tagPrefix}", last: 1, orderBy: {field: ALPHABETICAL, direction: ASC}) {
nodes {
name
}
}
}
}`
const variables = {
owner: context.repo.owner,
name: context.repo.repo,
}
const lastTagVersion = (await github.graphql(query, variables)).repository.refs.nodes[0]?.name ?? "0.0.0-0"
const newIdx = lastTagVersion.split("-")[0] === "${{ env.TODAY }}" ? parseInt(lastTagVersion.split("-")[1]) + 1 : 1
const newTagVersion = `${{ env.TODAY }}-${newIdx}`
const tagName = tagPrefix + newTagVersion
await github.rest.git.createRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: `refs/tags/${tagName}`,
sha: context.sha
})
let note = "🌱 First deployment with github action 🌱"
try {
note = (await github.rest.repos.generateReleaseNotes({
owner: context.repo.owner,
repo: context.repo.repo,
tag_name: tagName,
previous_tag_name: tagPrefix + lastTagVersion
})).data.body
} catch {}
await github.rest.repos.createRelease({
owner: context.repo.owner,
repo: context.repo.repo,
tag_name: tagName,
name: tagName,
body: note
})
} else {
throw 'Ref match exception'
}