-
Notifications
You must be signed in to change notification settings - Fork 0
109 lines (93 loc) · 3.64 KB
/
main-version.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
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
name: Version Check and Create Tag
env:
VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}
on:
push:
branches:
- main
jobs:
check_version:
runs-on: ubuntu-latest
outputs:
create_tag: ${{ steps.compare_versions.outputs.create_tag }}
version: ${{ steps.get_version.outputs.version }}
latest_tag: ${{ steps.get_latest_tag.outputs.latest_tag }}
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: "18"
- name: Get project version
id: get_version
run: |
VERSION=$(node -p "require('./package.json').version")
echo "VERSION=$VERSION" >> $GITHUB_ENV
echo "::set-output name=version::$VERSION"
- name: Get latest tag from main branch
id: get_latest_tag
run: |
# Fetch tags from the main branch
git fetch origin main --tags
# Get the latest tag from the main branch
TAG=$(git tag --list --sort=-v:refname | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+' | head -n 1 || echo "0.0.0")
# Remove the 'v' prefix from the tag
CLEAN_TAG=$(echo $TAG | sed 's/^v//')
echo "LATEST_TAG=$CLEAN_TAG" >> $GITHUB_ENV
echo "::set-output name=latest_tag::$CLEAN_TAG"
- name: Compare versions
id: compare_versions
run: |
echo "Comparing versions..."
# Fetch values from environment
VERSION=${{ env.VERSION }}
LATEST_TAG=${{ env.LATEST_TAG }}
# Ensure both version and tag are properly formatted
VERSION_PADDED=$(printf "%s\n" "$VERSION" | awk -F. '{printf("%04d%04d%04d", $1, $2, $3)}')
LATEST_TAG_PADDED=$(printf "%s\n" "$LATEST_TAG" | awk -F. '{printf("%04d%04d%04d", $1, $2, $3)}')
echo "Padded Project version: $VERSION_PADDED"
echo "Padded Latest tag version: $LATEST_TAG_PADDED"
# Compare the padded versions
if [ "$VERSION_PADDED" -gt "$LATEST_TAG_PADDED" ]; then
echo "Project version $VERSION is greater than latest tag $LATEST_TAG"
echo "::set-output name=create_tag::true"
else
echo "Project version $VERSION is not greater than latest tag $LATEST_TAG"
echo "::set-output name=create_tag::false"
exit 1
fi
create_tag:
runs-on: ubuntu-latest
needs: check_version
if: ${{ needs.check_version.outputs.create_tag == 'true' }}
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Git
run: |
git config --global user.name "github-actions"
git config --global user.email "[email protected]"
- name: Create and push tag
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
TAG="v${{ needs.check_version.outputs.version }}"
# Create a new tag
git tag $TAG
# Push the tag to the remote repository
git push origin $TAG
deploy-production:
runs-on: ubuntu-latest
needs: create_tag
steps:
- uses: actions/checkout@v3
- name: Install Vercel CLI
run: npm install --global vercel@latest
- name: Pull Vercel Environment Information
run: vercel pull --yes --environment=production --token=${{ secrets.VERCEL_TOKEN }}
- name: Build Project Artifacts
run: vercel build --prod --token=${{ secrets.VERCEL_TOKEN }}
- name: Deploy Project Artifacts to Vercel
run: vercel deploy --prebuilt --prod --token=${{ secrets.VERCEL_TOKEN }}