diff --git a/.github/workflows/dev-deploy.yml b/.github/workflows/dev-deploy.yml index 0d36c27..af59589 100644 --- a/.github/workflows/dev-deploy.yml +++ b/.github/workflows/dev-deploy.yml @@ -2,9 +2,6 @@ name: Dev Deployment on: workflow_dispatch: - push: - branches: - - dev jobs: build_and_upload_image: diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml new file mode 100644 index 0000000..b2492ea --- /dev/null +++ b/.github/workflows/dev.yml @@ -0,0 +1,83 @@ +name: Build, Test, and Deploy for Development + +on: + push: + branches: + - dev + +jobs: + build_and_upload_artifact: + runs-on: ubuntu-latest + if: github.event.repository.fork == false + environment: development + env: + SSH_USERNAME: ${{ secrets.SSH_USERNAME }} + SSH_HOST: ${{ secrets.SSH_HOST }} + SSH_PORT: ${{ secrets.SSH_PORT }} + SSH_PASSWORD: ${{ secrets.SSH_PASSWORD }} + + steps: + - name: Checkout Code + uses: actions/checkout@v4 + - name: Set Golang + uses: actions/setup-go@v4 + with: + go-version: "1.22.1" + - name: Create app config file + run: cp app-sample.env app.env + - name: Build the application + run: go build -o development_app + + - name: Create app.env file + uses: vicradon/create-env-action@v1.0.1 + with: + action_input_file: "app-sample.env" + action_output_file: "app.env" + action_true_string_variables: | + MAIL_PASSWORD + SERVER_PORT: ${{ secrets.SERVER_PORT }} + DB_NAME: ${{ secrets.DB_NAME }} + USERNAME: ${{ secrets.USERNAME }} + APP_NAME: "development" + APP_URL: ${{ vars.URL}} + REDIS_PORT: ${{ secrets.REDIS_PORT }} + MAIL_SERVER: ${{ secrets.MAIL_SERVER }} + MAIL_USERNAME: ${{ secrets.MAIL_USERNAME }} + MAIL_PASSWORD: ${{ secrets.MAIL_PASSWORD }} + MAIL_PORT: ${{ secrets.MAIL_PORT }} + MIGRATE: "true" + + - name: Copy artifacts to server + uses: appleboy/scp-action@v0.1.7 + with: + host: ${{ env.SSH_HOST }} + username: ${{ env.SSH_USERNAME }} + password: ${{ env.SSH_PASSWORD }} + port: ${{ env.SSH_PORT }} + source: "app.env,development_app" + target: ~/deployments/development + + restart_app: + runs-on: ubuntu-latest + needs: build_and_upload_artifact + environment: development + env: + SSH_USERNAME: ${{ secrets.SSH_USERNAME }} + SSH_HOST: ${{ secrets.SSH_HOST }} + SSH_PORT: ${{ secrets.SSH_PORT }} + SSH_PASSWORD: ${{ secrets.SSH_PASSWORD }} + + steps: + - name: SSH into server and deploy + uses: appleboy/ssh-action@v1.0.3 + with: + host: ${{ env.SSH_HOST }} + username: ${{ env.SSH_USERNAME }} + password: ${{ env.SSH_PASSWORD }} + port: ${{ env.SSH_PORT }} + script: | + mkdir -p ~/deployments/development + cd ~/deployments/development + git reset --hard + git pull origin dev + pm2 restart development_app \ No newline at end of file diff --git a/.github/workflows/prod.yml b/.github/workflows/prod.yml new file mode 100644 index 0000000..e4c9f89 --- /dev/null +++ b/.github/workflows/prod.yml @@ -0,0 +1,83 @@ +name: Build, Test, and Deploy for Production + +on: + push: + branches: + - main + +jobs: + build_and_upload_artifact: + runs-on: ubuntu-latest + if: github.event.repository.fork == false + environment: production + env: + SSH_USERNAME: ${{ secrets.SSH_USERNAME }} + SSH_HOST: ${{ secrets.SSH_HOST }} + SSH_PORT: ${{ secrets.SSH_PORT }} + SSH_PASSWORD: ${{ secrets.SSH_PASSWORD }} + + steps: + - name: Checkout Code + uses: actions/checkout@v4 + - name: Set Golang + uses: actions/setup-go@v4 + with: + go-version: "1.22.1" + - name: Create app config file + run: cp app-sample.env app.env + - name: Build the application + run: go build -o production_app + + - name: Create app.env file + uses: vicradon/create-env-action@v1.0.1 + with: + action_input_file: "app-sample.env" + action_output_file: "app.env" + action_true_string_variables: | + MAIL_PASSWORD + SERVER_PORT: ${{ secrets.SERVER_PORT }} + DB_NAME: ${{ secrets.DB_NAME }} + USERNAME: ${{ secrets.USERNAME }} + APP_NAME: "production" + APP_URL: ${{ vars.URL}} + REDIS_PORT: ${{ secrets.REDIS_PORT }} + MAIL_SERVER: ${{ secrets.MAIL_SERVER }} + MAIL_USERNAME: ${{ secrets.MAIL_USERNAME }} + MAIL_PASSWORD: ${{ secrets.MAIL_PASSWORD }} + MAIL_PORT: ${{ secrets.MAIL_PORT }} + MIGRATE: "true" + + - name: Copy artifacts to server + uses: appleboy/scp-action@v0.1.7 + with: + host: ${{ env.SSH_HOST }} + username: ${{ env.SSH_USERNAME }} + password: ${{ env.SSH_PASSWORD }} + port: ${{ env.SSH_PORT }} + source: "app.env,production_app" + target: ~/deployments/production + + restart_app: + runs-on: ubuntu-latest + needs: build_and_upload_artifact + environment: production + env: + SSH_USERNAME: ${{ secrets.SSH_USERNAME }} + SSH_HOST: ${{ secrets.SSH_HOST }} + SSH_PORT: ${{ secrets.SSH_PORT }} + SSH_PASSWORD: ${{ secrets.SSH_PASSWORD }} + + steps: + - name: SSH into server and deploy + uses: appleboy/ssh-action@v1.0.3 + with: + host: ${{ env.SSH_HOST }} + username: ${{ env.SSH_USERNAME }} + password: ${{ env.SSH_PASSWORD }} + port: ${{ env.SSH_PORT }} + script: | + mkdir -p ~/deployments/production + cd ~/deployments/production + git reset --hard + git pull origin main + pm2 restart production_app \ No newline at end of file diff --git a/.github/workflows/staging-deploy.yml b/.github/workflows/staging-deploy.yml index 57640b5..ec7a038 100644 --- a/.github/workflows/staging-deploy.yml +++ b/.github/workflows/staging-deploy.yml @@ -2,9 +2,6 @@ name: Staging Deployment on: workflow_dispatch: - push: - branches: - - staging jobs: build_and_upload_image: diff --git a/.github/workflows/staging.yml b/.github/workflows/staging.yml new file mode 100644 index 0000000..f54692b --- /dev/null +++ b/.github/workflows/staging.yml @@ -0,0 +1,83 @@ +name: Build, Test, and Deploy for Staging + +on: + push: + branches: + - staging + +jobs: + build_and_upload_artifact: + runs-on: ubuntu-latest + if: github.event.repository.fork == false + environment: staging + env: + SSH_USERNAME: ${{ secrets.SSH_USERNAME }} + SSH_HOST: ${{ secrets.SSH_HOST }} + SSH_PORT: ${{ secrets.SSH_PORT }} + SSH_PASSWORD: ${{ secrets.SSH_PASSWORD }} + + steps: + - name: Checkout Code + uses: actions/checkout@v4 + - name: Set Golang + uses: actions/setup-go@v4 + with: + go-version: "1.22.1" + - name: Create app config file + run: cp app-sample.env app.env + - name: Build the application + run: go build -o staging_app + + - name: Create app.env file + uses: vicradon/create-env-action@v1.0.1 + with: + action_input_file: "app-sample.env" + action_output_file: "app.env" + action_true_string_variables: | + MAIL_PASSWORD + SERVER_PORT: ${{ secrets.SERVER_PORT }} + DB_NAME: ${{ secrets.DB_NAME }} + USERNAME: ${{ secrets.USERNAME }} + APP_NAME: "staging" + APP_URL: ${{ vars.URL}} + REDIS_PORT: ${{ secrets.REDIS_PORT }} + MAIL_SERVER: ${{ secrets.MAIL_SERVER }} + MAIL_USERNAME: ${{ secrets.MAIL_USERNAME }} + MAIL_PASSWORD: ${{ secrets.MAIL_PASSWORD }} + MAIL_PORT: ${{ secrets.MAIL_PORT }} + MIGRATE: "true" + + - name: Copy artifacts to server + uses: appleboy/scp-action@v0.1.7 + with: + host: ${{ env.SSH_HOST }} + username: ${{ env.SSH_USERNAME }} + password: ${{ env.SSH_PASSWORD }} + port: ${{ env.SSH_PORT }} + source: "app.env,staging_app" + target: ~/deployments/staging + + restart_app: + runs-on: ubuntu-latest + needs: build_and_upload_artifact + environment: staging + env: + SSH_USERNAME: ${{ secrets.SSH_USERNAME }} + SSH_HOST: ${{ secrets.SSH_HOST }} + SSH_PORT: ${{ secrets.SSH_PORT }} + SSH_PASSWORD: ${{ secrets.SSH_PASSWORD }} + + steps: + - name: SSH into server and deploy + uses: appleboy/ssh-action@v1.0.3 + with: + host: ${{ env.SSH_HOST }} + username: ${{ env.SSH_USERNAME }} + password: ${{ env.SSH_PASSWORD }} + port: ${{ env.SSH_PORT }} + script: | + mkdir -p ~/deployments/staging + cd ~/deployments/staging + git reset --hard + git pull origin staging + pm2 restart staging_app \ No newline at end of file diff --git a/.gitignore b/.gitignore index ef23b7b..192d97c 100644 --- a/.gitignore +++ b/.gitignore @@ -34,4 +34,5 @@ app.env development_app staging_app -production_app \ No newline at end of file +production_app +.DS_Store \ No newline at end of file diff --git a/Dockerfile b/docker/Dockerfile similarity index 100% rename from Dockerfile rename to docker/Dockerfile diff --git a/docker-compose-production.yml b/docker/docker-compose-production.yml similarity index 95% rename from docker-compose-production.yml rename to docker/docker-compose-production.yml index a6b568e..cea71f8 100644 --- a/docker-compose-production.yml +++ b/docker/docker-compose-production.yml @@ -17,7 +17,7 @@ services: image: redis:latest backend: - image: golang_prod + image: golang_prod:latest build: context: . depends_on: diff --git a/docker-compose-staging.yml b/docker/docker-compose-staging.yml similarity index 95% rename from docker-compose-staging.yml rename to docker/docker-compose-staging.yml index f89079f..6ea13f9 100644 --- a/docker-compose-staging.yml +++ b/docker/docker-compose-staging.yml @@ -19,7 +19,7 @@ services: - ../golang_volumes/redis_volumes/golang_staging/:/data backend: - image: golang_staging + image: golang_staging:latest build: context: . depends_on: diff --git a/docker-compose.yml b/docker/docker-compose.yml similarity index 96% rename from docker-compose.yml rename to docker/docker-compose.yml index 04797ac..30ed204 100644 --- a/docker-compose.yml +++ b/docker/docker-compose.yml @@ -23,7 +23,7 @@ services: - ../golang_volumes/redis_volumes/golang_dev/:/data backend: - image: golang_dev + image: golang_dev:latest build: context: . depends_on: diff --git a/nginx.conf b/nginx/nginx.conf similarity index 100% rename from nginx.conf rename to nginx/nginx.conf