Skip to content

Commit

Permalink
chore: Update GitHub Actions workflow for golang linting, testing, an…
Browse files Browse the repository at this point in the history
…d building
  • Loading branch information
waveyboym committed Jun 12, 2024
1 parent 4ae2346 commit 2b39732
Show file tree
Hide file tree
Showing 7 changed files with 267 additions and 11 deletions.
70 changes: 70 additions & 0 deletions .github/workflows/deploy-golang-develop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: Lint, Test, Build and Deploy golang

on:
push:
branches: ["develop", "feat/backend/deployment-and-up"]
paths: [
"occupi-backend/cmd/**",
"occupi-backend/configs/**",
"occupi-backend/pkg/**",
"occupi-backend/.golangci.yml",
"occupi-backend/tests/**",
".github/workflows/lint-test-build-golang.yml",
".github/workflows/deploy-golang-develop.yml"
]

workflow_dispatch:

defaults:
run:
working-directory: occupi-backend

jobs:
build-push-docker:
name: Build and Push Develop Docker Image
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Login to DockerHub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Build and push Docker image
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ secrets.DOCKER_USERNAME }}/occupi-backend:latest

deploy:
needs: build-push-docker
name: Deploy to Develop
runs-on: ubuntu-latest

steps:
- name: SSH to VM
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.VM_IP }}
username: ${{ secrets.VM_USERNAME }}
key: ${{ secrets.VM_SSH_KEY }}
script: |
cd /home/Y2KODELABS/occupi-backend
gpg --quiet --batch --yes --decrypt --passphrase=$GPG_PASSPHRASE --output .env .env.gpg
docker-compose -f docker-compose.traefik.yml up -d
docker-compose -f docker-compose.dev.yml pull
docker-compose -f docker-compose.dev.yml up -d --build
133 changes: 133 additions & 0 deletions .github/workflows/deploy-golang-prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
name: Lint, Test and Build golang

on:
push:
branches: ["master"]
paths: [
"occupi-backend/cmd/**",
"occupi-backend/configs/**",
"occupi-backend/pkg/**",
"occupi-backend/.golangci.yml",
"occupi-backend/tests/**",
".github/workflows/lint-test-build-golang.yml",
".github/workflows/deploy-golang-prod.yml"
]

workflow_dispatch:

defaults:
run:
working-directory: occupi-backend

jobs:
lint:
name: Lint
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.21' # Specify the Go version you are using

- name: Install golangci-lint
run: |
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
- name: Run golangci-lint
run: |
golangci-lint run
test:
needs: lint
name: Test
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.21' # Specify the Go version you are using

- name: Run tests
run: |
go test -v -coverpkg=github.com/COS301-SE-2024/occupi/occupi-backend/pkg/utils,github.com/COS301-SE-2024/occupi/occupi-backend/pkg/handlers ./tests/... -coverprofile=coverage.out
- name: Upload coverage reports to Codecov
uses: codecov/[email protected]
with:
token: ${{ secrets.CODECOV_TOKEN }}

build:
needs: test
name: Build
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.21' # Specify the Go version you are using

- name: Build the code
run: |
go build -v cmd/occupi-backend/main.go
build-push-docker:
needs: build
name: Build and Push Master Docker Image
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Login to DockerHub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Build and push Docker image
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ secrets.DOCKER_USERNAME }}/occupi-backend:latest

deploy:
needs: build-push-docker
name: Deploy to Master
runs-on: ubuntu-latest

steps:
- name: SSH to VM
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.VM_IP }}
username: ${{ secrets.VM_USERNAME }}
key: ${{ secrets.VM_SSH_KEY }}
script: |
cd /home/Y2KODELABS/occupi-backend
gpg --quiet --batch --yes --decrypt --passphrase=$GPG_PASSPHRASE --output .env .env.gpg
docker-compose -f docker-compose.traefik.yml up -d
docker-compose -f docker-compose.prod.yml pull
docker-compose -f docker-compose.prod.yml up -d --build
10 changes: 0 additions & 10 deletions .github/workflows/lint-test-build-golang.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
name: Lint, Test and Build golang

on:
push:
branches: ["develop"]
paths: [
"occupi-backend/cmd/**",
"occupi-backend/configs/**",
"occupi-backend/pkg/**",
"occupi-backend/.golangci.yml",
"occupi-backend/tests/**",
".github/workflows/lint-test-build-golang.yml"
]
pull_request:
branches: ["develop"]
paths: [
Expand Down
5 changes: 4 additions & 1 deletion occupi-backend/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@
*.tmp
*.swp
*.gitignore
*.md
*.md
*.out
*.toml
*.exe
18 changes: 18 additions & 0 deletions occupi-backend/docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: '3.8'
services:
occupi-backend-dev:
build: .
container_name: occupi-backend-dev
env_file:
- .env
environment:
- ENV=development
networks:
- webnet
labels:
- "traefik.http.routers.occupi-backend-dev.rule=Host(`dev.occupi.tech`, `www.dev.occupi.tech`)"
- "traefik.http.services.occupi-backend-dev.loadbalancer.server.port=8080"

networks:
webnet:
external: true
18 changes: 18 additions & 0 deletions occupi-backend/docker-compose.prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: '3.8'
services:
occupi-backend-prod:
build: .
container_name: occupi-backend-prod
env_file:
- .env
environment:
- ENV=production
networks:
- webnet
labels:
- "traefik.http.routers.occupi-backend-prod.rule=Host(`occupi.tech`)"
- "traefik.http.services.occupi-backend-prod.loadbalancer.server.port=8080"

networks:
webnet:
external: true
24 changes: 24 additions & 0 deletions occupi-backend/docker-compose.traefik.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
version: '3.8'
services:
traefik:
image: traefik:v3.0.2
command:
- "--api.insecure=true"
- "--providers.docker=true"
- "--entrypoints.web.address=:80"
- "--entrypoints.websecure.address=:443"
- "--certificatesresolvers.myresolver.acme.tlschallenge=true"
- "--certificatesresolvers.myresolver.acme.email=u21546551@tuks.co.za"
- "--certificatesresolvers.myresolver.acme.storage=/letsencrypt/acme.json"
ports:
- "80:80"
- "443:443"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
- "./letsencrypt:/letsencrypt"
networks:
- webnet

networks:
webnet:
external: true

0 comments on commit 2b39732

Please sign in to comment.