image: edbizarro/gitlab-ci-pipeline-php:7.3-alpine stages: - prepare - testing - build - cleanup variables: APP_IMAGE_NAME: "$CI_REGISTRY_IMAGE/app" WEB_IMAGE_NAME: "$CI_REGISTRY_IMAGE/web" DB_IMAGE_NAME: "$CI_REGISTRY_IMAGE/db" ####################### # Shared script steps # ####################### # GitLab do not supports bash syntax in the "variables" definitions, # so we use custom step to define all necessary environment variables .defineVars: &defineVars |- export VERSION="${CI_COMMIT_TAG:-${CI_COMMIT_REF_NAME}-${CI_COMMIT_SHORT_SHA}}" export APP_VERSIONED_IMAGE_NAME="$APP_IMAGE_NAME:$VERSION" export APP_LATEST_IMAGE_NAME="$APP_IMAGE_NAME:latest" export WEB_VERSIONED_IMAGE_NAME="$WEB_IMAGE_NAME:$VERSION" export WEB_LATEST_IMAGE_NAME="$WEB_IMAGE_NAME:latest" export DB_VERSIONED_IMAGE_NAME="$DB_IMAGE_NAME:$VERSION" export DB_LATEST_IMAGE_NAME="$DB_IMAGE_NAME:latest" .dockerLogin: &dockerLogin |- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY ################### # Steps to extend # ################### .vendorCache: cache: key: backend-deps paths: - vendor policy: pull ################# # Prepare stage # ################# Precache dependencies: stage: prepare extends: - .vendorCache cache: policy: pull-push script: - composer install --ignore-platform-reqs ################# # Testing stage # ################# PHP-CS-Fixer: stage: testing extends: - .vendorCache script: - vendor/bin/php-cs-fixer fix -v --dry-run Codeception: stage: testing extends: - .vendorCache services: - name: redis:4.0.10-alpine alias: redis - name: bitnami/mariadb:10.3.20-debian-9-r4 alias: db variables: # App config DB_HOST: "db" DB_DATABASE: "ely_accounts_test" DB_USER: "ely_accounts_tester" DB_PASSWORD: "ely_accounts_tester_password" REDIS_HOST: "redis" REDIS_PORT: "6379" # MariaDB config ALLOW_EMPTY_PASSWORD: "yes" MARIADB_DATABASE: "ely_accounts_test" MARIADB_USER: "ely_accounts_tester" MARIADB_PASSWORD: "ely_accounts_tester_password" before_script: # We don't count code coverage yet, so xdebug can be removed - sudo rm /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini script: - php yii rbac/generate - ./docker/php/wait-for-it.sh "${DB_HOST}:3306" -s -t 0 -- "php yii migrate/up --interactive=0" - vendor/bin/codecept run ############### # Build stage # ############### Docker images: stage: build image: docker:19.03.2 before_script: - *defineVars - *dockerLogin - sed -i -e "s/{{PLACE_VERSION_HERE}}/$VERSION/g" common/config/config.php script: # Download previous images to use them as a cache - docker pull "$APP_LATEST_IMAGE_NAME" || true - docker pull "$WEB_LATEST_IMAGE_NAME" || true - docker pull "$DB_LATEST_IMAGE_NAME" || true # Build images - > docker build --pull --target app --build-arg "build_env=prod" --cache-from "$APP_LATEST_IMAGE_NAME" -t "$APP_VERSIONED_IMAGE_NAME" -t "$APP_LATEST_IMAGE_NAME" . - > docker build --pull --target web --build-arg "build_env=prod" --cache-from "$APP_VERSIONED_IMAGE_NAME" --cache-from "$WEB_LATEST_IMAGE_NAME" -t "$WEB_VERSIONED_IMAGE_NAME" -t "$WEB_LATEST_IMAGE_NAME" . - > docker build --pull --target db --build-arg "build_env=prod" --cache-from "$APP_VERSIONED_IMAGE_NAME" --cache-from "$WEB_VERSIONED_IMAGE_NAME" --cache-from "$DB_LATEST_IMAGE_NAME" -t "$DB_VERSIONED_IMAGE_NAME" -t "$DB_LATEST_IMAGE_NAME" . # Push images to the registry - docker push $APP_VERSIONED_IMAGE_NAME - docker push $APP_LATEST_IMAGE_NAME - docker push $WEB_VERSIONED_IMAGE_NAME - docker push $WEB_LATEST_IMAGE_NAME - docker push $DB_VERSIONED_IMAGE_NAME - docker push $DB_LATEST_IMAGE_NAME only: - master - tags ################# # Cleanup stage # ################# Remove Docker images: stage: cleanup image: docker:19.03.2 variables: GIT_STRATEGY: none before_script: - *defineVars script: - docker rmi "$APP_LATEST_IMAGE_NAME" "$APP_VERSIONED_IMAGE_NAME" || true - docker rmi "$WEB_LATEST_IMAGE_NAME" "$WEB_VERSIONED_IMAGE_NAME" || true - docker rmi "$DB_LATEST_IMAGE_NAME" "$DB_VERSIONED_IMAGE_NAME" || true when: always only: - master - tags