Skip to content

Commit

Permalink
Add a script to push assets on github (monicahq#1544)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored Jul 7, 2018
1 parent f2f865d commit 8be306f
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 13 deletions.
15 changes: 3 additions & 12 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -183,19 +183,10 @@ jobs:
name: Upload yarn lockfile
command: CIRCLE_PREVIOUS_BUILD_NUM=$(test "$CIRCLE_BRANCH" != "master" -a "greenkeeperio-bot" = "`git log --format="%an" -n 1`" || echo false) $(yarn global bin)/greenkeeper-lockfile-upload

# Build js and css assets
# Update js and css assets eventually
- run:
name: Build assets
command: |
php artisan lang:generate
yarn run production
- persist_to_workspace:
root: *root
paths:
- public/mix-manifest.json
- public/css
- public/fonts
- public/js
name: Update assets
command: scripts/ci/update-assets.sh

tests-7.2:
<<: *defaults
Expand Down
Binary file modified .circleci/config.yml.sig
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ npm-debug.log*
/results
.deploy.json
yarn-error.log
php-extensions-*.tar.bz2
3 changes: 2 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
UNRELEASED CHANGES:

* Add U2F/yubikey support and refactor MultiFactor Authentication
* Add a script to update assets automatically
*

RELEASED VERSIONS:

Expand All @@ -20,7 +22,6 @@ v2.3.0 - 2018-06-13
* Fix resetting account not working
* Fix CSV import that can break if dates have the wrong format
* Add default accounts email confirmation in setup:test
* Fix CSV import that can break if dates have the wrong format
* Set the default tooltip delay to 0 so the tooltip does not stay displayed for 200ms by default
* Replace queries with hardcoded "monica" database name to use the current default connection database
* Set the default_avatar_color property before saving a contact model.
Expand Down
80 changes: 80 additions & 0 deletions scripts/ci/update-assets.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/bin/bash

if [ "$CIRCLECI" == "true" ]; then
if [[ ! -z $CIRCLE_PULL_REQUEST ]] ; then export CIRCLE_PR_NUMBER="${CIRCLE_PR_NUMBER:-${CIRCLE_PULL_REQUEST##*/}}" ; fi
REPO=$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME
BRANCH=${CIRCLE_BRANCH:-$CIRCLE_TAG}
PR_NUMBER=${CIRCLE_PR_NUMBER:-false}
fi

REPOSITORY_OWNER=monicahq/monica

set -euo pipefail

# Update assets
echo -e "\033[1;32m# Build assets ...\033[0:37m"
echo -e "\033[1;36mphp artisan lang:generate\033[0:37m"
php artisan lang:generate
echo ""
echo -e "\033[1;36myarn run production\033[0:37m"
yarn run production
echo ""

# Check if there is zero update needed
status=$(git status --porcelain)
if [ "$status" = "" ]; then
echo "Nothing to push, already up to date."
exit 0;
fi

# Add files
git add public/mix-manifest.json
git add public/js/*
git add public/css/*
git add public/fonts/*

# Commit
if [ -z "${ASSETS_USERNAME:-}" ]; then
#No username
exit 0
fi
git config user.email $ASSETS_EMAIL
git config user.name $ASSETS_USERNAME
git commit -m "chore(assets): Update assets"

# Push
if [ "$BRANCH" == "master" ] && [ "$PR_NUMBER" == "false" ]; then
echo -e "\033[0;31mmaster is not up to date, but we can't update it directly...\033[0:37m"
exit 0

elif [ -n "${ASSETS_GITHUB_TOKEN:-}" ]; then
REPOS_VALUES=($(curl -H "Authorization: token $ASSETS_GITHUB_TOKEN" -sSL https://api.github.com/repos/$REPO/pulls/$PR_NUMBER | jq -r -c ".head.repo.full_name, .head.ref"))

PULL_REQUEST_BRANCH=
PULL_REQUEST_REPOSITORY=${REPOS_VALUES[0]}
PULL_REQUEST_HEADBRANCH=${REPOS_VALUES[1]}

if [ -z "${PULL_REQUEST_REPOSITORY:-}" ] || [ "$PULL_REQUEST_REPOSITORY" == "null" ]; then
echo -e "\033[0;31mError with github api call\033[0:37m"
exit 1
elif [ "$PULL_REQUEST_REPOSITORY" == "$REPOSITORY_OWNER" ]; then
PULL_REQUEST_BRANCH=$PULL_REQUEST_HEADBRANCH
else
echo -e "\033[0;31mMonica asset are not up to date.\033[0:37m"
echo "We can't commit in $PULL_REQUEST_REPOSITORY to update them directly."
echo "Please update the Monica assets yourself by running:"
echo " ~ php artisan lang:generate"
echo " ~ yarn run production"
exit 2
fi

echo "Pushing files to $PULL_REQUEST_BRANCH branch ..."
remote="https://$ASSETS_USERNAME:$ASSETS_GITHUB_TOKEN@github.com/$REPO"
git remote add gk-origin $remote
git push gk-origin HEAD:$PULL_REQUEST_BRANCH

# Exit with error to stop the current build
echo "...pushed files successfully."
echo "Exit with error to stop the current build."
exit -1
fi

0 comments on commit 8be306f

Please sign in to comment.