Merge pull request #101 from smswithoutborders/security/update #30
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Staging Deployment Pipeline | |
| on: | |
| push: | |
| branches: | |
| - staging | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| environment: | |
| name: staging | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: "20" | |
| - name: Enable Corepack and Use Latest Yarn | |
| run: | | |
| corepack enable | |
| corepack prepare yarn@stable --activate | |
| - name: Install dependencies | |
| run: | | |
| yarn install | |
| - name: Build project | |
| run: | | |
| yarn build | |
| - name: Prepare build for deployment | |
| run: | | |
| mkdir -p build_output | |
| mkdir -p build_output/artifacts | |
| mv build/* build_output/artifacts/ | |
| mv nginx/ build_output/nginx/ | |
| mv scripts/ build_output/scripts/ | |
| mv Dockerfile.nginx build_output/Dockerfile | |
| mv .gitignore build_output/.gitignore | |
| mv deploy.sh build_output/deploy.sh | |
| mv docker-compose.yml build_output/docker-compose.yml | |
| - name: Set up Git for commit | |
| uses: qoomon/actions--setup-git@v1 | |
| with: | |
| user: bot | |
| - name: Commit and Push to Build Branch | |
| run: | | |
| git checkout --orphan build-staging | |
| git rm -rf . | |
| mv build_output/* . | |
| mv build_output/.gitignore .gitignore | |
| timestamp=$(date +"%Y-%m-%d %H:%M:%S (%Z)") | |
| git add . | |
| git commit -m "π Build deployed at ${timestamp}" | |
| git push origin build-staging --force | |
| deploy: | |
| name: Execute Deployment Script on Server | |
| needs: build | |
| runs-on: ubuntu-latest | |
| environment: | |
| name: staging | |
| url: https://staging.smswithoutborders.com:18600 | |
| steps: | |
| - name: Execute Remote SSH Commands | |
| uses: appleboy/ssh-action@master | |
| with: | |
| host: ${{ secrets.HOST }} | |
| username: ${{ secrets.USERNAME }} | |
| key: ${{ secrets.KEY }} | |
| script: | | |
| set -e | |
| cd ${{ secrets.PROJECT_PATH }} | |
| echo "============================" | |
| echo "π Deleting local build-staging branch if exists ..." | |
| echo "============================" | |
| current_branch=$(git rev-parse --abbrev-ref HEAD) | |
| if [ "$current_branch" = "build-staging" ]; then | |
| if git branch --list main; then | |
| git checkout main | |
| elif git branch --list master; then | |
| git checkout master | |
| else | |
| echo "β No main or master branch to switch to before deleting build-staging!" | |
| exit 1 | |
| fi | |
| fi | |
| if git branch --list build-staging; then | |
| git branch -D build-staging | |
| fi | |
| echo "===============================" | |
| echo "β Local build-staging branch deleted (if existed)" | |
| echo "===============================" | |
| echo "============================" | |
| echo "π Fetching all branches ..." | |
| echo "============================" | |
| if ! git fetch --all; then | |
| echo "β Error fetching branches!" | |
| exit 1 | |
| fi | |
| echo "===============================" | |
| echo "β Branch fetch complete" | |
| echo "===============================" | |
| echo "============================" | |
| echo "π Switching to build-staging branch ..." | |
| echo "============================" | |
| if ! git checkout build-staging; then | |
| echo "β Error switching to build-staging branch!" | |
| exit 1 | |
| fi | |
| echo "===============================" | |
| echo "β Switched to build-staging branch" | |
| echo "===============================" | |
| echo "=========================" | |
| echo "π Building project ..." | |
| echo "=========================" | |
| if ! ${{ secrets.BUILD_CMD }}; then | |
| echo "β Error building project!" | |
| exit 1 | |
| fi | |
| echo "===========================" | |
| echo "β Project build complete" | |
| echo "===========================" | |
| echo "=============================" | |
| echo "Cleaning up staging builds ..." | |
| echo "=============================" | |
| if ! ${{ secrets.CLEANUP_CMD }}; then | |
| echo "β Error cleaning up builds!" | |
| exit 1 | |
| fi | |
| echo "=============================" | |
| echo "β Cleanup complete" | |
| echo "=============================" |