diff --git a/.github/workflows/deploy-golang-develop.yml b/.github/workflows/deploy-golang-develop.yml new file mode 100644 index 00000000..5e026104 --- /dev/null +++ b/.github/workflows/deploy-golang-develop.yml @@ -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 + diff --git a/.github/workflows/deploy-golang-prod.yml b/.github/workflows/deploy-golang-prod.yml new file mode 100644 index 00000000..889d9f82 --- /dev/null +++ b/.github/workflows/deploy-golang-prod.yml @@ -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/codecov-action@v4.0.1 + 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 + diff --git a/.github/workflows/lint-test-build-golang.yml b/.github/workflows/lint-test-build-golang.yml index 4a4c674c..14594537 100644 --- a/.github/workflows/lint-test-build-golang.yml +++ b/.github/workflows/lint-test-build-golang.yml @@ -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: [ diff --git a/occupi-backend/.dockerignore b/occupi-backend/.dockerignore index bef063d5..8719c5a0 100644 --- a/occupi-backend/.dockerignore +++ b/occupi-backend/.dockerignore @@ -18,4 +18,7 @@ *.tmp *.swp *.gitignore -*.md \ No newline at end of file +*.md +*.out +*.toml +*.exe \ No newline at end of file diff --git a/occupi-backend/docker-compose.dev.yml b/occupi-backend/docker-compose.dev.yml new file mode 100644 index 00000000..8ab5b7ee --- /dev/null +++ b/occupi-backend/docker-compose.dev.yml @@ -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 diff --git a/occupi-backend/docker-compose.prod.yml b/occupi-backend/docker-compose.prod.yml new file mode 100644 index 00000000..145db0fd --- /dev/null +++ b/occupi-backend/docker-compose.prod.yml @@ -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 diff --git a/occupi-backend/docker-compose.traefik.yml b/occupi-backend/docker-compose.traefik.yml new file mode 100644 index 00000000..305a1418 --- /dev/null +++ b/occupi-backend/docker-compose.traefik.yml @@ -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