Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# This is a basic workflow to help you get started with Actions

name: CI-CD

# Controls when the action will run.
on:
# Triggers the workflow on push or pull request events but only for the master branch
push:
branches: [ master ]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
CI:
# The type of runner that the job will run on
runs-on: ubuntu-latest

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2

- name: Docker Login
# You may pin to the exact commit or the version.
# uses: docker/login-action@28218f9b04b4f3f62068d7b6ce6ca5b26e35336c
uses: docker/login-action@v1.9.0
with:
# Username used to log against the Docker registry
username: ${{secrets.DOCKERHUB_USER}}
# Password or personal access token used to log against the Docker registry
password: ${{secrets.DOCKERHUB_PWD}}

- name: Build and push Docker images
# You may pin to the exact commit or the version.
# uses: docker/build-push-action@e1b7f96249f2e4c8e4ac1519b9608c0d48944a1f
uses: docker/build-push-action@v2.4.0
with:
context: ./src
# Path to the Dockerfile
file: ./src/Dockerfile
# List of metadata for an image
push: true
tags: |
nunorcsousa/api-produto:latest
nunorcsousa/api-produto:${{github.run_number}}

CD:
# The type of runner that the job will run on
runs-on: ubuntu-latest
needs: [CI]
steps:
- uses: actions/checkout@v2
- name: Kubernetes set context
uses: Azure/k8s-set-context@v1
with:
# Acceptable values: kubeconfig or service-account
method: kubeconfig
# Kubernetes Config
kubeconfig: ${{secrets.K8S_CONFIG}}

- name: Deploy MongoDB
uses: Azure/k8s-deploy@v1.3
with:
manifests: |
k8s/mongodb/deployment.yaml
k8s/mongodb/service.yaml

- name: Deploy WebAPI
uses: Azure/k8s-deploy@v1.3
with:
images: nunorcsousa/api-produto:${{github.run_number}}
manifests: |
k8s/api/deployment.yaml
37 changes: 37 additions & 0 deletions k8s/api/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: api
spec:
replicas: 3
selector:
matchLabels:
app: api
template:
metadata:
labels:
app: api
spec:
containers:
- name: api
image: nunorcsousa/api-bootcamp-produto:v2
ports:
- containerPort: 8080
env:
- name: MONGODB_URI
value: mongodb://mongouser:mongopwd@mongodb-service:27017/admin

---

apiVersion: v1
kind: Service
metadata:
name: api-service
spec:
selector:
app: api
ports:
- port: 80
targetPort: 8080
nodePort: 30000
type: LoadBalancer
23 changes: 23 additions & 0 deletions k8s/mongodb/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: mongodb
spec:
selector:
matchLabels:
app: mongodb
template:
metadata:
labels:
app: mongodb
spec:
containers:
- name: mongodb
image: mongo:4.4.5
ports:
- containerPort: 27017
env:
- name: MONGO_INITDB_ROOT_USERNAME
value: mongouser
- name: MONGO_INITDB_ROOT_PASSWORD
value: mongopwd
11 changes: 11 additions & 0 deletions k8s/mongodb/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: v1
kind: Service
metadata:
name: mongodb-service
spec:
selector:
app: mongodb
ports:
- port: 27017
targetPort: 27017
type: ClusterIP
7 changes: 7 additions & 0 deletions src/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM node:14.16.1-alpine3.13
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD ["node", "app.js"]
2 changes: 1 addition & 1 deletion src/controllers/produto.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ exports.productAll = function (req, res) {
return res.json(err);
}

var retu = { product, machine: os.hostname() };
var retu = { product, machine: os.hostname(), version: "3.0" };

res.json(retu);
})
Expand Down