Skip to content

Instantly share code, notes, and snippets.

@evandandrea
Last active January 23, 2021 14:56
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save evandandrea/c754964bfdfb176844f26f605ebbb8db to your computer and use it in GitHub Desktop.
Save evandandrea/c754964bfdfb176844f26f605ebbb8db to your computer and use it in GitHub Desktop.
Automatically publish to the snap store from Travis
#!/bin/sh -e
if [ -z "$SNAPCRAFT_SECRET" ]; then
# Run `sh seed.sh` on your local machine so SNAPCRAFT_SECRET is set.
exit 0
fi
mkdir -p ".encrypted"
if [ ! -e ".encrypted/snapcraft.cfg.enc" ]; then
echo "Seeding a new macaroon."
echo "$SNAPCRAFT_CONFIG" > ".encrypted/snapcraft.cfg.enc"
fi
mkdir -p "$HOME/.config/snapcraft"
# Decrypt the macaroon (secret).
openssl enc -aes-256-cbc -base64 -pass env:SNAPCRAFT_SECRET -d -in ".encrypted/snapcraft.cfg.enc" -out "$HOME/.config/snapcraft/snapcraft.cfg"
if docker run -v $HOME:/root -v $(pwd):/cwd snapcore/snapcraft sh -c 'cd /cwd; snapcraft'; then
docker run -v $HOME:/root -v $(pwd):/cwd snapcore/snapcraft sh -c "cd /cwd; snapcraft push *.snap --release edge"
fi
# The macaroon (secret) has been refreshed; re-encrypt it.
openssl enc -aes-256-cbc -base64 -pass env:SNAPCRAFT_SECRET -out ".encrypted/snapcraft.cfg.enc" < "$HOME/.config/snapcraft/snapcraft.cfg"
rm -f "$HOME/.config/snapcraft/snapcraft.cfg"
sudo: required
dist: trusty
services:
- docker
cache:
directories:
- .encrypted
script:
- ./.travis.sh
#!/bin/sh
# 1. Get a macaroon (secret) from the Store for publishing snaps
# 2. Create a private key that only Travis can decrypt
# 3. Then use it to encrypt the macaroon
#
# As encrypted variables are only available to Travis commit runs,
# pull requests won't be able to steal the macaroon and publish
# under your name.
snapcraft login
export SNAPCRAFT_SECRET=$(pwgen 20 -1)
export SNAPCRAFT_CONFIG="$(openssl enc -aes-256-cbc -base64 -pass env:SNAPCRAFT_SECRET < ~/.config/snapcraft/snapcraft.cfg)"
travis encrypt SNAPCRAFT_SECRET=$SNAPCRAFT_SECRET -a
travis env set SNAPCRAFT_CONFIG "$SNAPCRAFT_CONFIG"
# Don't forget to commit the changes back to your .travis.yml.
@come-maiz
Copy link

@evandandrea
Copy link
Author

@ElOpio why?

@come-maiz
Copy link

@evandandrea, because on line 14 you are already decrypting it. Unless I'm misunderstanding it, this step in 24 is not doing anything useful.

@evandandrea
Copy link
Author

@ElOpio line 14 decrypts, line 24 re-encrypts (because it's a refreshed macaroon).

@come-maiz
Copy link

I see. A comment in the shell script would be nice.

@evandandrea
Copy link
Author

Good thinking, @ElOpio. Fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment