Skip to content

Merge pull request #188 from shutter-network/fix/174-fix-tag-checkout #84

Merge pull request #188 from shutter-network/fix/174-fix-tag-checkout

Merge pull request #188 from shutter-network/fix/174-fix-tag-checkout #84

Workflow file for this run

name: Build and Deploy
on:
push:
branches:
- main
- staging
pull_request:
branches:
- main
- staging
jobs:
run_cypress_tests:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: "20.12.2"
- name: Install dependencies
run: |
cd frontend
npm install
- name: Run Cypress component tests
run: |
cd frontend
npx cypress run --component
# Branch Name Validation for Pull Requests
validate_branch_name:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
steps:
- name: Validate branch name
run: |
BRANCH_NAME="${{ github.head_ref }}"
echo "Validating branch name: $BRANCH_NAME"
# Allow 'staging' and 'main' branches
if [[ "$BRANCH_NAME" == "staging" || "$BRANCH_NAME" == "main" ]]; then
exit 0
fi
# Check if the branch name starts with the expected prefixes
if [[ "$BRANCH_NAME" == release/* ]] || [[ "$BRANCH_NAME" == feat/* ]] || [[ "$BRANCH_NAME" == fix/* ]] || [[ "$BRANCH_NAME" == chore/* ]] || [[ "$BRANCH_NAME" == docs/* ]] || [[ "$BRANCH_NAME" == test/* ]]; then
echo "Branch name is valid: $BRANCH_NAME"
else
echo "Error: Branch name must start with 'release/', 'feat/', 'fix/', 'chore/', 'docs/', or 'test/'."
exit 1 # Exit if the branch name is not valid
fi
# Staging Deployment (with Tagging)
deploy_instance_staging:
if: github.ref == 'refs/heads/staging' && github.event_name == 'push'
runs-on: ubuntu-latest
needs: run_cypress_tests
steps:
- name: Checkout code
uses: actions/checkout@v3
# Fetch tags to get the latest version tag
- name: Fetch tags
run: git fetch --tags
# Install semver for version bumping
- name: Install semver
run: npm install -g semver
# Determine version bump after a merge to staging
- name: Determine version bump
id: version_bump
run: |
# Get the latest tag (assuming semantic versioning format v1.2.3)
LATEST_TAG=$(git describe --tags $(git rev-list --tags --max-count=1))
# Default version bump is patch
VERSION_BUMP="patch"
# Find the source branch by checking the latest merge commit
SOURCE_BRANCH=$(git log -1 --pretty=%B | grep -oP '(?<=from\s)[^\s]+')
# Remove the 'shutter-network/' prefix if it exists
CLEANED_SOURCE_BRANCH=$(echo "$SOURCE_BRANCH" | sed 's/^shutter-network\///')
echo "Source branch: $CLEANED_SOURCE_BRANCH"
# Allow 'staging' and 'main' branches without version bump
if [[ "$CLEANED_SOURCE_BRANCH" == "staging" || "$CLEANED_SOURCE_BRANCH" == "main" ]]; then
echo "No version bump required for 'staging' or 'main' branch."
exit 0
fi
# Determine version bump based on the source branch
if [[ "$CLEANED_SOURCE_BRANCH" == release/* ]]; then
VERSION_BUMP="major"
elif [[ "$CLEANED_SOURCE_BRANCH" == feat/* ]]; then
VERSION_BUMP="minor"
elif [[ "$CLEANED_SOURCE_BRANCH" == fix/* ]]; then
VERSION_BUMP="patch"
elif [[ "$CLEANED_SOURCE_BRANCH" == chore/* ]]; then
VERSION_BUMP="patch"
elif [[ "$CLEANED_SOURCE_BRANCH" == docs/* ]]; then
VERSION_BUMP="patch"
elif [[ "$CLEANED_SOURCE_BRANCH" == test/* ]]; then
VERSION_BUMP="patch"
else
echo "Error: Source branch name must start with 'release/', 'feat/', 'fix/', 'chore/', 'docs/' or 'test/'."
exit 1
fi
# Calculate the next version using semver
NEXT_VERSION=$(npx semver "$LATEST_TAG" -i $VERSION_BUMP)
echo "NEXT_VERSION=$NEXT_VERSION" >> $GITHUB_ENV
echo "Next version: $NEXT_VERSION"
# Create a new tag for the staging deployment
- name: Create and push new version tag
if: env.NEXT_VERSION != ''
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git tag "v${{ env.NEXT_VERSION }}"
git push origin "v${{ env.NEXT_VERSION }}"
# Setup SSH for deployment
- name: Setup SSH
run: |
mkdir -p ~/.ssh
echo "${{ secrets.SERVER_KEY }}" > ~/.ssh/id_ed25519 && chmod 600 ~/.ssh/id_ed25519
# Deploy to Staging
- name: Deploy to Staging
run: >
ssh -o StrictHostKeyChecking=no ${{secrets.STAGING_SERVER_USER}}@${{secrets.STAGING_SERVER_HOST}}
"set -x && cd /root/shutter-explorer && git pull origin staging && git submodule update --init --recursive && cd docker && docker compose up -d --build"
# Production Deployment (using the tag from staging)
deploy_instance_prod:
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
runs-on: ubuntu-latest
needs: run_cypress_tests
steps:
- name: Checkout code
uses: actions/checkout@v3
# Fetch tags to get the latest version tag from staging
- name: Fetch tags
run: git fetch --tags
# Get the latest tag from staging
- name: Get latest version tag
id: get_latest_tag
run: |
LATEST_TAG=$(git describe --tags $(git rev-list --tags --max-count=1))
echo "LATEST_TAG=$LATEST_TAG" >> $GITHUB_ENV
# Setup SSH for deployment
- name: Setup SSH
run: |
mkdir -p ~/.ssh
echo "${{ secrets.SERVER_KEY }}" > ~/.ssh/id_ed25519 && chmod 600 ~/.ssh/id_ed25519
# Deploy to Production with the latest tag from staging
- name: Deploy to Production
run: >
ssh -o StrictHostKeyChecking=no ${{secrets.PROD_SERVER_USER}}@${{secrets.PROD_SERVER_HOST}}
"set -x && cd /root/shutter-explorer && git pull origin main && git submodule update --init --recursive && git checkout ${{ env.LATEST_TAG }} && cd docker && docker compose up -d --build"