wshihadeh / docker-deployment-action

A GitHub Action that supports docker-compose and Docker Swarm deployments
https://docs.shihadeh.dev
MIT License
132 stars 75 forks source link

How to pass environment variables? #36

Open mauriciabad opened 1 year ago

mauriciabad commented 1 year ago

Hi, I'm having a problem using the action. I think that the environment variables are not being sent from the .env file to the remote server. I can't figure out how to send them. I'm new using Docker, maybe what I'm trying doesn't make sense.

I tried using the docker-compose --env-file option inside the args but it gives another error saying that the option is invalid. (See log 2)

This is my GitHub actions workflow:

name: Deploy backend

on: push

jobs:
  deploy-backend:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Create backend .env file
        run: echo "${{ secrets.BACKEND_PROD_ENV_FILE }}" > ./backend/.env

      - name: Deploy to server
        uses: wshihadeh/docker-deployment-action@v2
        with:
          remote_docker_host: ghactions@89.58.2.185
          ssh_private_key: ${{ secrets.DOCKER_SSH_PRIVATE_KEY }}
          ssh_public_key: ${{ secrets.DOCKER_SSH_PUBLIC_KEY }}
          stack_file_name: backend/docker-compose.yaml
          args: up -d

These are the error logs:

Run wshihadeh/docker-deployment-action@v[2](https://github.com/mauriciabad/llocs-de-begur/actions/runs/5932422673/job/16086198816?pr=3#step:5:2)
  with:
    remote_docker_host: ghactions@89.58.2.185
    ssh_private_key: ***
    ssh_public_key: ***
    stack_file_name: backend/docker-compose.yaml
    args: up -d
    remote_docker_port: 22
/usr/bin/docker run --name ef7d850d12d10ba656[4](https://github.com/mauriciabad/llocs-de-begur/actions/runs/5932422673/job/16086198816?pr=3#step:5:4)[5](https://github.com/mauriciabad/llocs-de-begur/actions/runs/5932422673/job/16086198816?pr=3#step:5:5)9dbbe00[6](https://github.com/mauriciabad/llocs-de-begur/actions/runs/5932422673/job/16086198816?pr=3#step:5:6)1190f55c2a_a[7](https://github.com/mauriciabad/llocs-de-begur/actions/runs/5932422673/job/16086198816?pr=3#step:5:7)[8](https://github.com/mauriciabad/llocs-de-begur/actions/runs/5932422673/job/16086198816?pr=3#step:5:8)dae --label ef7d85 --workdir /github/workspace --rm -e "INPUT_REMOTE_DOCKER_HOST" -e "INPUT_SSH_PRIVATE_KEY" -e "INPUT_SSH_PUBLIC_KEY" -e "INPUT_STACK_FILE_NAME" -e "INPUT_ARGS" -e "INPUT_REMOTE_DOCKER_PORT" -e "INPUT_DEPLOYMENT_MODE" -e "INPUT_COPY_STACK_FILE" -e "INPUT_DEPLOY_PATH" -e "INPUT_KEEP_FILES" -e "INPUT_DOCKER_PRUNE" -e "INPUT_PRE_DEPLOYMENT_COMMAND_ARGS" -e "INPUT_PULL_IMAGES_FIRST" -e "HOME" -e "GITHUB_JOB" -e "GITHUB_REF" -e "GITHUB_SHA" -e "GITHUB_REPOSITORY" -e "GITHUB_REPOSITORY_OWNER" -e "GITHUB_REPOSITORY_OWNER_ID" -e "GITHUB_RUN_ID" -e "GITHUB_RUN_NUMBER" -e "GITHUB_RETENTION_DAYS" -e "GITHUB_RUN_ATTEMPT" -e "GITHUB_REPOSITORY_ID" -e "GITHUB_ACTOR_ID" -e "GITHUB_ACTOR" -e "GITHUB_TRIGGERING_ACTOR" -e "GITHUB_WORKFLOW" -e "GITHUB_HEAD_REF" -e "GITHUB_BASE_REF" -e "GITHUB_EVENT_NAME" -e "GITHUB_SERVER_URL" -e "GITHUB_API_URL" -e "GITHUB_GRAPHQL_URL" -e "GITHUB_REF_NAME" -e "GITHUB_REF_PROTECTED" -e "GITHUB_REF_TYPE" -e "GITHUB_WORKFLOW_REF" -e "GITHUB_WORKFLOW_SHA" -e "GITHUB_WORKSPACE" -e "GITHUB_ACTION" -e "GITHUB_EVENT_PATH" -e "GITHUB_ACTION_REPOSITORY" -e "GITHUB_ACTION_REF" -e "GITHUB_PATH" -e "GITHUB_ENV" -e "GITHUB_STEP_SUMMARY" -e "GITHUB_STATE" -e "GITHUB_OUTPUT" -e "RUNNER_OS" -e "RUNNER_ARCH" -e "RUNNER_NAME" -e "RUNNER_ENVIRONMENT" -e "RUNNER_TOOL_CACHE" -e "RUNNER_TEMP" -e "RUNNER_WORKSPACE" -e "ACTIONS_RUNTIME_URL" -e "ACTIONS_RUNTIME_TOKEN" -e "ACTIONS_CACHE_URL" -e GITHUB_ACTIONS=true -e CI=true -v "/var/run/docker.sock":"/var/run/docker.sock" -v "/home/runner/work/_temp/_github_home":"/github/home" -v "/home/runner/work/_temp/_github_workflow":"/github/workflow" -v "/home/runner/work/_temp/_runner_file_commands":"/github/file_commands" -v "/home/runner/work/llocs-de-begur/llocs-de-begur":"/github/workspace" ef7d85:0d12d10ba65645[9](https://github.com/mauriciabad/llocs-de-begur/actions/runs/5932422673/job/16086198816?pr=3#step:5:10)dbbe0061190f55c2a up -d
Registering SSH keys...
Agent pid [10](https://github.com/mauriciabad/llocs-de-begur/actions/runs/5932422673/job/16086198816?pr=3#step:5:11)
Identity added: /github/home/.ssh/id_rsa (/github/home/.ssh/id_rsa)
Add known hosts
Connecting to ghactions@89.58.2.[18](https://github.com/mauriciabad/llocs-de-begur/actions/runs/5932422673/job/16086198816?pr=3#step:5:19)5... Command: docker-compose  --log-level debug --host ssh://ghactions@89.58.2.185:[22](https://github.com/mauriciabad/llocs-de-begur/actions/runs/5932422673/job/16086198816?pr=3#step:5:23) -f backend/docker-compose.yaml up -d
Using configuration files: ./backend/docker-compose.yaml
The DATABASE_CLIENT variable is not set. Defaulting to a blank string.
The DATABASE_NAME variable is not set. Defaulting to a blank string.
The DATABASE_USERNAME variable is not set. Defaulting to a blank string.
The DATABASE_PORT variable is not set. Defaulting to a blank string.
The JWT_SECRET variable is not set. Defaulting to a blank string.
The ADMIN_JWT_SECRET variable is not set. Defaulting to a blank string.
The DATABASE_PASSWORD variable is not set. Defaulting to a blank string.
The NODE_ENV variable is not set. Defaulting to a blank string.
The Compose file './backend/docker-compose.yaml' is invalid because:
networks.llocs-de-begur value Additional properties are not allowed ('name' was unexpected)
Unsupported config option for services.llocs-de-begurDB: 'platform'

And this is my docker-compose.yaml:

version: '3'
services:
  llocs-de-begur:
    container_name: llocs-de-begur
    build: .
    image: llocs-de-begur:latest
    restart: unless-stopped
    env_file: .env
    environment:
      DATABASE_CLIENT: ${DATABASE_CLIENT}
      DATABASE_HOST: llocs-de-begurDB
      DATABASE_NAME: ${DATABASE_NAME}
      DATABASE_USERNAME: ${DATABASE_USERNAME}
      DATABASE_PORT: ${DATABASE_PORT}
      JWT_SECRET: ${JWT_SECRET}
      ADMIN_JWT_SECRET: ${ADMIN_JWT_SECRET}
      DATABASE_PASSWORD: ${DATABASE_PASSWORD}
      NODE_ENV: ${NODE_ENV}
    volumes:
      - ./config:/opt/app/config
      - ./src:/opt/app/src
      - ./package.json:/opt/package.json
      - ./package-lock.json:/opt/package-lock.json

      - ./.env:/opt/app/.env
      - ./public/uploads:/opt/app/public/uploads
    ports:
      - '1337:1337'
    networks:
      - llocs-de-begur
    depends_on:
      - llocs-de-begurDB

  llocs-de-begurDB:
    container_name: llocs-de-begurDB
    platform: linux/amd64 #for platform error on Apple M1 chips
    restart: unless-stopped
    env_file: .env
    image: mysql:latest
    command: --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_USER: ${DATABASE_USERNAME}
      MYSQL_ROOT_PASSWORD: ${DATABASE_PASSWORD}
      MYSQL_PASSWORD: ${DATABASE_PASSWORD}
      MYSQL_DATABASE: ${DATABASE_NAME}
      MYSQL_ROOT_HOST: '%'
    volumes:
      - llocs-de-begur-data:/var/lib/mysql
      #- ./data:/var/lib/postgresql/data/ # if you want to use a bind folder
    ports:
      - '3306:3306'
    networks:
      - llocs-de-begur

  llocs-de-begurAdminer:
    container_name: llocs-de-begurAdminer
    image: adminer
    restart: unless-stopped
    ports:
      - '9090:8080'
    environment:
      - ADMINER_DEFAULT_SERVER=llocs-de-begurDB
    networks:
      - llocs-de-begur
    depends_on:
      - llocs-de-begurDB

volumes:
  llocs-de-begur-data:

networks:
  llocs-de-begur:
    name: Llocs-de-begur
    driver: bridge

Log 2, for the error using --env-vars

Run wshihadeh/docker-deployment-action@v[2](https://github.com/mauriciabad/llocs-de-begur/actions/runs/5932546176/job/16086528096?pr=3#step:5:2)
  with:
    remote_docker_host: ghactions@89.58.2.185
    ssh_private_key: ***
    ssh_public_key: ***
    stack_file_name: backend/docker-compose.yaml
    args: up -d --env-file backend/.env
    remote_docker_port: 22
/usr/bin/docker run --name ef7d85e9c2d5e1[3](https://github.com/mauriciabad/llocs-de-begur/actions/runs/5932546176/job/16086528096?pr=3#step:5:3)820[4](https://github.com/mauriciabad/llocs-de-begur/actions/runs/5932546176/job/16086528096?pr=3#step:5:4)47c8[5](https://github.com/mauriciabad/llocs-de-begur/actions/runs/5932546176/job/16086528096?pr=3#step:5:5)7cb7b2e503d37[6](https://github.com/mauriciabad/llocs-de-begur/actions/runs/5932546176/job/16086528096?pr=3#step:5:6)_436b2a --label ef[7](https://github.com/mauriciabad/llocs-de-begur/actions/runs/5932546176/job/16086528096?pr=3#step:5:7)d[8](https://github.com/mauriciabad/llocs-de-begur/actions/runs/5932546176/job/16086528096?pr=3#step:5:8)5 --workdir /github/workspace --rm -e "INPUT_REMOTE_DOCKER_HOST" -e "INPUT_SSH_PRIVATE_KEY" -e "INPUT_SSH_PUBLIC_KEY" -e "INPUT_STACK_FILE_NAME" -e "INPUT_ARGS" -e "INPUT_REMOTE_DOCKER_PORT" -e "INPUT_DEPLOYMENT_MODE" -e "INPUT_COPY_STACK_FILE" -e "INPUT_DEPLOY_PATH" -e "INPUT_KEEP_FILES" -e "INPUT_DOCKER_PRUNE" -e "INPUT_PRE_DEPLOYMENT_COMMAND_ARGS" -e "INPUT_PULL_IMAGES_FIRST" -e "HOME" -e "GITHUB_JOB" -e "GITHUB_REF" -e "GITHUB_SHA" -e "GITHUB_REPOSITORY" -e "GITHUB_REPOSITORY_OWNER" -e "GITHUB_REPOSITORY_OWNER_ID" -e "GITHUB_RUN_ID" -e "GITHUB_RUN_NUMBER" -e "GITHUB_RETENTION_DAYS" -e "GITHUB_RUN_ATTEMPT" -e "GITHUB_REPOSITORY_ID" -e "GITHUB_ACTOR_ID" -e "GITHUB_ACTOR" -e "GITHUB_TRIGGERING_ACTOR" -e "GITHUB_WORKFLOW" -e "GITHUB_HEAD_REF" -e "GITHUB_BASE_REF" -e "GITHUB_EVENT_NAME" -e "GITHUB_SERVER_URL" -e "GITHUB_API_URL" -e "GITHUB_GRAPHQL_URL" -e "GITHUB_REF_NAME" -e "GITHUB_REF_PROTECTED" -e "GITHUB_REF_TYPE" -e "GITHUB_WORKFLOW_REF" -e "GITHUB_WORKFLOW_SHA" -e "GITHUB_WORKSPACE" -e "GITHUB_ACTION" -e "GITHUB_EVENT_PATH" -e "GITHUB_ACTION_REPOSITORY" -e "GITHUB_ACTION_REF" -e "GITHUB_PATH" -e "GITHUB_ENV" -e "GITHUB_STEP_SUMMARY" -e "GITHUB_STATE" -e "GITHUB_OUTPUT" -e "RUNNER_OS" -e "RUNNER_ARCH" -e "RUNNER_NAME" -e "RUNNER_ENVIRONMENT" -e "RUNNER_TOOL_CACHE" -e "RUNNER_TEMP" -e "RUNNER_WORKSPACE" -e "ACTIONS_RUNTIME_URL" -e "ACTIONS_RUNTIME_TOKEN" -e "ACTIONS_CACHE_URL" -e GITHUB_ACTIONS=true -e CI=true -v "/var/run/docker.sock":"/var/run/docker.sock" -v "/home/runner/work/_temp/_github_home":"/github/home" -v "/home/runner/work/_temp/_github_workflow":"/github/workflow" -v "/home/runner/work/_temp/_runner_file_commands":"/github/file_commands" -v "/home/runner/work/llocs-de-begur/llocs-de-begur":"/github/workspace" ef7d85:e[9](https://github.com/mauriciabad/llocs-de-begur/actions/runs/5932546176/job/16086528096?pr=3#step:5:10)c2d5e13820447c857cb7b2e503d376 up -d --env-file backend/.env
Registering SSH keys...
Identity added: /github/home/.ssh/id_rsa (/github/home/.ssh/id_rsa)
Agent pid [10](https://github.com/mauriciabad/llocs-de-begur/actions/runs/5932546176/job/16086528096?pr=3#step:5:11)
Add known hosts
Connecting to ghactions@89.58.2.[18](https://github.com/mauriciabad/llocs-de-begur/actions/runs/5932546176/job/16086528096?pr=3#step:5:19)5... Command: docker-compose  --log-level debug --host ssh://ghactions@89.58.2.185:[22](https://github.com/mauriciabad/llocs-de-begur/actions/runs/5932546176/job/16086528096?pr=3#step:5:23) -f backend/docker-compose.yaml up -d --env-file backend/.env
Builds, (re)creates, starts, and attaches to containers for a service.

Unless they are already running, this command also starts any linked services.

The `docker-compose up` command aggregates the output of each container. When
the command exits, all containers are stopped. Running `docker-compose up -d`
starts the containers in the background and leaves them running.

If there are existing containers for a service, and the service's configuration
or image was changed after the container's creation, `docker-compose up` picks
up the changes by stopping and recreating the containers (preserving mounted
volumes). To prevent Compose from picking up changes, use the `--no-recreate`
flag.

If you want to force Compose to stop and recreate all containers, use the
`--force-recreate` flag.

Usage: up [options] [--scale SERVICE=NUM...] [SERVICE...]

Options:
    -d, --detach               Detached mode: Run containers in the background,
                               print new container names. Incompatible with
                               --abort-on-container-exit.
    --no-color                 Produce monochrome output.
    --quiet-pull               Pull without printing progress information
    --no-deps                  Don't start linked services.
    --force-recreate           Recreate containers even if their configuration
                               and image haven't changed.
    --always-recreate-deps     Recreate dependent containers.
                               Incompatible with --no-recreate.
    --no-recreate              If containers already exist, don't recreate
                               them. Incompatible with --force-recreate and -V.
    --no-build                 Don't build an image, even if it's missing.
    --no-start                 Don't start the services after creating them.
    --build                    Build images before starting containers.
    --abort-on-container-exit  Stops all containers if any container was
                               stopped. Incompatible with -d.
    --attach-dependencies      Attach to dependent containers
    -t, --timeout TIMEOUT      Use this timeout in seconds for container
                               shutdown when attached or when containers are
                               already running. (default: 10)
    -V, --renew-anon-volumes   Recreate anonymous volumes instead of retrieving
                               data from the previous containers.
    --remove-orphans           Remove containers for services not defined
                               in the Compose file.
    --exit-code-from SERVICE   Return the exit code of the selected service
                               container. Implies --abort-on-container-exit.
    --scale SERVICE=NUM        Scale SERVICE to NUM instances. Overrides the
                               `scale` setting in the Compose file if present.
0s
mauriciabad commented 1 year ago

Well, at the end I just didn't use this action and this this workflow:

Where DOCKER_HOST looks like this: ssh://username@serverip

name: Deploy backend

on:
  push:
    branches: main

jobs:
  deploy-backend:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Create backend .env file
        run: echo "${{ secrets.BACKEND_PROD_ENV_FILE }}" > ./backend/.env

      - name: Install SSH key
        uses: shimataro/ssh-key-action@v2
        with:
          key: ${{ secrets.DOCKER_SSH_PRIVATE_KEY }}
          known_hosts: ${{ secrets.DOCKER_SSH_KNOWN_HOSTS }}

      - name: Deploy to server
        run: docker compose -f backend/docker-compose.yaml up -d
        env:
          DOCKER_HOST: ${{ secrets.DOCKER_HOST }}

I was expecting this action to do exactly the same as I did with this workflow, but somehow it does a different thing.