How to make your own CI container for github actions

Draft Disclaimer: Please note that this article is currently in draft form and may undergo revisions before final publication. The content, including information, opinions, and recommendations, is subject to change and may not represent the final version. We appreciate your understanding and patience as we work to refine and improve the quality of this article. Your feedback is valuable in shaping the final release.

Language Mismatch Disclaimer: Please be aware that the language of this article may not match the language settings of your browser or device.
Do you want to read articles in English instead ?

How to make your own CI container for github actions

  • able to run your github actions online
  • context: needed to install more php extensions
  • avoid repetition between linting and testing workflows Lint
on: push
name: Code Style
jobs:
  php:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: 7.4
          tools: pecl
          extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, couchbase
          coverage: none
        # By default, extensions which cannot be added or removed gracefully leave an error message in the logs, the action is not interrupted. To change this behaviour you can set fail-fast flag to true.
        env:
          fail-fast: true

      - name: Get Composer Cache Directory
        id: composer-cache
        run: |
          echo "::set-output name=dir::$(composer config cache-files-dir)"

      - uses: actions/cache@v2
        with:
          path: ${{ steps.composer-cache.outputs.dir }}
          key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: |
            ${{ runner.os }}-composer-

      - name: Install Composer dependencies
        run: composer global require friendsofphp/php-cs-fixer

      - name: Check PHP CS Fixer version
        run: php-cs-fixer --version

      - name: Check Coding Standards
        run: composer lint

Test

on: push
name: Tests
jobs:
  php:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: 7.4
          tools: pecl
          extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, couchbase
          coverage: none
        # By default, extensions which cannot be added or removed gracefully leave an error message in the logs, the action is not interrupted. To change this behaviour you can set fail-fast flag to true.
        env:
          fail-fast: true

      - name: Run test suites
        run: composer test
  • using a custom customer
FROM ubuntu:20.04
LABEL maintainer="Optimus Debugger <[email protected]>"
ARG DEBIAN_FRONTEND=noninteractive

ENV GOSS_VERSION="0.3.6"

RUN apt-get update && apt-get install -y software-properties-common curl
RUN add-apt-repository ppa:ondrej/php -y
RUN add-apt-repository ppa:git-core/ppa -y
RUN apt-get update -y
RUN apt-get install -y \
    unzip  \
    php7.4-cli \
    php7.4-gd \
    php7.4-json \
    php7.4-ldap \
    php7.4-mbstring \
    php7.4-mysql \
    php7.4-pgsql \
    php7.4-sqlite3 \
    php7.4-xml \
    php7.4-xsl \
    php7.4-zip \
    php7.4-curl \
    php7.4-soap \
    php7.4-gmp \
    php7.4-bcmath \
    php7.4-imagick \
    php7.4-intl \
    php7.4-redis \
    php7.4-imap \
    php7.4-phpdbg \
    php7.4-dom \
    php7.4-pdo \
    php7.4-sqlite \
    php7.4-bcmath \
    php7.4-intl \
    php7.4-soap \
    php7.4-exif \
    php7.4-iconv \
    php7.4-imagick

# composer
ENV COMPOSER_HOME=/composer
ENV PATH=./vendor/bin:/composer/vendor/bin:/root/.yarn/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
ENV COMPOSER_ALLOW_SUPERUSER=1
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# mysql client
RUN apt-get install -y mysql-client

# git
RUN apt-get install -y git

# node and yarn
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash -
RUN apt-get install -y nodejs
RUN npm install -g yarn

# goss
RUN curl -fsSL https://goss.rocks/install | GOSS_VER=v${GOSS_VERSION} sh

## Install PECL
RUN apt-get install -y php-pear php7.4-dev php7.4-xml

## MongoDB
RUN pecl install mongodb-1.10.0alpha1
  • Usage become

Lint

on: push
name: Code Style
jobs:
  php:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1

      - name: Install system dependencies
        run: |
          sudo wget -O - https://packages.couchbase.com/ubuntu/couchbase.key | sudo apt-key add -
          echo "deb https://packages.couchbase.com/ubuntu bionic bionic/main" | sudo tee /etc/apt/sources.list.d/couchbase.list
          sudo apt-get update
          sudo apt-get install libcouchbase-dev

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: 7.4
          tools: pecl
          extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, couchbase
          coverage: none
        # By default, extensions which cannot be added or removed gracefully leave an error message in the logs, the action is not interrupted. To change this behaviour you can set fail-fast flag to true.
        env:
          fail-fast: true

      - name: Get Composer Cache Directory
        id: composer-cache
        run: |
          echo "::set-output name=dir::$(composer config cache-files-dir)"

      - uses: actions/cache@v2
        with:
          path: ${{ steps.composer-cache.outputs.dir }}
          key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: |
            ${{ runner.os }}-composer-

      - name: Install Composer dependencies
        run: composer global require friendsofphp/php-cs-fixer

      - name: Check PHP CS Fixer version
        run: php-cs-fixer --version

      - name: Check Coding Standards
        run: composer lint

Test

on: push
name: Tests
jobs:
  php:
    runs-on: ubuntu-latest
    container:
      image: your-org/laravel-test-runner:7.4

    steps:
      - uses: actions/checkout@v1

      - name: Run test suites
        run: composer test
  • Simpler is better
  • CI to auto publish and validate publish: .github/workflows/php-7.4-publish.yml
name: php-7.4-publish
on:
push:
branches:
- master
jobs:
publish:
runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v1

    - name: Publish to Registry
      uses: elgohr/Publish-Docker-Github-Action@master
      with:
        name: pfizer/canvas-test-runner
        username: ${{ secrets.DOCKER_USERNAME }}
        password: ${{ secrets.DOCKER_PASSWORD }}
        workdir: 7.4
        tags: "7.4"

validate: .github/workflows/php-7.4-validate.yml

on: push
name: php-7.4-validate
jobs:
  validate:
    runs-on: ubuntu-latest
    container:
      image: pfizer/canvas-test-runner:7.4

    steps:
    - uses: actions/checkout@v1
    - run: goss --gossfile 7.4/goss.yaml validate

  • goss goss.yaml
command:
  node --version:
    exit-status: 0
    stdout:
      - "v12"
  yarn --version:
    exit-status: 0
  npm --version:
    exit-status: 0
    stdout:
      - "6"
  git --version:
    exit-status: 0
  composer --version:
    exit-status: 0
  php --version:
    exit-status: 0
    stdout:
      - 7
  php -m:
    exit-status: 0
    stdout:
      - bcmath
      - calendar
      - exif
      - gd
      - iconv
      - imagick
      - imap
      - intl
      - ldap
      - mbstring
      - mysqli
      - pcntl
      - pdo_mysql
      - pdo_pgsql
      - pgsql
      - soap
      - xml
      - zip
      - mongodb
      - couchbase