Skip to content

Deploy Go to EC2 (manual) #49

Deploy Go to EC2 (manual)

Deploy Go to EC2 (manual) #49

Workflow file for this run

name: Deploy Go to EC2 (manual)
on:
workflow_dispatch: # ← Manual trigger button
inputs:
environment:
description: 'environment'
required: true
default: 'dev'
type: choice
options:
- prod
- dev
version:
description: 'version'
required: false
default: 'latest'
type: string
command:
description: 'deploy or restart'
required: true
default: 'deploy'
type: choice
options:
- build
- deploy
- restart
- rollback
jobs:
generate-env:
name: Generate .env file
runs-on: ubuntu-latest
outputs:
env-path: ${{ steps.set-output.outputs.env-path }}
steps:
- name: Generate .env file
run: |
echo "MYSQL_PASSWORD=${{ secrets.MYSQL_PASSWORD }}" >> env-file
echo "SMTP_PASSWORD=${{ secrets.SMTP_PASSWORD }}" >> env-file
echo "SMTP_EMAIL_FROM=${{ secrets.SMTP_EMAIL_FROM }}" >> env-file
echo "JWT_SECRET=${{ secrets.JWT_SECRET }}" >> env-file
- name: Upload env-file as artifact
uses: actions/upload-artifact@v4
with:
name: env-file
path: env-file
build:
if: ${{ github.event.inputs.command == 'deploy' }} || ${{ github.event.inputs.command == 'build' }}
runs-on: ubuntu-latest
env:
DOCKER_HUB_USERNAME: ${{ secrets.DOCKER_HUB_USERNAME }}
steps:
- name: checkout
uses: actions/checkout@v4
- name: login to dockerhub
uses: docker/login-action@184bdaa0721073962dff0199f1fb9940f07167d1
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
- name: build docker image
run: |
docker build -t "${DOCKER_HUB_USERNAME}/ceramicraft-user-mservice:${{ github.event.inputs.version }}" server/
- name: push to dockerhub
run: |
docker push "${DOCKER_HUB_USERNAME}/ceramicraft-user-mservice:${{ github.event.inputs.version }}"
deploy:
if: ${{ github.event.inputs.command == 'deploy' }} && ${{ github.event.inputs.environment == 'dev' }}
needs:
- build
- generate-env
runs-on: [self-hosted, dev, docker]
environment: dev
env:
DOCKER_HUB_USERNAME: ${{ secrets.DOCKER_HUB_USERNAME }}
steps:
- name: Download .env file
uses: actions/download-artifact@v4
with:
name: env-file
path: .
- name: pull the docker image
run: |
docker pull "${DOCKER_HUB_USERNAME}/ceramicraft-user-mservice:${{ github.event.inputs.version }}"
- name: run docker container
run: |
docker stop ceramicraft-user-mservice || true
docker rm ceramicraft-user-mservice || true
docker run -d --name ceramicraft-user-mservice --network ceramicraft-network -v /home/ubuntu/logs:/app/logs --env-file env-file "${DOCKER_HUB_USERNAME}/ceramicraft-user-mservice:${{ github.event.inputs.version }}"
swarm-deploy:
if: ${{ github.event.inputs.command == 'deploy' }} && ${{ github.event.inputs.environment == 'prod' }}
runs-on: [self-hosted,prod,swarm]
environment: prod
needs: build
env:
DOCKER_HUB_USERNAME: ${{ secrets.DOCKER_HUB_USERNAME }}
steps:
- name: pull the docker image
run: |
docker pull "${DOCKER_HUB_USERNAME}/ceramicraft-user-mservice:${{ github.event.inputs.version }}"
- name: update the service
run: |
docker service update --force --image "${DOCKER_HUB_USERNAME}/ceramicraft-user-mservice:${{ github.event.inputs.version }}" ms-stack_user-ms || true
restart:
if: ${{ github.event.inputs.command == 'restart' }}
environment: ${{ github.event.inputs.environment }}
runs-on: ${{ matrix.runner}}
strategy:
matrix:
environment: [dev, prod]
include:
- environment: dev
runner: [self-hosted, dev, docker]
- environment: prod
runner: [self-hosted, prod, swarm]
runner: [self-hosted, dev, docker, prod, swarm]
steps:
- name: restart docker container
run: |
echo "Restarting container on ${{ matrix.environment }} environment"
docker restart ceramicraft-user-mservice || true
rollback:
if: ${{ github.event.inputs.command == 'rollback' }} && ${{ github.event.inputs.environment == 'dev' }}
runs-on: [self-hosted, dev, docker]
environment: dev
needs: generate-env
env:
DOCKER_HUB_USERNAME: ${{ secrets.DOCKER_HUB_USERNAME }}
steps:
- name: Download .env file
uses: actions/download-artifact@v4
with:
name: env-file
path: .
- name: pull the docker image
run: |
docker pull "${DOCKER_HUB_USERNAME}/ceramicraft-user-mservice:${{ github.event.inputs.version }}"
- name: run docker container
run: |
docker stop ceramicraft-user-mservice || true
docker rm ceramicraft-user-mservice || true
docker run -d --name ceramicraft-user-mservice --network ceramicraft-network -v /home/ubuntu/logs:/app/logs --env-file env-file "${DOCKER_HUB_USERNAME}/ceramicraft-user-mservice:${{ github.event.inputs.version }}"
swarm-rollback:
if: ${{ github.event.inputs.command == 'rollback' }} && ${{ github.event.inputs.environment == 'prod' }}
runs-on: [self-hosted,prod,swarm]
environment: prod
env:
DOCKER_HUB_USERNAME: ${{ secrets.DOCKER_HUB_USERNAME }}
steps:
- name: pull the docker image
run: |
docker pull "${DOCKER_HUB_USERNAME}/ceramicraft-user-mservice:${{ github.event.inputs.version }}"
- name: update the service
run: |
docker service update --force --image "${DOCKER_HUB_USERNAME}/ceramicraft-user-mservice:${{ github.event.inputs.version }}" ms-stack_user-ms || true