From 7557007a3ff90faaa461db56667ddc2142abb693 Mon Sep 17 00:00:00 2001 From: Giuseppe Arcifa Date: Fri, 21 Feb 2025 15:38:41 +0100 Subject: [PATCH 1/2] Add GitHub Action to deploy and test the plugin in a target environment --- .github/workflows/deploy-and-test.yml | 104 ++++++++++++++++++++++++++ cypress.config.js | 8 +- 2 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/deploy-and-test.yml diff --git a/.github/workflows/deploy-and-test.yml b/.github/workflows/deploy-and-test.yml new file mode 100644 index 00000000..d09e94b2 --- /dev/null +++ b/.github/workflows/deploy-and-test.yml @@ -0,0 +1,104 @@ +name: Deploy Plugin to Server + +on: + push: + branches: + - main + workflow_dispatch: + +env: + PLUGIN_NAME: bluehost-wordpress-plugin + CYPRESS_TEST_PATH: tests/cypress/integration/help.cy.js +jobs: + deploy: + runs-on: ubuntu-latest + strategy: + matrix: + environment: [bluehost-shared] + fail-fast: false + + environment: ${{ matrix.environment }} + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Setup Node.js + uses: actions/setup-node@v3 + with: + node-version: 18 + registry-url: "https://npm.pkg.github.com" + scope: "@newfold-labs" + + - name: Authenticate with GitHub Packages + run: | + echo "@newfold-labs:registry=https://npm.pkg.github.com" >> .npmrc + echo "//npm.pkg.github.com/:_authToken=${{ secrets.NEWFOLD_ACCESS_TOKEN }}" >> .npmrc + + - name: Install PHP and Composer + run: | + sudo apt update + sudo apt install -y php-cli unzip curl + curl -sS https://getcomposer.org/installer | php + sudo mv composer.phar /usr/local/bin/composer + + - name: Install Composer dependencies + run: composer install --no-dev --optimize-autoloader + + - name: Install Node dependencies and build project + run: | + npm install + npm run build + npm run create:dev + + - name: Upload build artifact + uses: actions/upload-artifact@v4 + with: + name: plugin-build-${{ matrix.environment }} + path: ./${{ env.PLUGIN_NAME }}.zip + + - name: Setup SSH Key + run: | + echo "${{ secrets.SERVER_SSH_PRIVATE_KEY }}" > github-actions + chmod 600 github-actions + + - name: Copy .zip file to server via SCP + run: | + scp -i github-actions -o StrictHostKeyChecking=no ./${{ env.PLUGIN_NAME }}.zip ${{ vars.SERVER_USERNAME }}@${{ vars.SERVER_IP }}:${{ vars.SERVER_PATH }}/wp-content/uploads + + - name: Extract Plugin and Activate + run: | + ssh -i github-actions -o StrictHostKeyChecking=no ${{ vars.SERVER_USERNAME }}@${{ vars.SERVER_IP }} << 'EOF' + cd ${{ vars.SERVER_PATH }}/wp-content + wp plugin install uploads/${{ env.PLUGIN_NAME }}.zip --force --path=./../ + rm uploads/${{ env.PLUGIN_NAME }}.zip + wp plugin activate ${{ env.PLUGIN_NAME }} --path=./../ + EOF + + - name: Cleanup SSH Key + run: rm -f github-actions + + - name: Wait for Server to be Ready + run: | + for i in {1..30}; do + STATUS_CODE=$(curl -s -o /dev/null -w "%{http_code}" "${{ vars.SITE_URL }}/wp-login.php") + if [[ "$STATUS_CODE" == "200" ]]; then + echo "Server is up with status code: $STATUS_CODE" + exit 0 + fi + echo "Waiting for server to be ready... (Last status: $STATUS_CODE)" + sleep 10 + done + echo "Server not ready after 5 minutes" && exit 1 + + - name: Run Specific Cypress Tests + uses: cypress-io/github-action@v6 + with: + install: true + start: npm start + wait-on: ${{ vars.SITE_URL }} + config: baseUrl=${{ vars.SITE_URL }} + command: npx cypress run --spec ${{ env.CYPRESS_TEST_PATH }} + env: + BASE_URL: ${{ vars.SITE_URL }} + WP_ADMIN_USERNAME: ${{ secrets.WP_ADMIN_USERNAME }} + WP_ADMIN_PASSWORD: ${{ secrets.WP_ADMIN_PASSWORD }} diff --git a/cypress.config.js b/cypress.config.js index a05e82f2..eb4fcbef 100644 --- a/cypress.config.js +++ b/cypress.config.js @@ -5,13 +5,13 @@ const wpVersion = /[^/]*$/.exec( core )[ 0 ]; module.exports = defineConfig( { projectId: '71eo94', env: { - wpUsername: 'admin', - wpPassword: 'password', - wpVersion, + baseUrl: process.env.BASE_URL || 'http://localhost:8882', + wpUsername: process.env.WP_ADMIN_USERNAME || 'admin', + wpPassword: process.env.WP_ADMIN_PASSWORD || 'password', phpVersion, pluginId: 'bluehost', appId: 'wppbh', - pluginSlug: 'wp-plugin-bluehost', + pluginSlug: 'wp-plugin-bluehost' }, downloadsFolder: 'tests/cypress/downloads', fixturesFolder: 'tests/cypress/fixtures', From 1419881b95434b6fd4de6fb5cb4f37a607ab6799 Mon Sep 17 00:00:00 2001 From: Giuseppe Arcifa Date: Mon, 24 Feb 2025 16:37:22 +0100 Subject: [PATCH 2/2] Updated GH Workflowf to use Node version v20 Updated step name --- .github/workflows/deploy-and-test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/deploy-and-test.yml b/.github/workflows/deploy-and-test.yml index d09e94b2..5903c44f 100644 --- a/.github/workflows/deploy-and-test.yml +++ b/.github/workflows/deploy-and-test.yml @@ -25,7 +25,7 @@ jobs: - name: Setup Node.js uses: actions/setup-node@v3 with: - node-version: 18 + node-version: 20 registry-url: "https://npm.pkg.github.com" scope: "@newfold-labs" @@ -77,7 +77,7 @@ jobs: - name: Cleanup SSH Key run: rm -f github-actions - - name: Wait for Server to be Ready + - name: Check if the remote Server is up run: | for i in {1..30}; do STATUS_CODE=$(curl -s -o /dev/null -w "%{http_code}" "${{ vars.SITE_URL }}/wp-login.php")