edbizarro / gitlab-ci-pipeline-php

:coffee: Docker images for test PHP applications with Gitlab CI (or any other CI platform!)
https://hub.docker.com/r/edbizarro/gitlab-ci-pipeline-php
MIT License
528 stars 166 forks source link

How to set timezone for gitlab-ci #85

Open tenken opened 4 years ago

tenken commented 4 years ago

Hi,

I have noticed when I use this container as my main image for a stage, Bash within the container can match the same TZ as the Host OS. But when php uses date or something I get UTC output.

Example .gitlab-ci.yml file

# This file is a template, and might need editing before it works on your project.
# Select image from https://hub.docker.com/_/php/
image: edbizarro/gitlab-ci-pipeline-php:7.2

# Bring in any services we need http://docs.gitlab.com/ee/ci/docker/using_docker_images.html#what-is-a-service
# See http://docs.gitlab.com/ce/ci/services/README.html for examples.
services:
  - {name: 'tenken/mysqlci:8.0.16', alias: 'mysql-service'}

# Set any variables we need
variables:
  # Configure mysql environment variables (https://hub.docker.com/r/_/mysql/)
  MYSQL_ROOT_PASSWORD: test
  MYSQL_USER: test
  MYSQL_PASSWORD: test
  MYSQL_DATABASE: testdb
  # https://gitlab.com/gitlab-org/gitlab-runner/issues/4405#note_225960882
  MYSQL_INITDB_SKIP_TZINFO: 1
  DB_HOST: mysql-service
  CURRENT_QUARTER: $CURRENT_QUARTER
  GIT_SSL_NO_VERIFY: 1

# Run our tests
# If Xdebug was installed you can generate a coverage report and see code coverage metrics.
test:
  script:
    - php test.php
    - date
    # Output some MYSQL information to assure DBs created ok.
    - mysql -h $DB_HOST -e "SHOW VARIABLES LIKE '%version%';" -uroot -p$MYSQL_ROOT_PASSWORD
    # To debug the container add the following line that will keep the container alive forever.
    - tail -f /dev/null

Output

php date is: 2020-01-14 09:44:17 UTC
$ date
818 Tue Jan 14 13:44:17 PST 2020

Is there a way to override TZ via an env var or a php config file. Maybe I could try a before_script usage:

before_script:
  - echo "date.timezone = America/Los_Angeles" > /usr/local/etc/php/php.ini

But I have not tried that. I'd rather not hardset the TZ via php application code if that can be avoided.

I like the idea of mounting a custom volume to the docker container and updating the php tzdata.conf value for it, but for a Gitlab service container this is not possible (easily) on Gitlab 12.x

Any feedback is appreciated.

tenken commented 4 years ago

This works for me at the moment (setting the value in 1 place):

image:
  name: edbizarro/gitlab-ci-pipeline-php:7.2
# Nothing in this thread seemed to help to set it globally for the image
# https://gitlab.com/gitlab-org/gitlab-runner/issues/2692#note_50147081
#
#  entrypoint: ["/usr/local/bin/docker-php-entrypoint", "-a", "-d date.timezone=America/Los_Angeles"]

variables:
  GIT_SSL_NO_VERIFY: 1

before_script:
  - whoami
  # If our shell is not interactive or a login shell we may need to allow alias expansion.
  - shopt -s expand_aliases
  - alias php='php -d date.timezone=America/Los_Angeles'

# Cannot do below in any script because invalid permissions as
# /usr/local/etc/php/conf.d is owned by root and gitlab-runner runs php
# container with user "php".
#  - sudo echo "date.timezone = America/Los_Angeles" > /usr/local/etc/php/conf.d/tzdata.ini

# Run an example Test stage.
test:
  script:
    - set -x
    # List found aliases
    - alias
    # Locally setting -d date.timezone works here, but doing this per-line sucks.
    # - php -d date.timezone=America/Los_Angeles test.php
    - php test.php
    - date
    # Show a list of available shells for possible debugging.
    - cat /etc/shells
    # To debug the container add the following line that will keep the container alive forever.
    # - tail -f /dev/null