added deploy action
This commit is contained in:
@@ -0,0 +1,147 @@
|
||||
name: Deploy Environment (reusable)
|
||||
|
||||
on:
|
||||
workflow_call:
|
||||
inputs:
|
||||
environment:
|
||||
description: "Nome ambiente (test, prod, development, ...)"
|
||||
required: true
|
||||
type: string
|
||||
host-var:
|
||||
description: "Nome della variabile org che contiene l'host target (es. TEST_HOST, PROD_HOST)"
|
||||
required: true
|
||||
type: string
|
||||
compose-opts:
|
||||
description: "Opzioni aggiuntive per docker compose up (es. --force-recreate)"
|
||||
required: false
|
||||
type: string
|
||||
default: ""
|
||||
pre-deploy-cmd:
|
||||
description: "Comando da eseguire sul server prima del deploy (es. backup)"
|
||||
required: false
|
||||
type: string
|
||||
default: ""
|
||||
post-deploy-cmd:
|
||||
description: "Comando da eseguire sul server dopo il deploy (es. reload apache)"
|
||||
required: false
|
||||
type: string
|
||||
default: ""
|
||||
|
||||
jobs:
|
||||
deploy:
|
||||
runs-on: ubuntu-latest
|
||||
env:
|
||||
ENV_NAME: ${{ inputs.environment }}
|
||||
DEPLOY_USER: ${{ vars.DEPLOY_USER }}
|
||||
DEPLOY_BASE_PATH: ${{ vars.DEPLOY_BASE_PATH }}
|
||||
TARGET_HOST: ${{ vars[inputs.host-var] }}
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
|
||||
- name: Verifica configurazione
|
||||
run: |
|
||||
missing=0
|
||||
for v in DEPLOY_USER DEPLOY_BASE_PATH; do
|
||||
eval "val=\${$v}"
|
||||
if [ -z "$val" ]; then echo "MANCANTE: $v"; missing=1; else echo "OK: $v"; fi
|
||||
done
|
||||
if [ -z "${{ vars[inputs.host-var] }}" ]; then
|
||||
echo "MANCANTE: variabile org '${{ inputs.host-var }}'"
|
||||
missing=1
|
||||
else
|
||||
echo "OK: ${{ inputs.host-var }} = ${{ vars[inputs.host-var] }}"
|
||||
fi
|
||||
[ "$missing" -eq 0 ] || { echo "Imposta le Variables mancanti a livello org."; exit 1; }
|
||||
|
||||
- name: Verifica file ambiente
|
||||
run: |
|
||||
COMPOSE="docker-compose-${ENV_NAME}.yml"
|
||||
ENV_FILE=".env_${ENV_NAME}"
|
||||
|
||||
if [ ! -f "$COMPOSE" ]; then
|
||||
echo "ERRORE: $COMPOSE non trovato nel repo"
|
||||
exit 1
|
||||
fi
|
||||
echo "Compose: $COMPOSE"
|
||||
|
||||
if [ -f "$ENV_FILE" ]; then
|
||||
echo "Env file: $ENV_FILE"
|
||||
else
|
||||
echo "WARNING: $ENV_FILE non trovato, il deploy procede senza env file"
|
||||
fi
|
||||
|
||||
# Cerca anche eventuali file apache
|
||||
APACHE="apache-${ENV_NAME}.conf"
|
||||
if [ -f "$APACHE" ]; then
|
||||
echo "Apache config: $APACHE"
|
||||
fi
|
||||
|
||||
- name: Setup SSH
|
||||
run: |
|
||||
mkdir -p ~/.ssh
|
||||
echo "${{ secrets.DEPLOY_SSH_KEY }}" > ~/.ssh/id_ed25519
|
||||
chmod 600 ~/.ssh/id_ed25519
|
||||
ssh-keyscan -H "${{ vars[inputs.host-var] }}" >> ~/.ssh/known_hosts 2>/dev/null
|
||||
|
||||
- name: Pre-deploy
|
||||
if: ${{ inputs.pre-deploy-cmd != '' }}
|
||||
run: |
|
||||
ssh ${DEPLOY_USER}@${{ vars[inputs.host-var] }} "export GPG_TTY=$(tty)"
|
||||
REMOTE_PATH="${DEPLOY_BASE_PATH}/${{ github.repository_owner }}/${{ github.event.repository.name }}"
|
||||
ssh ${DEPLOY_USER}@${{ vars[inputs.host-var] }} "a2dissite ${{ github.event.repository.name }}.site-ssl -q"
|
||||
ssh ${DEPLOY_USER}@${{ vars[inputs.host-var] }} "service apache2 reload"
|
||||
[ -f "$APACHE" ] && ssh ${DEPLOY_USER}@${{ vars[inputs.host-var] }} "cp -v /etc/apache2/sites-available/${{ github.event.repository.name }}.site-ssl.conf /etc/apache2/sites-available/${{ github.event.repository.name }}.site-ssl.conf_$(date +%s)"
|
||||
[ -f "$APACHE" ] && ssh ${DEPLOY_USER}@${{ vars[inputs.host-var] }} "cp -v ${REMOTE_PATH}/apache-${ENV_NAME}.conf /etc/apache2/sites-available/${{ github.event.repository.name }}.site-ssl.conf"
|
||||
ssh ${DEPLOY_USER}@${{ vars[inputs.host-var] }} "${{ inputs.pre-deploy-cmd }}"
|
||||
|
||||
- name: Docker login sul server
|
||||
run: |
|
||||
HOST="${{ vars[inputs.host-var] }}"
|
||||
ssh ${DEPLOY_USER}@${HOST} \
|
||||
"echo '${{ secrets.MAVEN_PASSWORD }}' | docker login ${{ vars.DOCKER_REGISTRY }} -u '${{ secrets.MAVEN_USERNAME }}' --password-stdin"
|
||||
|
||||
- name: Deploy
|
||||
run: |
|
||||
HOST="${{ vars[inputs.host-var] }}"
|
||||
REMOTE_PATH="${DEPLOY_BASE_PATH}/${{ github.repository_owner }}/${{ github.event.repository.name }}"
|
||||
COMPOSE="docker-compose-${ENV_NAME}.yml"
|
||||
ENV_FILE=".env_${ENV_NAME}"
|
||||
APACHE="apache-${ENV_NAME}.conf"
|
||||
|
||||
# Crea directory remota
|
||||
ssh ${DEPLOY_USER}@${HOST} "mkdir -p ${REMOTE_PATH}"
|
||||
|
||||
# Copia file
|
||||
FILES_TO_COPY="${COMPOSE}"
|
||||
[ -f "$ENV_FILE" ] && FILES_TO_COPY="${FILES_TO_COPY} ${ENV_FILE}"
|
||||
[ -f "$APACHE" ] && FILES_TO_COPY="${FILES_TO_COPY} ${APACHE}"
|
||||
|
||||
scp ${FILES_TO_COPY} ${DEPLOY_USER}@${HOST}:${REMOTE_PATH}/
|
||||
|
||||
# Componi il comando docker compose
|
||||
COMPOSE_CMD="cd ${REMOTE_PATH} && docker compose -f ${COMPOSE}"
|
||||
[ -f "$ENV_FILE" ] && COMPOSE_CMD="${COMPOSE_CMD} --env-file ${ENV_FILE}"
|
||||
COMPOSE_CMD="${COMPOSE_CMD} pull && ${COMPOSE_CMD} up -d ${{ inputs.compose-opts }}"
|
||||
|
||||
ssh ${DEPLOY_USER}@${HOST} "${COMPOSE_CMD}"
|
||||
|
||||
- name: Post-deploy
|
||||
if: ${{ inputs.post-deploy-cmd != '' }}
|
||||
run: |
|
||||
ssh ${DEPLOY_USER}@${{ vars[inputs.host-var] }} "a2ensite ${{ github.event.repository.name }}.site-ssl -q"
|
||||
ssh ${DEPLOY_USER}@${{ vars[inputs.host-var] }} "service apache2 reload"
|
||||
ssh ${DEPLOY_USER}@${{ vars[inputs.host-var] }} "docker image prune -a -f"
|
||||
ssh ${DEPLOY_USER}@${{ vars[inputs.host-var] }} "${{ inputs.post-deploy-cmd }}"
|
||||
|
||||
- name: Verifica container
|
||||
run: |
|
||||
HOST="${{ vars[inputs.host-var] }}"
|
||||
REMOTE_PATH="${DEPLOY_BASE_PATH}/${{ github.repository_owner }}/${{ github.event.repository.name }}"
|
||||
COMPOSE="docker-compose-${ENV_NAME}.yml"
|
||||
ENV_FILE=".env_${ENV_NAME}"
|
||||
|
||||
COMPOSE_CMD="cd ${REMOTE_PATH} && docker compose -f ${COMPOSE}"
|
||||
[ -f "$ENV_FILE" ] && COMPOSE_CMD="${COMPOSE_CMD} --env-file ${ENV_FILE}"
|
||||
|
||||
echo "── Container status ──"
|
||||
ssh ${DEPLOY_USER}@${HOST} "${COMPOSE_CMD} ps"
|
||||
Reference in New Issue
Block a user