Skip to content

Commit

Permalink
Add version info from env variables (#41)
Browse files Browse the repository at this point in the history
Override from ENV variables:

  - site.name (SSPKS_SITE_NAME)
  - site.theme (SSPKS_SITE_THEME)
  - site.redirectindex (SSPKS_SITE_REDIRECTINDEX)
  
And please Scrutinizer.
  • Loading branch information
jdel authored Aug 15, 2017
1 parent 842060e commit a5ad86e
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 5 deletions.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ FROM alpine:3.5
MAINTAINER Julien Del-Piccolo <julien@del-piccolo.com>
ARG BRANCH="master"
ARG COMMIT=""
ENV SSPKS_BRANCH=${BRANCH}
ENV SSPKS_COMMIT=${COMMIT}
LABEL branch=${BRANCH}
LABEL commit=${COMMIT}

Expand All @@ -25,6 +27,7 @@ RUN echo "BRANCH: ${BRANCH}" \
&& rm -rf /var/cache/apk/* \
&& mkdir /run/apache2 \
&& sed -i 's/Listen 80/Listen 8080/' /etc/apache2/httpd.conf \
&& sed -i 's/^variables_order = "GPCS"/variables_order = "EGPCS"/' /etc/php5/php.ini \
&& ln -sf /dev/stdout /var/log/apache2/access.log \
&& ln -sf /dev/stderr /var/log/apache2/error.log

Expand Down
6 changes: 1 addition & 5 deletions hooks/build
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,4 @@ echo "DOCKER_REPO: ${DOCKER_REPO}"
echo "DOCKER_TAG: ${DOCKER_TAG}"
echo "IMAGE_NAME: ${IMAGE_NAME}"

if [ -z "${DOCKER_TAG}" ]; then
docker build --build-arg=COMMIT=$(git rev-parse --short HEAD) --build-arg=BRANCH=${SOURCE_BRANCH} -t $IMAGE_NAME .
else
docker build --build-arg=COMMIT=$(git rev-parse --short HEAD) --build-arg=BRANCH=${DOCKER_TAG} -t $IMAGE_NAME .
fi
docker build --build-arg=COMMIT=$(git rev-parse --short HEAD) --build-arg=BRANCH=${SOURCE_BRANCH} -t $IMAGE_NAME .
38 changes: 38 additions & 0 deletions lib/SSpkS/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* @property string basePath Path to site root (where index.php is located)
* @property string baseUrl URL to site root (where index.php is located)
* @property string baseUrlRelative Relative URL to site root (without scheme or hostname)
* @property string SSPKS_COMMIT current commit hash taken from ENV variables
* @property string SSPKS_BRANCH current branch taken from ENV variables
*/
class Config implements \Iterator
{
Expand All @@ -39,10 +41,46 @@ public function __construct($basePath, $cfgFile = 'conf/sspks.yaml')
} catch (ParseException $e) {
throw new \Exception($e->getMessage());
}

/** Init variables that are not actual config variables */
$config['SSPKS_COMMIT'] = '';
$config['SSPKS_BRANCH'] = '';

/** Override config values with environment variables if present */
if ($this->envVarIsNotEmpty('SSPKS_COMMIT')) {
$config['SSPKS_COMMIT'] = $_ENV['SSPKS_COMMIT'];
}

if ($this->envVarIsNotEmpty('SSPKS_BRANCH')) {
$config['SSPKS_BRANCH'] = $_ENV['SSPKS_BRANCH'];
}

if ($this->envVarIsNotEmpty('SSPKS_SITE_NAME')) {
$config['site']['name'] = $_ENV['SSPKS_SITE_NAME'];
}

if ($this->envVarIsNotEmpty('SSPKS_SITE_THEME')) {
$config['site']['theme'] = $_ENV['SSPKS_SITE_THEME'];
}

if ($this->envVarIsNotEmpty('SSPKS_SITE_REDIRECTINDEX')) {
$config['site']['redirectindex'] = $_ENV['SSPKS_SITE_REDIRECTINDEX'];
}

$this->config = $config;
$this->config['basePath'] = $this->basePath;
}

/**
* Checks wether an env variable exists and is not an empty string.
*
* @param string $name Name of requested environment variable.
* @return boolean value.
*/
public function envVarIsNotEmpty($name)
{
return (array_key_exists($name, $_ENV) && $_ENV[$name]);
}

/**
* Getter magic method.
Expand Down
9 changes: 9 additions & 0 deletions lib/SSpkS/Output/HtmlOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,22 @@ public function __construct(\SSpkS\Config $config)
$this->setVariable('baseUrlRelative', $this->config->baseUrlRelative);
$this->setVariable('themeUrl', $this->config->baseUrlRelative . $this->config->paths['themes'] . $this->config->site['theme'] . '/');
$this->setVariable('requestUri', $_SERVER['REQUEST_URI']);
$this->setVariable('commitHash', $this->config->SSPKS_COMMIT);
$this->setVariable('branch', $this->config->SSPKS_BRANCH);
}

/**
* @param string $name
* @param mixed $value
*/
public function setVariable($name, $value)
{
$this->tplVars[$name] = $value;
}

/**
* @param string $tplName
*/
public function setTemplate($tplName)
{
$this->template = $tplName;
Expand Down
12 changes: 12 additions & 0 deletions themes/material/templates/partials/html_tail.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@
<li><a href="https://github.com/jdel/sspks">Source on GitHub</a></li>
</ul>
</div>
{{# branch }}
<div class="mdl-mini-footer--right-section">
<div class="mdl-logo">Version</div>
<ul class="mdl-mini-footer--link-list">
<li><a href="https://github.com/jdel/sspks/tree/{{ branch }}">{{ branch }}</a></li>
{{# commitHash }}
<li><a href="https://github.com/jdel/sspks/commit/{{ commitHash }}">{{ commitHash }}</a></li>
{{/ commitHash }}
</ul>
</div>
</div>
{{/ branch }}
</footer>
</main>
</div>
Expand Down

0 comments on commit a5ad86e

Please sign in to comment.