diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml deleted file mode 100644 index 58c79cde54..0000000000 --- a/.github/workflows/main.yml +++ /dev/null @@ -1,33 +0,0 @@ -name: Publish Docker image - -on: - push: - branches: ['master'] - -jobs: - push_to_registry: - name: Push Docker image to Docker Hub - runs-on: ubuntu-latest - steps: - - name: Check out the repo - uses: actions/checkout@v3 - - - name: Log in to Docker Hub - uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9 - with: - username: ${{ secrets.DOCKER_USERNAME }} - password: ${{ secrets.DOCKER_PASSWORD }} - - - name: Extract metadata (tags, labels) for Docker - id: meta - uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38 - with: - images: thetips4you/testjodejsapp - - - name: Build and push Docker image - uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc - with: - context: . - push: true - tags: ${{ steps.meta.outputs.tags }} - labels: ${{ steps.meta.outputs.labels }} diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml new file mode 100644 index 0000000000..645d96c528 --- /dev/null +++ b/.github/workflows/node.js.yml @@ -0,0 +1,43 @@ +name: Setup Workload Identity Federation + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +jobs: + setup: + runs-on: self-hosted + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Authenticate with Google Cloud + uses: google-github-actions/setup-gcloud@v2 + + - name: Get cluster credentials + run: gcloud container clusters get-credentials testgke-cluster --region asia-south1-a + + - name: Create namespace + run: | + kubectl get namespace test-ns || kubectl create namespace test-ns + + - name: Create ServiceAccount + run: | + kubectl get serviceaccount test-ksa --namespace test-ns || kubectl create serviceaccount test-ksa --namespace test-ns + + - name: Bind IAM policy for GCS bucket creation + run: | + gcloud projects add-iam-policy-binding goldengate-1 \ + --role=roles/storage.objectAdmin \ + --member=serviceAccount:goldengate-1.svc.id.goog[test-ns/test-ksa] \ + --condition=None + + - name: GCS Bucket + run: | + gsutil ls gs://test-innobucket1534 + + - name: Store application in GCS Bucket + run: | + gsutil cp -r /home/mnagaraju/actions-runner/Testproject-INNO gs://test-innobucket1534 diff --git a/README.md b/README.md new file mode 100644 index 0000000000..6d26216298 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +Node.js Application diff --git a/ansible/repo.yaml b/ansible/repo.yaml new file mode 100644 index 0000000000..c18ce6cf90 --- /dev/null +++ b/ansible/repo.yaml @@ -0,0 +1,44 @@ +--- +- name: Git Checkout, Docker Install, Build Image, Push to Artifact Registry, and Deploy to GKE + hosts: localhost + gather_facts: false + + tasks: + - name: Ensure git is installed + become: true + apt: + name: git + state: present + + - name: Ensure Docker dependencies are installed + become: true + apt: + name: "{{ item }}" + state: present + loop: + - containerd + - docker.io + + - name: Clone Node.js code from GitHub + git: + repo: https://github.com/mnagaraju5628/Testproject-INNO.git + dest: /home/ubuntu/actions-runner/Testproject-INNO + version: main + become: true + + - name: Build Docker image + command: "docker build -t us-central1-docker.pkg.dev/goldengate-1/test-project/node:latest /home/ubuntu/actions-runner/Testproject-INNO" + become: true + + - name: Push Docker image to artifact registry + command: "docker push us-central1-docker.pkg.dev/goldengate-1/test-project/node:latest" + become: true + + - name: Deploy to GKE + become: true + shell: | + cd /home/ubuntu/actions-runner/Testproject-INNO/ + gcloud container clusters get-credentials innotest-cluster --region us-central1-c + kubectl rollout restart deployment nodeapp-deployment + kubectl apply -f deployment.yaml + kubectl apply -f service.yaml diff --git a/deployment.yml b/deployment.yaml similarity index 64% rename from deployment.yml rename to deployment.yaml index 8468bbd661..1f42e272e9 100644 --- a/deployment.yml +++ b/deployment.yaml @@ -12,10 +12,14 @@ spec: template: metadata: labels: - app: nodeapp + app: nodeapp spec: containers: - name: nodeserver - image: thetips4you/nodeapp:latest + image: us-central1-docker.pkg.dev/goldengate-1/demogcr-ar/node:latest ports: - containerPort: 3000 + imagePullPolicy: Always + strategy: + type: RollingUpdate + diff --git a/deploymentservice.yml b/deploymentservice.yml deleted file mode 100644 index ab6f0b707d..0000000000 --- a/deploymentservice.yml +++ /dev/null @@ -1,38 +0,0 @@ -apiVersion: apps/v1 -kind: Deployment -metadata: - name: nodeapp-deployment - labels: - app: nodeapp -spec: - replicas: 1 - selector: - matchLabels: - app: nodeapp - template: - metadata: - labels: - app: nodeapp - spec: - containers: - - name: nodeserver - image: thetips4you/nodeapp:latest - ports: - - containerPort: 3000 - - ---- - -apiVersion: v1 -kind: Service -metadata: - name: nodeapp-service -spec: - selector: - app: nodeapp - type: LoadBalancer - ports: - - protocol: TCP - port: 5000 - targetPort: 3000 - nodePort: 31110 diff --git a/dockerfile b/dockerfile index bbb5d90735..4d6e673832 100644 --- a/dockerfile +++ b/dockerfile @@ -1,4 +1,4 @@ -FROM node:latest +FROM node:20-alpine WORKDIR /usr/src/app @@ -9,4 +9,6 @@ RUN npm install COPY . . EXPOSE 4000 + CMD [ "node", "index.js" ] + diff --git a/index.js b/index.js index 668a9f8ed6..5b4e6a50a3 100644 --- a/index.js +++ b/index.js @@ -2,11 +2,11 @@ var express = require('express'); var app = express(); app.get('/', function (req, res) { - res.send('{ "response": "Hello From Thetips4you" }'); + res.send('{ "response": "Welcome to Github-action CI/CD via ansible " }'); }); app.get('/will', function (req, res) { - res.send('{ "response": "Hello World" }'); + res.send('{ "response": "Welcome" }'); }); app.get('/ready', function (req, res) { res.send('{ "response": " Great!, It works!" }'); diff --git a/service.yml b/service.yaml similarity index 81% rename from service.yml rename to service.yaml index 1a0ab51485..d26c2b0f39 100644 --- a/service.yml +++ b/service.yaml @@ -1,3 +1,4 @@ +--- apiVersion: v1 kind: Service metadata: @@ -8,6 +9,6 @@ spec: type: LoadBalancer ports: - protocol: TCP - port: 5000 + port: 8000 targetPort: 3000 - nodePort: 31110 + nodePort: 31000 diff --git a/test/test.js b/test/test.js index 2794ef1f03..bf4f29c332 100644 --- a/test/test.js +++ b/test/test.js @@ -1,7 +1,7 @@ var request = require('supertest'); var app = require('../index.js'); describe('GET /will', function() { - it('respond with hello world', function(done) { - request(app).get('/will').expect('{ "response": "Hello World" }', done); + it('respond with Welcome', function(done) { + request(app).get('/will').expect('{ "response": "Welcome" }', done); }); -}); \ No newline at end of file +});