Skip to content

Commit e00d305

Browse files
committed
fix: support testing in GitHub Actions on Docker (#29)
this fixes #5 #24
1 parent fb27265 commit e00d305

4 files changed

Lines changed: 180 additions & 1 deletion

File tree

.github/workflows/unit-tests.yml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
name: UnitTests
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches: [ master ]
7+
pull_request:
8+
branches: [ master ]
9+
10+
env:
11+
PATH_CACHE: /tmp/docker-img-arch
12+
13+
jobs:
14+
php:
15+
name: Run tests via container
16+
runs-on: ubuntu-latest
17+
steps:
18+
- name: Skip Duplicate Actions
19+
uses: fkirc/skip-duplicate-actions@v5
20+
21+
- name: Checkout repo
22+
uses: actions/checkout@v4
23+
24+
- name: Create image tag from file hash
25+
uses: KEINOS/gh-action-hash-for-cache@main
26+
id: imagetag
27+
# Udate the hash if any of the below file is changed or the month has changed.
28+
with:
29+
path: |
30+
./Extension.php
31+
./composer.json
32+
./Dockerfile
33+
./docker-compose.yml
34+
variant: $(TZ=UTC-9 date '+%Y%m')
35+
36+
- name: Enable Cache
37+
id: cache
38+
uses: actions/cache@v4
39+
with:
40+
path: ${{ env.PATH_CACHE }}
41+
key: ${{ steps.imagetag.outputs.hash }}
42+
43+
- name: Load cached Docker images if any
44+
if: steps.cache.outputs.cache-hit == 'true'
45+
run: |
46+
docker load --input ${{ env.PATH_CACHE }}/${{ steps.imagetag.outputs.hash }}/github_min_1.tar
47+
docker load --input ${{ env.PATH_CACHE }}/${{ steps.imagetag.outputs.hash }}/github_max_1.tar
48+
docker load --input ${{ env.PATH_CACHE }}/${{ steps.imagetag.outputs.hash }}/github_latest_1.tar
49+
50+
- name: Pull base images if no-cache
51+
if: steps.cache.outputs.cache-hit != 'true'
52+
run: |
53+
: # Pull images for stability.
54+
docker compose pull
55+
56+
- name: Build Docker images if no-cache
57+
if: steps.cache.outputs.cache-hit != 'true'
58+
run: |
59+
docker compose build
60+
61+
- name: Run tests on minimum supported PHP version
62+
run: docker compose run --rm min
63+
64+
- name: Run tests on maximum supported PHP version
65+
run: docker compose run --rm max
66+
67+
- name: Run tests on latest PHP version (currently experimental. Expected to fail)
68+
run: docker compose run --rm latest || exit 0
69+
70+
- name: Save/export built images to cache dir if no-cache
71+
if: steps.cache.outputs.cache-hit != 'true'
72+
run: |
73+
mkdir -p ${{ env.PATH_CACHE }}/${{ steps.imagetag.outputs.hash }}
74+
docker save --output ${{ env.PATH_CACHE }}/${{ steps.imagetag.outputs.hash }}/github_min_1.tar parsedown-extension_table-of-contents-min:latest
75+
docker save --output ${{ env.PATH_CACHE }}/${{ steps.imagetag.outputs.hash }}/github_max_1.tar parsedown-extension_table-of-contents-max:latest
76+
docker save --output ${{ env.PATH_CACHE }}/${{ steps.imagetag.outputs.hash }}/github_latest_1.tar parsedown-extension_table-of-contents-latest:latest

Dockerfile

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# =============================================================================
2+
# Dockerfile for Testing
3+
#
4+
# Note: To run the container, DO NOT forget to mount the current directory
5+
# to /app in the container.
6+
# =============================================================================
7+
# Use the latest PHP on Alpine Linux by default
8+
ARG PHP_VERSION="cli-alpine"
9+
10+
# -----------------------------------------------------------------------------
11+
# First stage: preparation
12+
# -----------------------------------------------------------------------------
13+
FROM php:${PHP_VERSION} AS prepare
14+
15+
WORKDIR /app
16+
17+
# Create composer installer script
18+
COPY --chmod=755 <<'HEREDOC' /app/installer.sh
19+
#!/bin/sh
20+
21+
EXPECTED_CHECKSUM="$(php -r 'copy("https://composer.github.io/installer.sig", "php://stdout");')"
22+
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
23+
ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"
24+
25+
if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]
26+
then
27+
>&2 echo 'ERROR: Invalid installer checksum'
28+
rm composer-setup.php
29+
exit 1
30+
fi
31+
32+
php composer-setup.php ---quiet
33+
RESULT=$?
34+
rm composer-setup.php
35+
exit $RESULT
36+
HEREDOC
37+
38+
RUN /app/installer.sh
39+
40+
# -----------------------------------------------------------------------------
41+
# Final stage
42+
# -----------------------------------------------------------------------------
43+
44+
FROM php:${PHP_VERSION}
45+
46+
ENV PATH="~/.composer/vendor/bin:${PATH}"
47+
48+
# Stay up-to-date and install git
49+
RUN apk --no-cache add \
50+
git \
51+
jq \
52+
bash \
53+
curl \
54+
&& apk --no-cache upgrade
55+
56+
COPY --from=prepare /app/composer.phar /usr/local/bin/composer
57+
58+
WORKDIR /app
59+
60+
ENTRYPOINT ["/bin/bash", "-c"]
61+
CMD ["./tests/run-tests.sh"]

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
}
1717
],
1818
"require": {
19-
"php": ">=5.3.0",
19+
"php": ">=5.3.0 <=8.3",
2020
"ext-mbstring": "*",
2121
"erusev/parsedown": "^1.7"
2222
},

docker-compose.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# =============================================================================
2+
# Docker Compose file for testing on different PHP versions.
3+
#
4+
# Usage:
5+
# # Doewnload the base images for stability
6+
# $ docker compose pull
7+
#
8+
# # Build the images
9+
# $ docker compose build
10+
#
11+
# # Run the tests in the container for various PHP versions
12+
# $ docker compose run --rm min
13+
# $ docker compose run --rm max
14+
# $ docker compose run --rm latest
15+
# =============================================================================
16+
17+
services:
18+
# Minimum supported PHP version. Which is equivalent to Parsedown's minimum
19+
# supported version.
20+
min:
21+
build:
22+
dockerfile: ./Dockerfile
23+
args:
24+
PHP_VERSION: 7.1-cli-alpine
25+
volumes:
26+
- .:/app
27+
# Currently supported maximum PHP version.
28+
max:
29+
build:
30+
dockerfile: ./Dockerfile
31+
args:
32+
PHP_VERSION: 8.3-cli-alpine
33+
volumes:
34+
- .:/app
35+
# Experimental latest PHP version. Currently errors out.
36+
latest:
37+
build:
38+
dockerfile: ./Dockerfile
39+
args:
40+
PHP_VERSION: cli-alpine
41+
volumes:
42+
- .:/app

0 commit comments

Comments
 (0)