Skip to content

Instantly share code, notes, and snippets.

@maikelthedev
Last active September 29, 2024 10:43
Show Gist options
  • Save maikelthedev/f4974c85d144a20ca2c5e1e8bb4b7595 to your computer and use it in GitHub Desktop.
Save maikelthedev/f4974c85d144a20ca2c5e1e8bb4b7595 to your computer and use it in GitHub Desktop.
Standard Makefile for a Phoenix project with --no-ecto and same version in FISH shell script
#!/usr/bin/env fish
# Variables
set -x MIX_ENV prod
set -x APP_NAME experiment
set -x BUILD_DIR _build
set -x SERVER_USER ubuntu
set -x SECRET_KEY_BASE (mix phx.gen.secret)
set -x ENV_FILE .env
set -x RELEASE_DIR $BUILD_DIR/$MIX_ENV/rel/$APP_NAME
set -x PHX_HOST "maikeladas.es"
set -x PORT 4001
# Function to generate and export SECRET_KEY_BASE
function secret
echo "==> Generating and exporting SECRET_KEY_BASE"
echo "SECRET_KEY_BASE=$SECRET_KEY_BASE" > $ENV_FILE
end
# Function to install dependencies
function setup
echo "==> Installing dependencies"
mix deps.get --only $MIX_ENV
end
# Function to build the project
function build
echo "==> Building the project"
env MIX_ENV=$MIX_ENV mix compile
echo "==> Building assets"
env MIX_ENV=$MIX_ENV mix assets.deploy
end
# Function to create a release
function release
echo "==> Creating release"
env MIX_ENV=$MIX_ENV mix phx.gen.release
env MIX_ENV=$MIX_ENV mix release
end
# Function to clean build artifacts
function clean
echo "==> Cleaning build artifacts"
rm -rf $BUILD_DIR
end
# Function to run the server
function run
echo "==> Running the server with loaded environment variables"
if test -f $ENV_FILE
source $ENV_FILE
env PHX_HOST=$PHX_HOST PORT=$PORT $RELEASE_DIR/bin/$APP_NAME start
else
echo "No .env file found! Please run './script.fish setup' first."
end
end
# Function to deploy to production
function deploy
echo "==> Deploying to production"
ssh $SERVER_USER@prod "cd code/maikeladas && git pull && ./script.fish build && $BUILD_DIR/$MIX_ENV/rel/$APP_NAME/bin/$APP_NAME restart"
end
# Main function to run all steps by default
function all
setup
secret
build
release
end
# Call the function passed as an argument, or run 'all' by default
if test (count $argv) -gt 0
eval $argv
else
all
end
# Variables
MIX_ENV=prod
APP_NAME=experiment
BUILD_DIR=_build
SERVER_USER=ubuntu
SECRET_KEY_BASE=$(shell mix phx.gen.secret)
ENV_FILE=.env
RELEASE_DIR=$(BUILD_DIR)/$(MIX_ENV)/rel/$(APP_NAME)
PHX_HOST="maikeladas.es"
PORT=4001
.PHONY: all secret setup build release clean run deploy
# Default task
all: setup secret build release
# Generate secret
secret:
@echo "==> Generating and exporting SECRET_KEY_BASE"
@echo "SECRET_KEY_BASE=$(SECRET_KEY_BASE)" > .env
# Install necessary dependencies
setup:
@echo "==> Installing dependencies"
mix deps.get --only $(MIX_ENV)
# Build and compile the project
build:
@echo "==> Building the project"
MIX_ENV=$(MIX_ENV) mix compile
@echo "==> Building assets"
MIX_ENV=$(MIX_ENV) mix assets.deploy
# Create a release
release:
@echo "==> Creating release"
MIX_ENV=$(MIX_ENV) mix phx.gen.release
MIX_ENV=$(MIX_ENV) mix release
# Run the server with environment variables from .env
run:
@echo "==> Running the server with loaded environment variables"
@if [ -f $(ENV_FILE) ]; then \
export $$(cat $(ENV_FILE) | xargs) && \
PHX_HOST=$(PHX_HOST) PORT=$(PORT) $(RELEASE_DIR)/bin/server start; \
else \
echo "No .env file found! Please run 'make setup' first."; \
fi
# Deploy the build to production
deploy:
@echo "==> Deploying to production"
ssh prod 'cd code/maikeladas && git pull && make && _build/prod/rel/experiment/bin/experiment restart'
# Clean build artifacts
clean:
@echo "==> Cleaning build artifacts"
rm -rf $(BUILD_DIR)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment