Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automate docker image build and push to VPS #4

Open
1 of 3 tasks
elgorditosalsero opened this issue Nov 5, 2023 · 5 comments
Open
1 of 3 tasks

Automate docker image build and push to VPS #4

elgorditosalsero opened this issue Nov 5, 2023 · 5 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@elgorditosalsero
Copy link
Owner

Automate docker image build and push to VPS

Description

Feature type

  • Idea
  • Problem/Issue
  • Other

Idea

I'd love to automate the creation of the image when there are new merges into main to not have to deploy the bot manually and restart it via docker.

Proposal

I'm looking for help 😄

Alternatives

Please tell me 😄

@elgorditosalsero elgorditosalsero added enhancement New feature or request help wanted Extra attention is needed labels Nov 5, 2023
@fedy97
Copy link

fedy97 commented Nov 6, 2023

Hi @elgorditosalsero you could use this Github Action, by editing the last script if you do not want to use docker-compose. You can put secrets to Settings -> Secrets and variables -> Actions

name: Deploy Node App with Docker

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      - name: Copy project to server
        uses: appleboy/scp-action@master
        with:
          host: ${{ secrets.SSH_HOST }}
          username: ${{ secrets.SSH_USERNAME }}
          key: ${{ secrets.SSH_PRIVATE_KEY }}
          port: 22
          source: "."
          target: ${{ secrets.TARGET_PATH }}

      - name: Build and Run Docker Container on server
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.SSH_HOST }}
          username: ${{ secrets.SSH_USERNAME }}
          key: ${{ secrets.SSH_PRIVATE_KEY }}
          port: 22
          script: |
            cd {{ secrets.TARGET_PATH }}
            docker-compose up --build -d

@elgorditosalsero
Copy link
Owner Author

Thank you @fedy97!

This is super nice and seems promising 😄

I need to understand how to handle env var too, but that's a starting point!

@fedy97
Copy link

fedy97 commented Nov 6, 2023

If you already have a .env file in the same directory as the docker-compose.yaml file, docker should automatically read it. Otherwise, you can change the command to docker-compose --env-file /path/to/your/.env up --build -d

@elgorditosalsero
Copy link
Owner Author

If you already have a .env file in the same directory as the docker-compose.yaml file, docker should automatically read it. Otherwise, you can change the command to docker-compose --env-file /path/to/your/.env up --build -d

Yeah, but I'm referring to the .env for the project, I'd love to avoid to write it directly on the VPS, as if I want to make some changes, I always have to login there, but I can handle them via the secrets here would be probably be better 😄

I have to do some research about it

@fedy97
Copy link

fedy97 commented Nov 8, 2023

If you already have a .env file in the same directory as the docker-compose.yaml file, docker should automatically read it. Otherwise, you can change the command to docker-compose --env-file /path/to/your/.env up --build -d

Yeah, but I'm referring to the .env for the project, I'd love to avoid to write it directly on the VPS, as if I want to make some changes, I always have to login there, but I can handle them via the secrets here would be probably be better 😄

I have to do some research about it

@elgorditosalsero a brutal way of doing that would be to edit the last action in something like this

- name: Build and Run Docker Container on server
   uses: appleboy/ssh-action@master
   with:
      host: ${{ secrets.SSH_HOST }}
      username: ${{ secrets.SSH_USERNAME }}
      key: ${{ secrets.SSH_PRIVATE_KEY }}
      port: 22
      script: |
        cd ${{ secrets.TARGET_PATH }}
        # Export your secrets as environment variables
        export SECRET1="${{ secrets.SECRET1 }}"
        export SECRET2="${{ secrets.SECRET2 }}"
        docker-compose up --build -d

or recreating the .env each time

- name: Create .env file and Run Docker Container on server
   uses: appleboy/ssh-action@master
   with:
      host: ${{ secrets.SSH_HOST }}
      username: ${{ secrets.SSH_USERNAME }}
      key: ${{ secrets.SSH_PRIVATE_KEY }}
      port: 22
      script: |
        cd ${{ secrets.TARGET_PATH }}
        cat <<EOF > .env
        SECRET1=${{ secrets.SECRET1 }}
        SECRET2=${{ secrets.SECRET2 }}
        EOF
        docker-compose up --build -d

so that all the secrets stored in github are exported each time a new deploy is triggered. Let's see if someone else has a smarter solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
Status: 🆕 Idea
Development

No branches or pull requests

2 participants