Skip to content

Instantly share code, notes, and snippets.

@SupaMic
Last active June 20, 2023 07:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SupaMic/af8c428d12af260d7cc723c65f29e2b4 to your computer and use it in GitHub Desktop.
Save SupaMic/af8c428d12af260d7cc723c65f29e2b4 to your computer and use it in GitHub Desktop.
Elixir (for Phoenix w/ Postgres) Deployment on Gitpod using ASDF and Dockerfile
#!/bin/bash
# These should be done in order and before mix deps.get and mix compile
# Verify postgres database credentials because if docker image starts `FROM gitpod/workspace-postgres`,
# then it seems to use `gitpod` as the default owner on the postgres DB setup
# Check if the user exists
user_exists=$(psql -U gitpod -c "SELECT usename FROM pg_user WHERE usename='postgres';" -t)
if [[ -z "$user_exists" ]]; then
echo "Creating postgres user to be compatible with default Elixir dev config..."
# Create the user
psql -U gitpod -c "CREATE USER postgres SUPERUSER;ALTER USER postgres PASSWORD 'postgres';"
psql -U gitpod -c "ALTER DATABASE postgres OWNER TO postgres;"
psql -U gitpod -c "GRANT ALL PRIVILEGES ON DATABASE postgres TO gitpod;"
echo "'postgres' user created successfully and made owner of 'postgres' db!"
else
echo "'postgres' user already exists."
fi;
# check if HEX v2.x.x installed via asdf, and if not install
# Run mix hex to see if exists, $MIX_HOME is set in .gitpod.ElixirDockerfile
echo "Checking Hex version in $MIX_HOME...";
if echo $(mix hex) | grep -q "Hex v2."; then
echo "Hex package manager is already installed."
else
echo "Hex package manager is not installed. Installing...";
mix local.hex --force;
if [ $? -eq 0 ]; then
echo "Hex package manager installed successfully."
else
echo "Failed to install Hex package manager."
fi;
fi;
# Determine is if Rebar3 was installed properly using asdf
mix_path=$(asdf which mix)
mix_directory=$(dirname "$mix_path")
mix_directory=${mix_directory%/bin} # Remove '/bin' from the end of the path
#Parsing to dermine the path of asdf install dir
elixir_version=$(elixir --version | grep -oE "Elixir [0-9]+\.[0-9]+\.[0-9]+" | awk '{print $2}')
major_version=$(echo "$elixir_version" | cut -d "." -f 1)
minor_version=$(echo "$elixir_version" | cut -d "." -f 2)
internal_mix_path="$mix_directory/.mix/elixir/$major_version-$minor_version"
rebar_bin_path="$internal_mix_path/rebar3"
rebar_info=$($rebar_bin_path -v)
if echo "$rebar_info" | grep -q "rebar 3."; then
echo "Rebar3 package manager is already installed."
else
echo "Rebar3 package manager is not installed. Installing..."
mix local.rebar --force
if [ $? -eq 0 ]; then
echo "Rebar3 package manager installed successfully."
else
echo "Failed to install Rebar3 package manager."
fi
fi
# the .gitpod.yml uses --if-missing flags for mix local.hex & mix local.rebar and makes these install checks redundant however,
# dynamically determining the Elixir version asdf path routes might be useful for other deployment scenarios like umbrellas
# This adds support for elixir-lsp.elixir-ls and victorbjorklund.phoenix extensions
# (in theory, but in practice the extensions themselves are somewhat unreliable in vscode-remote IDE)
# so in short, this configuration and extension setup *needs work*
json_file="/workspace/.vscode-remote/data/Machine/settings.json"
# Read the JSON file and add the new key-value pair
updated_json=$(jq '. + { "emmet.includeLanguages": { "phoenix-heex": "html", "html-eex": "html" } }' "$json_file")
# Write the updated JSON back to the file
echo "$updated_json" > "$json_file"
FROM gitpod/workspace-postgres
ENV DEBIAN_FRONTEND noninteractive
ENV ASDF_BRANCH v0.12.0
ENV ERLANG_V 25.2
ENV ELIXIR_V 1.14.2-otp-25
ENV KERL_BUILD_DOCS yes
USER root
# install dependencies here
RUN apt-get clean && apt-get update && apt-get -y install --no-install-recommends \
inotify-tools jq xsltproc libncurses-dev automake autoconf xsltproc fop \
&& rm -rf /var/lib/apt/lists/*
USER gitpod
SHELL ["/bin/bash", "-lc"]
RUN git config --global advice.detachedHead false
RUN git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch ${ASDF_BRANCH}
RUN sed -i '1s;^;source $HOME/.asdf/asdf.sh\nsource $HOME/.asdf/completions/asdf.bash\n;' ~/.bashrc
RUN asdf plugin add erlang https://github.com/asdf-vm/asdf-erlang.git && \
asdf plugin-add elixir https://github.com/asdf-vm/asdf-elixir.git && \
asdf install erlang ${ERLANG_V} && \
asdf install elixir ${ELIXIR_V}
RUN asdf global erlang ${ERLANG_V} && \
asdf global elixir ${ELIXIR_V}
ENV MIX_HOME "$HOME/.asdf/installs/elixir/${ELIXIR_V}/.mix"
image:
file: .gitpod.ElixirDockerfile
# List the start up tasks. Learn more https://www.gitpod.io/docs/config-start-tasks/
# --if-missing flag is only available for Elixir >= v1.13.0
tasks:
- name: phx
before: mix local.hex --if-missing --force && mix local.rebar --force --if-missing
init: bash .gitpod.elixir_init.sh && mix deps.get && mix compile
command: mix ecto.create && mix ecto.migrate && iex -S mix phx.server
# List the ports to expose. Learn more https://www.gitpod.io/docs/config-ports/
ports:
- port: 4000
onOpen: open-preview
- port: 5432
onOpen: ignore
vscode:
extensions:
- bradlc.vscode-tailwindcss
- elixir-lsp.elixir-ls
- victorbjorklund.phoenix
- benvp.vscode-hex-pm-intellisense
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment