SpicyPizza/create-envfile

How do I create the env file in a server i have provided ssh credentials for?

simkimsia opened this issue · 2 comments

This is my workflow

# This is a basic workflow to help you get started with Actions

name: digitalocean-pull-and-setup-env

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the $default-branch branch
  # push:
  #   branches: [ $default-branch ]
  # pull_request:
  #   branches: [ $default-branch ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # SSH into digitalocean droplet
      - name: executing remote ssh commands using password
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.SSH_HOST }}
          username: ${{ secrets.SSH_USER }}
          key: ${{ secrets.SSH_KEY }}


      # Runs a single command using the runners shell
      - name: Run a one-line script
        run: echo Hello, world!

      # Runs a set of commands using the runners shell
      - name: Run a multi-line script
        run: |
          echo Add other actions to build,
          echo test, and deploy your project.

      # create the env file
      - name: Make envfile
        uses: SpicyPizza/create-envfile@v1
        with:
          DEBUG: False
          DEVELOPMENT_MODE: False
          DJANGO_SECRET_KEY: ${{ secrets.DJANGO_SECRET }}
          directory: /home/django/all_django_projects
          file_name: .django

i have already setup the ssh-action but it looks like it run the last step separately from the ssh action

Just in case you haven't found the solution yet:

Every step in your script is run completely seperately. So when you're SSH-ing into your DigitalOcean droplet, you're not actually doing anything in this script.

To run commands inside your droplet, you have to provide these to your ssh-action configuration like so:

- name: executing remote ssh commands using password
  uses: appleboy/ssh-action@master
  with:
    host: ${{ secrets.SSH_HOST }}
    username: ${{ secrets.SSH_USER }}
    key: ${{ secrets.SSH_KEY }}
    script: |
      echo "This command is run on the DigitalOcean droplet"

So your second question "How do you get the .env file on the droplet" you can see as this:

  1. Generate your .env file
  2. SCP this generate file to the droplet.

You can do this like this:

- name: Make envfile
  uses: SpicyPizza/create-envfile@v1
  with:
    envkey_DEBUG: False
    envkey_DEVELOPMENT_MODE: False
    envkey_DJANGO_SECRET_KEY: ${{ secrets.DJANGO_SECRET }}
    file_name: .django

Pay attention to the envkey_ prefix for your environment variables, these are required.

And then copying it to your server:

- name: Copy env file to server
  uses: appleboy/scp-action@master
  with:
    host: ${{ secrets.SSH_HOST }}
    username: ${{ secrets.SSH_USER }}
    key: ${{ secrets.SSH_KEY }}
    source: ".django"
    target: "/home/django/all_django_projects"

Let me know if you have any other questions.