Skip to content

Merge pull request #70 from werlleyg/develop #7

Merge pull request #70 from werlleyg/develop

Merge pull request #70 from werlleyg/develop #7

Workflow file for this run

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 }}