-
Notifications
You must be signed in to change notification settings - Fork 2
177 lines (146 loc) · 6.2 KB
/
deploy.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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 fetch --tags && git checkout ${{ env.LATEST_TAG }} && cd docker && docker compose up -d --build"