-
Notifications
You must be signed in to change notification settings - Fork 0
76 lines (65 loc) · 2.56 KB
/
on_pr.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
name: Create Tag and Increment Version
on:
pull_request:
branches:
- main
types: [closed]
workflow_dispatch: # Allow manual triggering
jobs:
create-tag:
if: ${{ github.event.pull_request.merged }} || github.event_name == 'workflow_dispatch
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Get latest tag and increment version
id: tag_version
uses: actions/github-script@v7
with:
script: |
// Get the latest tag
const tags = await github.rest.repos.listTags({
owner: context.repo.owner,
repo: context.repo.repo,
}).then(res => res.data);
let latestTag = 'v0.0.0'; // Default if no tags exist
if (tags.length > 0) {
latestTag = tags[0].name; // Assuming tags are sorted by date (newest first)
}
// Extract version components
const [, year, month, minor] = latestTag.match(/v(\d{4})\.(\d{2})\.(\d+)/) || [null, 0, 0, 0];
// Get current year and month
const now = new Date();
const currentYear = now.getFullYear();
const currentMonth = now.getMonth() + 1; // Months are 0-indexed
// Determine the new version
let newMinor = parseInt(minor) + 1;
if (currentYear > parseInt(year) || currentMonth > parseInt(month)) {
newMinor = 0;
}
const newVersion = `v${currentYear}.${currentMonth.toString().padStart(2, '0')}.${newMinor}`;
// Output the new version
core.setOutput('new_version', newVersion);
- name: Create Tag
uses: actions/github-script@v7
with:
script: |
// Access the output directly using the step id and output name
const newVersion = '${{ steps.tag_version.outputs.new_version }}';
// Create the new tag
await github.rest.git.createRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: `refs/tags/${newVersion}`,
sha: context.sha,
});
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Trigger Tag Workflow
uses: peter-evans/repository-dispatch@v2
with:
token: ${{ secrets.GITHUB_TOKEN }}
event-type: tag_created # Choose a descriptive event type
client-payload: '{"tag_name": "${{ steps.tag_version.outputs.new_version }}"}'