From 85f20466d05bae97d8545a3948840f7ee9140064 Mon Sep 17 00:00:00 2001 From: Priyadi Iman Nurcahyo <1102197+priyadi@users.noreply.github.com> Date: Mon, 1 Apr 2024 21:44:47 +0000 Subject: [PATCH] build: dockerize demo (#10) * build: dockerize demo * fix permission * fix image.yml * populate database * update * update branch to main --- .github/workflows/{php.yml => ci.yml} | 4 +- .github/workflows/image.yml | 78 +++++++++++++++++++++++++++ Dockerfile | 29 ++++++++++ tests/public/.htaccess | 29 ++++++++++ 4 files changed, 137 insertions(+), 3 deletions(-) rename .github/workflows/{php.yml => ci.yml} (97%) create mode 100644 .github/workflows/image.yml create mode 100644 Dockerfile create mode 100644 tests/public/.htaccess diff --git a/.github/workflows/php.yml b/.github/workflows/ci.yml similarity index 97% rename from .github/workflows/php.yml rename to .github/workflows/ci.yml index 2565aeb..02d78ef 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/ci.yml @@ -1,8 +1,6 @@ -name: Test +name: CI on: - push: - branches: [ "main", "test" ] pull_request: branches: [ "main" ] diff --git a/.github/workflows/image.yml b/.github/workflows/image.yml new file mode 100644 index 0000000..f351f32 --- /dev/null +++ b/.github/workflows/image.yml @@ -0,0 +1,78 @@ +# +name: Image + +# Configures this workflow to run every time a change is pushed to the branch called `release`. +on: + push: + branches: ['main'] + workflow_dispatch: {} + +# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds. +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +# There is a single job in this workflow. It's configured to run on the latest available version of Ubuntu. +jobs: + build-and-push-image: + runs-on: ubuntu-latest + # Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job. + permissions: + contents: read + packages: write + # + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Install PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '8.3' + extensions: intl + tools: flex + + - name: Cache Composer packages + id: composer-cache + uses: actions/cache@v3 + with: + path: vendor + key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} + restore-keys: | + ${{ runner.os }}-php- + + - name: Install dependencies + uses: ramsey/composer-install@v2 + with: + composer-options: --prefer-dist --no-progress + + - name: Populate database + run: | + tests/bin/console doctrine:schema:create + tests/bin/console doctrine:fixtures:load --no-interaction + + # Uses the `docker/login-action` action to log in to the Container registry registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here. + - name: Log in to the Container registry + uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + # This step uses [docker/metadata-action](https://github.com/docker/metadata-action#about) to extract tags and labels that will be applied to the specified image. The `id` "meta" allows the output of this step to be referenced in a subsequent step. The `images` value provides the base name for the tags and labels. + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + + # This step uses the `docker/build-push-action` action to build the image, based on your repository's `Dockerfile`. If the build succeeds, it pushes the image to GitHub Packages. + # It uses the `context` parameter to define the build's context as the set of files located in the specified path. For more information, see "[Usage](https://github.com/docker/build-push-action#usage)" in the README of the `docker/build-push-action` repository. + # It uses the `tags` and `labels` parameters to tag and label the image with the output from the "meta" step. + - name: Build and push Docker image + uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1a7483c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,29 @@ +# Use an official PHP runtime as a parent image +FROM php:8.3-apache +ENV APACHE_DOCUMENT_ROOT /var/www/html/tests/public + +# Set the working directory in the container +WORKDIR /var/www/html + +# Set the Apache document root +RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf +RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf + +# Enable Apache mod_rewrite +RUN a2enmod rewrite + +# Install PHP extensions and other dependencies +RUN apt-get update && \ + apt-get install -y libicu-dev && \ + docker-php-ext-install intl + +# Expose the port Apache listens on +EXPOSE 80 + +# Copy your PHP application code into the container +COPY . . + +RUN mkdir -p /var/www/html/tests/var && chmod -R 777 /var/www/html/tests/var + +# Start Apache when the container runs +CMD ["apache2-foreground"] \ No newline at end of file diff --git a/tests/public/.htaccess b/tests/public/.htaccess new file mode 100644 index 0000000..f405dd9 --- /dev/null +++ b/tests/public/.htaccess @@ -0,0 +1,29 @@ +DirectoryIndex index.php + + Options -MultiViews + + + + RewriteEngine On + RewriteCond %{REQUEST_URI}::$0 ^(/.+)/(.*)::\2$ + RewriteRule .* - [E=BASE:%1] + + # Sets the HTTP_AUTHORIZATION header removed by Apache + RewriteCond %{HTTP:Authorization} .+ + RewriteRule ^ - [E=HTTP_AUTHORIZATION:%0] + + # Removes the /index.php/ part from a URL, if present + RewriteCond %{ENV:REDIRECT_STATUS} ="" + RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L] + + # If the requested filename exists, simply serve it. + # Otherwise rewrite all other queries to the front controller. + RewriteCond %{REQUEST_FILENAME} !-f + RewriteRule ^ %{ENV:BASE}/index.php [L] + + + + + RedirectMatch 307 ^/$ /index.php/ + +