|
| 1 | +# Workflow name |
| 2 | +name: Auto-testing-builds & Deployments |
| 3 | + |
| 4 | +# This workflow is triggered on every push to the repository. |
| 5 | +on: |
| 6 | + push: |
| 7 | + |
| 8 | +# Defines the jobs that will be executed as part of this workflow. |
| 9 | +jobs: |
| 10 | + build: |
| 11 | + # Specifies the runner environment for the job. In this case, it\'s Ubuntu 22.04. |
| 12 | + runs-on: ubuntu-22.04 |
| 13 | + |
| 14 | + # Defines the sequence of steps that make up the \'build\' job. |
| 15 | + steps: |
| 16 | + # This action exposes GitHub environment variables, making them available in subsequent steps. |
| 17 | + - name: GitHub Environment Variables Action |
| 18 | + uses: FranzDiebold/[email protected] |
| 19 | + |
| 20 | + # Sets a project name based on the GitHub repository name. |
| 21 | + # It extracts the repository name, converts it to lowercase, and saves it as an environment variable PROJECT_NAME. |
| 22 | + - name: Set Project Name |
| 23 | + run: | |
| 24 | + repo_full_name="${{ github.repository }}" |
| 25 | + repo_short_name="${repo_full_name##*/}" |
| 26 | + echo "PROJECT_NAME=${repo_short_name,,}" >> $GITHUB_ENV |
| 27 | +
|
| 28 | + # Checks out the repository\'s code so the workflow can access it. |
| 29 | + - name: Checkout code |
| 30 | + uses: actions/checkout@v2 |
| 31 | + |
| 32 | + # Sets up PHP version 8.0 and installs Composer. |
| 33 | + - name: Setup PHP 8.0 |
| 34 | + uses: shivammathur/setup-php@v2 |
| 35 | + with: |
| 36 | + php-version: 8.0 |
| 37 | + tools: composer |
| 38 | + |
| 39 | + # Installs Node.js version 21. |
| 40 | + - name: Install Node.js |
| 41 | + uses: actions/setup-node@v2 |
| 42 | + with: |
| 43 | + node-version: 21 |
| 44 | + |
| 45 | + # Installs PHP dependencies for a production environment using Composer. |
| 46 | + # --no-dev: Skips development dependencies. |
| 47 | + # --ignore-platform-reqs: Ignores PHP version and extension requirements. |
| 48 | + # --prefer-dist: Downloads and unpacks archives instead of cloning from version control. |
| 49 | + # --no-scripts: Prevents execution of scripts defined in composer.json. |
| 50 | + # --optimize-autoloader: Converts PSR-0/4 rules into classmap rules for faster class loading. |
| 51 | + - name: Install dependencies |
| 52 | + run: composer install --no-dev --ignore-platform-reqs --prefer-dist --no-scripts --optimize-autoloader |
| 53 | + |
| 54 | + # Installs Node.js dependencies using npm. |
| 55 | + - name: Install nodejs dependencies |
| 56 | + run: npm install |
| 57 | + |
| 58 | + # Compiles and bundles assets for production. |
| 59 | + - name: Generate production build |
| 60 | + run: npm run production |
| 61 | + |
| 62 | + # Generates translation files for the project. |
| 63 | + - name: Generate translation files |
| 64 | + run: npm run translate |
| 65 | + |
| 66 | + # Removes unnecessary files and directories to create a clean build for deployment. |
| 67 | + # This includes development-related files, version control directories, and source assets. |
| 68 | + - name: Clean unneeded files |
| 69 | + run: | |
| 70 | + [ -f .gitattributes ] && rm .gitattributes |
| 71 | + [ -d .github ] && rm -r .github |
| 72 | + [ -d .git ] && rm -r .git |
| 73 | + [ -f .gitignore ] && rm .gitignore |
| 74 | + [ -f phpcs.xml ] && rm phpcs.xml |
| 75 | + [ -f wpgulp.config.js ] && rm wpgulp.config.js |
| 76 | + [ -f gulpfile.babel.js ] && rm gulpfile.babel.js |
| 77 | + [ -f composer.json ] && rm composer.json |
| 78 | + [ -f composer.lock ] && rm composer.lock |
| 79 | + [ -f package.json ] && rm package.json |
| 80 | + [ -f package-lock.json ] && rm package-lock.json |
| 81 | + [ -f readme.md ] && rm readme.md |
| 82 | + [ -f playwright.config.js ] && rm playwright.config.js |
| 83 | + [ -d assets/sass ] && rm -r assets/sass |
| 84 | + [ -d assets/js/src ] && rm -r assets/js/src |
| 85 | + [ -d node_modules ] && rm -r node_modules |
| 86 | + [ -d e2etests ] && rm -r e2etests |
| 87 | +
|
| 88 | + # Creates a new directory named after the project to hold the build files. |
| 89 | + - name: Create directory for the build |
| 90 | + run: mkdir ${{ env.PROJECT_NAME }} |
| 91 | + |
| 92 | + # Moves all the cleaned project files into the newly created build directory. |
| 93 | + - name: Move files to the build directory |
| 94 | + run: rsync -av --progress --exclude=${{ env.PROJECT_NAME }} . ${{ env.PROJECT_NAME }}/ |
| 95 | + |
| 96 | + # Creates a zip archive of the build directory. |
| 97 | + - name: Create Artifact |
| 98 | + run: zip -qq -r "${{ env.PROJECT_NAME }}.zip" ${{ env.PROJECT_NAME }}/ |
| 99 | + |
| 100 | + # The following steps are for uploading the build to hub.wpforms.com. |
| 101 | + # Sets up an SSH key to securely connect to the deployment server. |
| 102 | + # The private key is stored as a secret. |
| 103 | + - name: Setup SSH key for hub.wpforms.com upload |
| 104 | + run: | |
| 105 | + mkdir -p ~/.ssh |
| 106 | + echo "${{ secrets.WPFORMS_TEST_DOCKER_SSH_PRIVATE_KEY }}" > ~/.ssh/id_ed25519 |
| 107 | + chmod 600 ~/.ssh/id_ed25519 |
| 108 | + chmod 700 ~/.ssh |
| 109 | +
|
| 110 | + # Creates the necessary directory structure on the local runner before uploading. |
| 111 | + - name: Create builds directory structure |
| 112 | + run: | |
| 113 | + mkdir -p builds/${{ env.PROJECT_NAME }}/${{ env.CI_REF_NAME_SLUG }} |
| 114 | + cp ${{ env.PROJECT_NAME }}.zip builds/${{ env.PROJECT_NAME }}/${{ env.CI_REF_NAME_SLUG }}/ |
| 115 | +
|
| 116 | + # Uploads the build to the deployment server using rsync over SSH. |
| 117 | + # --rsh: Specifies the remote shell to use (ssh with specific options). |
| 118 | + # -o StrictHostKeyChecking=no: Disables host key checking. |
| 119 | + # -p18765: Specifies the port for the SSH connection. |
| 120 | + # --ignore-times: Doesn\'t skip files that have the same size and modification time. |
| 121 | + # --archive: Archive mode, equivalent to -rlptgoD. |
| 122 | + # --verbose: Increases verbosity. |
| 123 | + # --compress: Compresses file data during the transfer. |
| 124 | + # --human-readable: Outputs numbers in a human-readable format. |
| 125 | + # --progress: Shows progress during transfer. |
| 126 | + # --whole-file: Copies files whole (without delta-xfer algorithm). |
| 127 | + # --remove-source-files: Removes files from the source directory after they are transferred. |
| 128 | + - name: Upload to hub.wpforms.com |
| 129 | + run: | |
| 130 | + rsync --rsh="ssh -o StrictHostKeyChecking=no -p18765" \ |
| 131 | + --ignore-times \ |
| 132 | + --archive \ |
| 133 | + --verbose \ |
| 134 | + --compress \ |
| 135 | + --human-readable \ |
| 136 | + --progress \ |
| 137 | + --whole-file \ |
| 138 | + --remove-source-files \ |
| 139 | + builds/ \ |
| 140 | + [email protected]:/home/staging/shared/hub.wpforms.com/builds/athemes/ |
| 141 | +
|
| 142 | + # Uploads the generated zip file as a workflow artifact. |
| 143 | + # This allows the build to be downloaded from the GitHub Actions run page. |
| 144 | + - name: Upload file list as artifact |
| 145 | + uses: actions/upload-artifact@v4 |
| 146 | + with: |
| 147 | + name: ${{ env.PROJECT_NAME }}.zip |
| 148 | + path: ${{ env.PROJECT_NAME }}.zip |
0 commit comments