diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 00000000..03fb28f4 --- /dev/null +++ b/.github/workflows/main.yml @@ -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 diff --git a/k8s/api/deployment.yaml b/k8s/api/deployment.yaml new file mode 100644 index 00000000..313efadf --- /dev/null +++ b/k8s/api/deployment.yaml @@ -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 diff --git a/k8s/mongodb/deployment.yaml b/k8s/mongodb/deployment.yaml new file mode 100644 index 00000000..c6c83f04 --- /dev/null +++ b/k8s/mongodb/deployment.yaml @@ -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 diff --git a/k8s/mongodb/service.yaml b/k8s/mongodb/service.yaml new file mode 100644 index 00000000..dc4aa61e --- /dev/null +++ b/k8s/mongodb/service.yaml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: Service +metadata: + name: mongodb-service +spec: + selector: + app: mongodb + ports: + - port: 27017 + targetPort: 27017 + type: ClusterIP diff --git a/src/Dockerfile b/src/Dockerfile new file mode 100644 index 00000000..93e71159 --- /dev/null +++ b/src/Dockerfile @@ -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"] diff --git a/src/controllers/produto.js b/src/controllers/produto.js index 7d24ee5e..65176639 100644 --- a/src/controllers/produto.js +++ b/src/controllers/produto.js @@ -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); })