From 5eaf7e87773b1b5c81189fb2d71ba904ab8b0b34 Mon Sep 17 00:00:00 2001 From: nunorcsousa Date: Sat, 1 May 2021 16:07:04 +0100 Subject: [PATCH 01/13] Create main.yml --- .github/workflows/main.yml | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 .github/workflows/main.yml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 00000000..140665d6 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,34 @@ +# 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" + build: + # 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 + + # Runs a single command using the runners shell + - name: Run a one-line script + run: echo Hello, world! + + # Runs a set of commands using the runners shell + - name: Run a multi-line script + run: | + echo Add other actions to build, + echo test, and deploy your project. From 18514edea7b314b2b38a3ca6a6fdf20aa9a4c694 Mon Sep 17 00:00:00 2001 From: nunorcsousa Date: Sat, 1 May 2021 16:25:30 +0100 Subject: [PATCH 02/13] Update main.yml --- .github/workflows/main.yml | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 140665d6..4d7e23fc 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -14,7 +14,7 @@ on: # 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" - build: + CI: # The type of runner that the job will run on runs-on: ubuntu-latest @@ -23,12 +23,16 @@ jobs: # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it - uses: actions/checkout@v2 - # Runs a single command using the runners shell - - name: Run a one-line script - run: echo Hello, world! - - # Runs a set of commands using the runners shell - - name: Run a multi-line script - run: | - echo Add other actions to build, - echo test, and deploy your project. + - 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-bootcamp-produto:latest + nunorcsousa/api-bootcamp-produto:${{github.run_number}} From 5c90310e02a4999be8c5d3dd03fdfe0edd914b85 Mon Sep 17 00:00:00 2001 From: nunorcsousa Date: Sat, 1 May 2021 16:35:15 +0100 Subject: [PATCH 03/13] Update main.yml --- .github/workflows/main.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 4d7e23fc..9687207b 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -22,6 +22,16 @@ jobs: 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. From 2090e066d56a0f470859bbb8b21bb531482011ce Mon Sep 17 00:00:00 2001 From: nunorcsousa Date: Sat, 1 May 2021 16:40:54 +0100 Subject: [PATCH 04/13] Update main.yml --- .github/workflows/main.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 9687207b..6b7311e0 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -44,5 +44,5 @@ jobs: # List of metadata for an image push: true tags: - nunorcsousa/api-bootcamp-produto:latest - nunorcsousa/api-bootcamp-produto:${{github.run_number}} + nunorcsousa/api-produto:latest + nunorcsousa/api-produto:${{github.run_number}} From 665cfb777a5fd0c05a81d073154fcaccd80f045f Mon Sep 17 00:00:00 2001 From: nunorcsousa Date: Sat, 1 May 2021 17:26:14 +0100 Subject: [PATCH 05/13] Update main.yml --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 6b7311e0..bfcd9faa 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -43,6 +43,6 @@ jobs: file: ./src/Dockerfile # List of metadata for an image push: true - tags: + tags: | nunorcsousa/api-produto:latest nunorcsousa/api-produto:${{github.run_number}} From 103fa9028c5365f3de8fe1bf02328d18d109a800 Mon Sep 17 00:00:00 2001 From: nunorcsousa Date: Sat, 1 May 2021 17:32:00 +0100 Subject: [PATCH 06/13] Add files via upload --- src/Dockerfile | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/Dockerfile 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"] From 19ea8f8570f8f62dc3dc6f34df135734d2f3ebc6 Mon Sep 17 00:00:00 2001 From: nunorcsousa Date: Sat, 1 May 2021 20:47:48 +0100 Subject: [PATCH 07/13] Update main.yml --- .github/workflows/main.yml | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index bfcd9faa..ac6f09ba 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -46,3 +46,23 @@ jobs: 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 From d85e4d2bf885613f5f5a132a29f414ce054d5dc3 Mon Sep 17 00:00:00 2001 From: nunorcsousa Date: Sat, 1 May 2021 20:58:43 +0100 Subject: [PATCH 08/13] Create deployment.yaml --- k8s/api/deployment.yaml | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 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 From df7dd86ad2572c2a1b88333450b706c81654aaab Mon Sep 17 00:00:00 2001 From: nunorcsousa Date: Sat, 1 May 2021 21:00:20 +0100 Subject: [PATCH 09/13] Create deployment.yaml --- k8s/mongodb/deployment.yaml | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 k8s/mongodb/deployment.yaml 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 From 750bf11fd4d646e4395ae7202e7389a1c7565c6d Mon Sep 17 00:00:00 2001 From: nunorcsousa Date: Sat, 1 May 2021 21:01:07 +0100 Subject: [PATCH 10/13] Create service.yaml --- k8s/mongodb/service.yaml | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 k8s/mongodb/service.yaml 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 From 4959f6c5122371279e45eb81812194577a8accf8 Mon Sep 17 00:00:00 2001 From: nunorcsousa Date: Sat, 1 May 2021 21:08:27 +0100 Subject: [PATCH 11/13] Update main.yml --- .github/workflows/main.yml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index ac6f09ba..b4d20583 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -60,9 +60,16 @@ jobs: 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: + manifests: | + k8s/api/deployment.yaml From 03160cf6b2c52e872f8666e217022bbaa333c549 Mon Sep 17 00:00:00 2001 From: nunorcsousa Date: Sat, 1 May 2021 21:40:53 +0100 Subject: [PATCH 12/13] Update main.yml --- .github/workflows/main.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b4d20583..03fb28f4 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -71,5 +71,6 @@ jobs: - name: Deploy WebAPI uses: Azure/k8s-deploy@v1.3 with: + images: nunorcsousa/api-produto:${{github.run_number}} manifests: | k8s/api/deployment.yaml From 3f17b369aa7845a255134457077ad1b6ed912dc7 Mon Sep 17 00:00:00 2001 From: nunorcsousa Date: Sat, 1 May 2021 21:52:54 +0100 Subject: [PATCH 13/13] Add files via upload --- src/controllers/produto.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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); })