diff --git a/.gitea/actions/deploy-ssh/action.yml b/.gitea/actions/deploy-ssh/action.yml index f257c91..9fe4c7e 100644 --- a/.gitea/actions/deploy-ssh/action.yml +++ b/.gitea/actions/deploy-ssh/action.yml @@ -10,6 +10,17 @@ inputs: host: description: SSH hostname or IP required: true + port: + description: SSH port on the deployment host + required: false + default: "22" + known_host: + description: Pinned OpenSSH known_hosts entry for the deployment endpoint + required: false + no_dependencies: + description: Deploy only the requested services + required: false + default: "false" compose_file: description: Path to compose file on remote server required: true @@ -39,6 +50,9 @@ runs: SSH_KEY: ${{ inputs.ssh_key }} REMOTE_USER: ${{ inputs.user }} REMOTE_HOST: ${{ inputs.host }} + REMOTE_PORT: ${{ inputs.port }} + KNOWN_HOST: ${{ inputs.known_host }} + NO_DEPENDENCIES: ${{ inputs.no_dependencies }} COMPOSE_FILE: ${{ inputs.compose_file }} COMPOSE_PROJECT_NAME: ${{ inputs.compose_project_name }} SERVICES: ${{ inputs.services }} @@ -46,17 +60,28 @@ runs: REGISTRY_USERNAME: ${{ inputs.registry_username }} REGISTRY_PASSWORD: ${{ inputs.registry_password }} run: | - mkdir -p ~/.ssh - chmod 700 ~/.ssh - echo "$SSH_KEY" > ~/.ssh/deploy_key - chmod 600 ~/.ssh/deploy_key - printf '%s' "$REGISTRY_PASSWORD" | ssh -i ~/.ssh/deploy_key \ - -o StrictHostKeyChecking=no \ - -o UserKnownHostsFile=/dev/null \ - "$REMOTE_USER@$REMOTE_HOST" \ - "docker login '$REGISTRY' -u '$REGISTRY_USERNAME' --password-stdin && \ - export COMPOSE_PROJECT_NAME='$COMPOSE_PROJECT_NAME' && \ - docker compose -f '$COMPOSE_FILE' up $SERVICES -d --pull=always; \ - rc=\$?; \ - docker logout '$REGISTRY'; \ - exit \$rc" + set -euo pipefail + umask 077 + ssh_dir=$(mktemp -d) + trap 'rm -rf -- "$ssh_dir"' EXIT + printf '%s\n' "$SSH_KEY" > "$ssh_dir/deploy_key" + ssh_options=(-p "$REMOTE_PORT" -i "$ssh_dir/deploy_key" -o BatchMode=yes -o IdentitiesOnly=yes) + if [ -n "$KNOWN_HOST" ]; then + printf '%s\n' "$KNOWN_HOST" > "$ssh_dir/known_hosts" + ssh_options+=(-F /dev/null -o StrictHostKeyChecking=yes + -o "UserKnownHostsFile=$ssh_dir/known_hosts" -o GlobalKnownHostsFile=/dev/null) + else + ssh_options+=(-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null) + fi + deploy_options=() + if [ "$NO_DEPENDENCIES" = true ]; then + deploy_options+=(--no-deps --wait --wait-timeout 90) + fi + read -r -a deploy_services <<< "$SERVICES" + printf -v compose_args ' %q' -p "$COMPOSE_PROJECT_NAME" -f "$COMPOSE_FILE" \ + up -d --pull always "${deploy_options[@]}" "${deploy_services[@]}" + printf -v login_args ' %q' "$REGISTRY" -u "$REGISTRY_USERNAME" --password-stdin + printf '%s' "$REGISTRY_PASSWORD" | ssh "${ssh_options[@]}" -- "$REMOTE_USER@$REMOTE_HOST" \ + "set -eu; umask 077; DOCKER_CONFIG=\$(mktemp -d); export DOCKER_CONFIG; \ + trap 'rm -rf -- \"\$DOCKER_CONFIG\"' EXIT; \ + docker login$login_args; docker compose$compose_args" diff --git a/.gitea/workflows/ci-stage.yaml b/.gitea/workflows/ci-stage.yaml index aecf162..29ccf1d 100644 --- a/.gitea/workflows/ci-stage.yaml +++ b/.gitea/workflows/ci-stage.yaml @@ -35,9 +35,13 @@ jobs: ssh_key: ${{ secrets.STAGE_SERVER_KEY }} user: ${{ secrets.STAGE_SERVER_USER }} host: ${{ secrets.STAGE_SERVER_HOST }} - compose_file: /services/srv-stage/compose.yml + port: "2223" + known_host: "[185.206.92.225]:2223 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID8clronRivcK4ypn57l8nGuZ+xszZBxYNp8C0iYaqLp" + no_dependencies: "true" + compose_file: /home/navid/darano/srv-stage/compose.yml compose_project_name: srv-stage - services: gl + services: gl gl-explorer + registry_username: ${{ secrets.REG_USER }} registry_password: ${{ secrets.REG_PASS }} - name: Notify Telegram if: always()