1+ #! /bin/bash
2+ #
3+ # SPDX-License-Identifier: MIT
4+ # Copyright © 2021 Apolo Pena
5+ #
6+ # before-tasks.sh
7+ # Description:
8+ # Tasks that should be run every time the workspace is created or started.
9+ #
10+ # Notes:
11+ # Gitpod currently does not persist files in the home directory so we must write them
12+ # in everytime the workspace starts. This is done in the 'before' task in .gitpod.yml
13+
14+ # BEGIN: Enable GPG key to sign Git commits.
15+ # Error handling for improper use of GPG environment variables
16+ err_msg_prefix1=" A GPG_KEY was found but it's corresponding GPG_KEY_ID was not."
17+ err_msg_prefix2=" A GPG_KEY_ID was found but it's corresponding GPG_KEY was not."
18+ err_msg_suffix=" Git commits will not be signed."
19+ [[ -n $GPG_KEY && -z $GPG_KEY_ID ]] &&
20+ echo " ERROR: $err_msg_prefix1 $err_msg_suffix "
21+ [[ -n $GPG_KEY_ID && -z $GPG_KEY ]] &&
22+ echo " ERROR: $err_msg_prefix2 $err_msg_suffix "
23+ # Main GPG key logic
24+ if [[ -n $GPG_KEY && -n $GPG_KEY_ID ]]; then
25+ gpg_conf_path=~ /.gnupg/gpg.conf
26+ msg=" Enabling Git commit signing for GPG key id: $GPG_KEY_ID "
27+ gpg -q --batch --import <( echo " $GPG_KEY " | base64 -d) &&
28+ echo ' pinentry-mode loopback' >> " $gpg_conf_path " &&
29+ git config --global user.signingkey " $GPG_KEY_ID " &&
30+ git config commit.gpgsign true
31+ ec=$?
32+ if [[ $ec -eq 0 ]]; then
33+ echo " SUCCESS: $msg "
34+ # Change the git email if the user needs it (ensures the commit is marked as 'Verified')
35+ if [[ -n $GPG_MATCH_GIT_TO_EMAIL ]]; then
36+ msg=" Setting user.email in ~/.gitconfig to $GPG_MATCH_GIT_TO_EMAIL "
37+ if git config --global user.email " $GPG_MATCH_GIT_TO_EMAIL " ; then
38+ echo " SUCCESS: $msg "
39+ else
40+ echo " ERROR: $msg "
41+ fi
42+ fi
43+ # Ultimately trust the key, bump to lowercase and check the value of the directive
44+ if [[ $( echo " $GPG_AUTO_ULTIMATE_TRUST " | tr ' [:upper:]' ' [:lower:]' ) == yes ]]; then
45+ msg=" Automagically giving ultimate trust to GPG_KEY_ID: $GPG_KEY_ID "
46+ # Prepend the key id as a trusted hex and update the local database with a silent arbitrary gpg call
47+ echo -e " " trusted-key 0x" $GPG_KEY_ID " " \n$( cat $gpg_conf_path ) " > " $gpg_conf_path " &&
48+ gpg --list-keys & > /dev/null
49+ ec=$?
50+ if [[ $ec -eq 0 ]]; then
51+ echo " SUCCESS: $msg "
52+ else
53+ echo " ERROR: $msg "
54+ fi
55+ fi
56+ else
57+ echo " ERROR: $msg "
58+ fi
59+ fi
60+ # END: Enable GPG key to sign Git commits.
61+
62+ # Auto activate intelephense if license key is available
63+ if [[ -n $INTELEPHENSE_LICENSEKEY ]]; then
64+ msg=" creating $HOME /intelephense/licence.txt"
65+ echo " INTELEPHENSE_LICENSEKEY environment variable found, $msg "
66+ mkdir -p " $HOME /intelephense" &&
67+ echo " $INTELEPHENSE_LICENSEKEY " > " $HOME /intelephense/licence.txt" &&
68+ ec=$?
69+ if [[ $ec -eq 0 ]]; then
70+ echo " SUCCESS: $msg "
71+ else
72+ echo " ERROR: $msg "
73+ fi
74+ fi
0 commit comments