Last active
November 2, 2018 13:57
-
-
Save popey/919281c32de605958eb0b362e22a3f2b to your computer and use it in GitHub Desktop.
Script to build an electron based app in lxd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Override that with the tag | |
# build-snap v1.16.2 # build specific tag | |
# Override with latest | |
# build-snap master # build git master | |
# Override with most recent release | |
# build-snap release # build latest stable release | |
# Set these variables | |
# Lower case name as it would appear in the store | |
SNAPNAME="mockman" | |
# Specify to master branch | |
GITURL=https://github.com/LanceGin/Mockman.git | |
# Correctly cased for the git project | |
APPNAME="Mockman" | |
# List of commands used to prepare the lxd container | |
PREP_COMMANDS="#!/bin/bash | |
echo '*** Installing core to get us latest snapd' | |
snap install core | |
echo '*** Installing node' | |
snap install --color=never --unicode=never node --classic --channel=10/stable | |
echo '*** Installing snapcraft' | |
snap install --color=never --unicode=never snapcraft --classic | |
echo '*** apt update' | |
apt update -y -qqq | |
echo '*** apt install build tools' | |
export DEBIAN_FRONTEND=noninteractive | |
apt install -y -qq -o Dpkg::Use-Pty=0 python build-essential > /dev/null | |
" | |
# List of commands used to ninja in snap support | |
PATCH_COMMANDS="#!/bin/bash | |
cd $APPNAME | |
sed -i -e 's/AppImage/snap/g' package.json | |
" | |
# Where does the snap file end up | |
OUTPUT_DIR="./build" | |
# List of commands used to build the application | |
BUILD_COMMANDS="#!/bin/bash | |
# Set dumb terminal to work around issue where snapcraft does progress indicators | |
export TERM=dumb | |
# Build the application | |
cd $APPNAME | |
# May need to install electron-builder if it's not already a devDependency | |
# npm install --save-dev electron-builder | |
# Here we build the application. Replace this with whatever commands | |
# are needed to build the app. Typically some node or yarn incantations | |
npm install | |
npm run build | |
# Once build we discover the name of the snap and put it in a file | |
# because lxc doesn't support wildcards and we don't know the name | |
# of the file ahead of time | |
find $OUTPUT_DIR -name '*.snap' > ~/snapfilename | |
" | |
# We begin | |
TMPDIR=$(mktemp -d) | |
DATESTAMP="$(date +%Y%m%d)-$(date +%H%M%S)" | |
CONTAINER=$SNAPNAME-$DATESTAMP | |
PREPSCRIPT=$APPNAME-prep.sh | |
PATCHSCRIPT=$APPNAME-patch.sh | |
BUILDSCRIPT=$APPNAME-build.sh | |
WAITFORSNAPD=5 | |
display_container () { | |
echo "Container: $CONTAINER" | |
} | |
## Get the source | |
CWD=$(pwd) | |
## Clone the repo | |
echo "*** Cloning: " $GITURL | |
git clone -q $GITURL $TMPDIR/$APPNAME | |
cd $TMPDIR/$APPNAME | |
git fetch -q | |
last_committed_tag="$(git describe --tags --abbrev=0)" | |
if [ "$1" = "master" ]; then | |
echo "*** Using master branch" | |
elif [ "$1" = "release" ]; then | |
echo "*** Checking out ${last_committed_tag}" | |
git checkout -q "${last_committed_tag}" | |
elif [ "$1" != "" ]; then | |
echo "*** Using $1 tagged release" | |
git checkout -q "$i" | |
fi | |
echo "*** Building: " $(git describe --tags) | |
cd $CWD | |
## Launch container | |
echo "*** Launch lxc container" | |
lxc launch -q ubuntu:16.04 $CONTAINER | |
if [ $? -ne 0 ]; | |
then | |
echo "Failed to launch container" | |
exit 1 | |
fi | |
## Push source into container | |
lxc file push -q -r $TMPDIR/$APPNAME $CONTAINER/root/ | |
if [ $? -ne 0 ]; | |
then | |
echo "Failed to push source to container" | |
exit 1 | |
fi | |
## Build prep script | |
echo "$PREP_COMMANDS" >> $TMPDIR/$PREPSCRIPT | |
## Make script executable | |
chmod +x $TMPDIR/$PREPSCRIPT | |
## Push script to container | |
lxc file push -q $TMPDIR/$PREPSCRIPT $CONTAINER/root/ | |
if [ $? -ne 0 ]; | |
then | |
echo "Failed to push to container" | |
exit 1 | |
fi | |
## Sleep because we're too quick to use snapd, so the prep will fail | |
sleep $WAITFORSNAPD | |
## Run prep script | |
echo "*** Run prep script in container" | |
/snap/bin/lxc exec -q $CONTAINER -- /root/$PREPSCRIPT | |
if [ $? -ne 0 ]; | |
then | |
echo "Failed to run prep script" | |
exit 1 | |
fi | |
## Build patch script | |
echo "$PATCH_COMMANDS" >> $TMPDIR/$PATCHSCRIPT | |
## Make script executable | |
chmod +x $TMPDIR/$PATCHSCRIPT | |
## Push script to container | |
lxc file push -q $TMPDIR/$PATCHSCRIPT $CONTAINER/root/ | |
if [ $? -ne 0 ]; | |
then | |
echo "Failed to push patch script to container" | |
exit 1 | |
fi | |
## Run patch script | |
echo "*** Run patch script in container" | |
/snap/bin/lxc exec -q $CONTAINER -- /root/$PATCHSCRIPT | |
if [ $? -ne 0 ]; | |
then | |
echo "Failed to run patch script" | |
exit 1 | |
fi | |
## Create the script used inside the container to build the application | |
echo "*** Construct the build script" | |
echo "$BUILD_COMMANDS" >> $TMPDIR/$BUILDSCRIPT | |
## Make build script executable | |
chmod +x $TMPDIR/$BUILDSCRIPT | |
## Show the build script | |
echo "*** Here's the build script we'll run" | |
cat $TMPDIR/$BUILDSCRIPT | |
## Push script to container | |
echo "*** Push build script to container" | |
lxc file push -q $TMPDIR/$BUILDSCRIPT $CONTAINER/root/ | |
if [ $? -ne 0 ]; | |
then | |
echo "Failed to push to container" | |
display_container | |
exit 1 | |
fi | |
## Run build script | |
echo "*** Run build script in container" | |
/snap/bin/lxc exec -q $CONTAINER -- /root/$BUILDSCRIPT $APPNAME | |
if [ $? -ne 0 ]; | |
then | |
echo "Failed to run build script" | |
display_container | |
exit 1 | |
fi | |
echo "*** Pull snap from container" | |
lxc file pull -q $CONTAINER/root/snapfilename $TMPDIR/snapfilename | |
if [ $? -ne 0 ]; | |
then | |
echo "Failed to get snapfilename" | |
display_container | |
exit 1 | |
fi | |
FILENAME=$(cat $TMPDIR/snapfilename) | |
lxc file pull -q $CONTAINER/root/$APPNAME/$FILENAME . | |
if [ $? -ne 0 ]; | |
then | |
echo "Failed to get snap" | |
display_container | |
exit 1 | |
fi | |
echo "*** Shutdown container" | |
lxc stop -q $CONTAINER | |
if [ $? -ne 0 ]; | |
then | |
echo "Failed to stop container" | |
display_container | |
exit 1 | |
fi | |
echo "*** Delete temp folder" | |
rm -rf $TMPDIR | |
if [ $? -ne 0 ]; | |
then | |
echo "Failed to remove $TMPDIR" | |
display_container | |
exit 1 | |
fi | |
echo "*** Destroy container" | |
lxc delete -q $CONTAINER | |
if [ $? -ne 0 ]; | |
then | |
echo "Failed to delete container" | |
display_container | |
exit 1 | |
fi | |
echo "*** Snap built" | |
echo $(basename $FILENAME) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment