Skip to content

Commit b694a48

Browse files
Added super simple Cypress tests + GitHub Actions to run those (#19)
Tested and is all functional, I'll add some instructions on the readme file on how to add the Github secrets for the auth.json in the correct format.
1 parent c6bbd6b commit b694a48

File tree

2 files changed

+240
-1
lines changed

2 files changed

+240
-1
lines changed

.gitpod.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ tasks:
3838
composer create-project --no-interaction --no-progress --repository-url=https://repo.magento.com/ magento/project-community-edition=${MAGENTO_VERSION} magento2 &&
3939
cd magento2 && cp -avr .* $GITPOD_REPO_ROOT;
4040
cd $GITPOD_REPO_ROOT && rm -r -f magento2 && git checkout -- .gitignore;
41-
npm install cypress --save-dev
41+
npm install cypress --save-dev;
42+
mkdir -p .github/workflows && cp $GITPOD_REPO_ROOT/gitpod/end-2-end-test.yml .github/workflows/end-2-end-test.yml
4243
command: gp ports await 3306 &&
4344
cd $GITPOD_REPO_ROOT &&
4445
test ! -f $GITPOD_REPO_ROOT/gitpod/db-installed.flag && $GITPOD_REPO_ROOT/gitpod/m2-install.sh ;

gitpod/end-2-end-test.yml

Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
name: MageTested.com - End 2 End Tests
2+
3+
on:
4+
# Enable this line to run the tests on every push
5+
push:
6+
workflow_dispatch:
7+
pull_request:
8+
types:
9+
- opened
10+
- labeled
11+
12+
jobs:
13+
# Remove flag used to trigger the e2e tests
14+
remove_flag:
15+
if: ${{ contains(github.event.*.labels.*.name, 'run_e2e_tests') }}
16+
runs-on: ubuntu-latest
17+
steps:
18+
- name: Remove run E2E tests label
19+
uses: actions/github-script@v5
20+
with:
21+
script: |
22+
github.rest.issues.removeLabel({
23+
issue_number: ${{ github.event.issue.number || github.event.number }},
24+
owner: context.repo.owner,
25+
repo: context.repo.repo,
26+
name: "run_e2e_tests"
27+
})
28+
29+
e2e-tests:
30+
runs-on: ubuntu-latest
31+
32+
env:
33+
WORKING_DIR: ./
34+
BIN_MAGENTO: bin/magento
35+
MAGENTO_LOCALES: en_US
36+
THEME_PATH: Magento/luma
37+
38+
steps:
39+
- uses: actions/checkout@v3
40+
with:
41+
lfs: true
42+
43+
- name: Generate auth.json when COMPOSER_AUTH_JSON is set
44+
env:
45+
auth_json: ${{ secrets.COMPOSER_AUTH_JSON }}
46+
if: ${{ env.auth_json != '' }}
47+
run: echo "$auth_json" > auth.json
48+
49+
# Get the composer cache directory so we can save it so GitHub Actions cache and restore it in the next run
50+
- name: Get Composer Cache Directory
51+
id: composer-cache
52+
run: |
53+
if ! test -f "auth.json"; then
54+
echo "Warning: You don't have an auth.json in place. Either commit it to this repository, or add it as a secret to your GitHub repository as COMPOSER_AUTH_JSON."
55+
exit 1;
56+
fi
57+
composer validate --working-dir=$WORKING_DIR
58+
echo "dir=$(composer config cache-files-dir --working-dir=$WORKING_DIR)" >> $GITHUB_OUTPUT
59+
60+
# Cache composer dependencies so the next run will be faster. Do NOT cache the vendor folder, as that would
61+
# it would not trigger the automatic creation of bin/magento and other files.
62+
- name: Cache vendor
63+
uses: actions/cache@v3
64+
with:
65+
path: |
66+
${{ steps.composer-cache.outputs.dir }}
67+
key: vendor-${{ hashFiles('**/composer.lock') }}
68+
69+
- name: Runs Mailcatcher
70+
run: |
71+
docker run -d -p 1080:1080 -p 1025:1025 --name mailcatcher schickling/mailcatcher
72+
go install github.com/mailhog/mhsendmail@latest
73+
74+
# Start mysql. If you have a database in place you can import it here.
75+
- name: Start mysql & import database
76+
run: |
77+
sudo /etc/init.d/mysql start
78+
mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';" -uroot -proot
79+
mysql -e 'CREATE DATABASE magento;' -uroot -proot
80+
mysql -e "CREATE USER 'magento'@'localhost' IDENTIFIED BY 'magento';" -uroot -proot
81+
mysql -e "GRANT ALL PRIVILEGES ON magento.* TO 'magento'@'localhost';" -uroot -proot
82+
mysql -e "FLUSH PRIVILEGES;" -uroot -proot
83+
84+
- name: Set up environment with secret
85+
run: |
86+
if [ -z "${{ secrets.SECRET_KEY }}" ]; then
87+
echo "USE_SECRET_KEY=DefaultSecretKeyStringThatIsLong" >> $GITHUB_ENV
88+
else
89+
echo "USE_SECRET_KEY=${{ secrets.SECRET_KEY }}" >> $GITHUB_ENV
90+
fi
91+
92+
# Copy the env.php.end-2-end file to env.php and replace the secret key
93+
# - name: Dump env.php
94+
# run: |
95+
# #!/bin/bash
96+
# set -e
97+
# SECRET_KEY="${{ env.USE_SECRET_KEY }}"
98+
# FILE="$WORKING_DIR/app/etc/env.php"
99+
# cp "$FILE.end-2-end" $FILE
100+
# sed -i "s/{{SECRET_KEY}}/$SECRET_KEY/g" $FILE
101+
102+
# Prepare for Elasticsearch
103+
- name: Configure sysctl limits
104+
run: |
105+
sudo swapoff -a
106+
sudo sysctl -w vm.swappiness=1
107+
sudo sysctl -w fs.file-max=262144
108+
sudo sysctl -w vm.max_map_count=262144
109+
110+
# Start Elasticsearch
111+
- name: Runs Elasticsearch
112+
uses: elastic/elastic-github-actions/elasticsearch@master
113+
with:
114+
stack-version: 7.6.0
115+
116+
- name: Runs Redis
117+
uses: superchargejs/redis-github-action@1.1.0
118+
119+
# Off-course we need PHP
120+
- name: Setup PHP
121+
uses: shivammathur/setup-php@v2
122+
with:
123+
php-version: '8.1'
124+
ini-values: |
125+
error_log=${{ github.workspace }}/${{ env.WORKING_DIR }}/var/log/php-error.log,sendmail_path="/home/runner/go/bin/mhsendmail --smtp-addr='localhost:1025'"
126+
127+
# Install node for building Hyva
128+
- uses: actions/setup-node@v3
129+
with:
130+
node-version: '16'
131+
132+
# Run composer install
133+
- name: Run Composer Install
134+
run: |
135+
composer install --no-interaction --no-progress --working-dir=$WORKING_DIR
136+
composer require n98/magerun2-dist --dev
137+
138+
# Enable this when applicable: Build Hyvä
139+
#- name: Build Hyvä
140+
# run: |
141+
# npm --prefix $WORKING_DIR/app/design/frontend/$THEME_PATH/web/tailwind ci
142+
# npm run --prefix $WORKING_DIR/app/design/frontend/$THEME_PATH/web/tailwind build-prod
143+
144+
# If you don't have an database in place you can go with the default Magento install
145+
- name: Run Magento Install
146+
run: |
147+
$BIN_MAGENTO setup:install \
148+
--backend-frontname=admin \
149+
--db-host=localhost \
150+
--db-name=magento \
151+
--db-user=magento \
152+
--db-password=magento \
153+
--search-engine=opensearch \
154+
--elasticsearch-host=localhost \
155+
--elasticsearch-port=9200 \
156+
--elasticsearch-index-prefix=magento2 \
157+
--elasticsearch-enable-auth=0 \
158+
--elasticsearch-timeout=15 \
159+
--session-save=redis \
160+
--session-save-redis-host=localhost \
161+
--session-save-redis-port=6379 \
162+
--session-save-redis-db=2 \
163+
--session-save-redis-max-concurrency=20 \
164+
--cache-backend=redis \
165+
--cache-backend-redis-server=localhost \
166+
--cache-backend-redis-db=0 \
167+
--cache-backend-redis-port=6379 \
168+
--page-cache=redis \
169+
--page-cache-redis-server=localhost \
170+
--page-cache-redis-db=1 \
171+
--page-cache-redis-port=6379 \
172+
--base-url=https://localhost \
173+
--timezone=Europe/London \
174+
--currency=EUR \
175+
--admin-user=magetested \
176+
--admin-password=magetested1 \
177+
--admin-email=info@magetested.com \
178+
--admin-firstname=Magetested \
179+
--admin-lastname=Magetested \
180+
--use-rewrites=1
181+
182+
# Run Magento Setup
183+
- name: Run Magento setup:upgrade
184+
run: |
185+
$BIN_MAGENTO indexer:reindex
186+
$BIN_MAGENTO config:set system/smtp/disable 0
187+
$BIN_MAGENTO config:set system/smtp/transport smtp
188+
$BIN_MAGENTO config:set system/smtp/port 1025
189+
# smtp/general/enabled == mageplaza smtp module
190+
# $BIN_MAGENTO config:set smtp/general/enabled 1
191+
# $BIN_MAGENTO config:set smtp/configuration_option/host localhost
192+
# $BIN_MAGENTO config:set smtp/configuration_option/port 1025
193+
# $BIN_MAGENTO config:set smtp/configuration_option/username ""
194+
# $BIN_MAGENTO config:set smtp/configuration_option/password ""
195+
196+
- name: Run setup:static-content:deploy
197+
run: $BIN_MAGENTO setup:static-content:deploy -f --area frontend $MAGENTO_LOCALES -j 12
198+
199+
# Start the PHP server and redirect all output to var/log/php-server.log
200+
- name: Start server
201+
run: nohup php -S 0.0.0.0:8080 -t $WORKING_DIR/pub/ $WORKING_DIR/phpserver/router.php > $WORKING_DIR/var/log/php-server.log 2>&1 &
202+
203+
# Set the correct base url and check if the server is online
204+
- name: Check if server is online
205+
run: |
206+
$BIN_MAGENTO
207+
$BIN_MAGENTO config:set web/secure/base_url http://localhost:8080/
208+
$BIN_MAGENTO config:set web/unsecure/base_url http://localhost:8080/
209+
$BIN_MAGENTO config:set web/secure/base_link_url http://localhost:8080/
210+
$BIN_MAGENTO config:set web/unsecure/base_link_url http://localhost:8080/
211+
curl --fail-with-body -v http://localhost:8080
212+
213+
# If no package.json is present, copy package.json.sample to package.json
214+
- name: Copy package.json.sample to package.json
215+
run: |
216+
if ! test -f "$WORKING_DIR/package.json"; then
217+
cp "$WORKING_DIR/package.json.sample" "$WORKING_DIR/package.json"
218+
npm install cypress --save-dev
219+
fi
220+
221+
# Run Cypress tests
222+
- name: Run Cypress tests
223+
uses: cypress-io/github-action@v6
224+
with:
225+
browser: chrome
226+
config: baseUrl=http://localhost:8080,defaultCommandTimeout=10000
227+
228+
# Upload artifacts on failure
229+
- name: Upload artifacts
230+
uses: actions/upload-artifact@v3
231+
if: failure()
232+
with:
233+
name: Cypress logs ${{ github.run_number }}
234+
path: |
235+
${{ env.WORKING_DIR }}/cypress/videos
236+
${{ env.WORKING_DIR }}/cypress/screenshots
237+
${{ env.WORKING_DIR }}/var/log
238+
${{ env.WORKING_DIR }}/var/report

0 commit comments

Comments
 (0)