Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ci cd workflow #23

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
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
152 changes: 152 additions & 0 deletions .github/workflows/auth-api-workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
name: Auth API Workflow

on:
push:
branches: [ "ci_cd_workflow" ]
paths:
- 'auth-api/**'

env:
REGISTRY: 859047589462.dkr.ecr.us-east-1.amazonaws.com
DOCKER_REPOSITORY: sample-microsvc-app-auth-api
CLUSTER_REPO: https://github.com/CelestialSystem/mcs-poc-001-k8s.git


permissions:
contents: read
packages: write
id-token: write
security-events: write
jobs:

getBranch_and_commitHash:
runs-on: ubuntu-latest

outputs:
output1: ${{ steps.branch.outputs.git_branch }}
output2: ${{ steps.hash.outputs.git_hash }}
steps:
- name: Get Branch
id: branch
run: echo "::set-output name=git_branch::${GITHUB_REF#refs/heads/}"

- name: Get Hash
id: hash
run: echo "::set-output name=git_hash::$(echo $GITHUB_SHA | cut -c1-7)"

build_and_push:

runs-on: ubuntu-latest
needs: getBranch_and_commitHash

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1


- name: Login to AWS ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Build the Docker image
working-directory: auth-api
run: docker build . --file Dockerfile --tag $REGISTRY/$DOCKER_REPOSITORY:${{needs.getBranch_and_commitHash.outputs.output1}}-${{needs.getBranch_and_commitHash.outputs.output2}}

- name: Push image to Amazon ECR
run: |
docker push $REGISTRY/$DOCKER_REPOSITORY:${{needs.getBranch_and_commitHash.outputs.output1}}-${{needs.getBranch_and_commitHash.outputs.output2}}

testDockerImage:
runs-on: ubuntu-latest
needs: [getBranch_and_commitHash, build_and_push]

steps:

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1

- name: Login to AWS ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Pull Docker Image
run: |
docker pull ${{ env.REGISTRY }}/${{ env.DOCKER_REPOSITORY }}:${{needs.getBranch_and_commitHash.outputs.output1}}-${{needs.getBranch_and_commitHash.outputs.output2}}
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@master
with:
image-ref: ${{ env.REGISTRY }}/${{ env.DOCKER_REPOSITORY }}:${{needs.getBranch_and_commitHash.outputs.output1}}-${{needs.getBranch_and_commitHash.outputs.output2}}
format: 'sarif'
output: 'trivy-results-docker.sarif'
severity: 'CRITICAL,HIGH'

- name: Debug output
run: |
echo "#########TRIVY################"
cat trivy-results-docker.sarif

- name: Upload Trivy scan results to Github security tab
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: 'trivy-results-docker.sarif'


deployToDevEnv:

runs-on: ubuntu-latest
needs: [getBranch_and_commitHash, build_and_push, testDockerImage]
env:
GH_USER: div1127
CLUSTER_REPO: CelestialSystem/mcs-poc-001-k8s.git
GIT_USER_EMAIL: d.srivastav@celestialsys.com
GIT_USER_NAME: Divyansh Srivastav
ARGOCD_APP: auth-api-service
REPO_CONTEXT: /home/runner/work/mcs-poc-001-k8s
SVC_CONTEXT: /home/runner/work/mcs-poc-001-k8s/k8s/auth-api


steps:

- name: Install yq (YAML Processor) & Git
run: |
sudo add-apt-repository ppa:rmescandon/yq
sudo apt update
sudo apt install yq git -y
- name: Install ArgoCD CLI
run: |
curl -sSL -o argocd-linux-amd64 https://github.com/argoproj/argo-cd/releases/latest/download/argocd-linux-amd64
sudo install -m 555 argocd-linux-amd64 /usr/local/bin/argocd
rm argocd-linux-amd64
- name: GitOps - Update deployment repository with new Image Version
uses: nick-fields/retry@v2
with:
timeout_minutes: 10
max_attempts: 4
retry_on: error
command: |
cd /home/runner/work
git clone "https://${{ env.GH_USER }}:${{ secrets.GH_TOKEN }}@github.com/${{ env.CLUSTER_REPO }}"
cd ${{ env.SVC_CONTEXT }}
yq eval '.spec.template.spec.containers[0].image = "${{ env.REGISTRY }}/${{ env.DOCKER_REPOSITORY }}:${{needs.getBranch_and_commitHash.outputs.output1}}-${{needs.getBranch_and_commitHash.outputs.output2}}"' -i deployment.yaml
cd ${{ env.REPO_CONTEXT }}
git config --global user.email ${{ env.GIT_USER_EMAIL }}
git config --global user.name ${{ env.GIT_USER_NAME }}
git add ${{ env.SVC_CONTEXT}}/deployment.yaml
git commit -m "Updated Image Version."
git push

- name: Synk ArgoCD app
run: |
argocd login ${{ secrets.ARGOCD_SERVER }} --username ${{ secrets.ARGOCD_USER }} --password ${{ secrets.ARGOCD_PASSWORD }} --insecure --grpc-web
argocd app sync ${{ env.ARGOCD_APP }} --grpc-web
76 changes: 76 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"

on:
push:
branches: [ "master" ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ "master" ]
schedule:
- cron: '35 0 * * 1'

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [ 'go', 'java', 'javascript', 'python' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
# Use only 'java' to analyze code written in Java, Kotlin or both
# Use only 'javascript' to analyze code written in JavaScript, TypeScript or both
# Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support

steps:
- name: Checkout repository
uses: actions/checkout@v3

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.

# Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
# queries: security-extended,security-and-quality


# Autobuild attempts to build any compiled languages (C/C++, C#, Go, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun

# If the Autobuild fails above, remove it and uncomment the following three lines.
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.

# - run: |
# echo "Run, Build Application using script"
# ./location_of_script_within_repo/buildscript.sh

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
with:
category: "/language:${{matrix.language}}"
152 changes: 152 additions & 0 deletions .github/workflows/frontend-workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
name: Auth API Workflow

on:
push:
branches: [ "ci_cd_workflow" ]
paths:
- 'frontend/**'

env:
REGISTRY: 859047589462.dkr.ecr.us-east-1.amazonaws.com
DOCKER_REPOSITORY: sample-microsvc-app-frontend
CLUSTER_REPO: https://github.com/CelestialSystem/mcs-poc-001-k8s.git


permissions:
contents: read
packages: write
id-token: write
security-events: write
jobs:

getBranch_and_commitHash:
runs-on: ubuntu-latest

outputs:
output1: ${{ steps.branch.outputs.git_branch }}
output2: ${{ steps.hash.outputs.git_hash }}
steps:
- name: Get Branch
id: branch
run: echo "::set-output name=git_branch::${GITHUB_REF#refs/heads/}"

- name: Get Hash
id: hash
run: echo "::set-output name=git_hash::$(echo $GITHUB_SHA | cut -c1-7)"

build_and_push:

runs-on: ubuntu-latest
needs: getBranch_and_commitHash

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1


- name: Login to AWS ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Build the Docker image
working-directory: frontend
run: docker build . --file Dockerfile --tag $REGISTRY/$DOCKER_REPOSITORY:${{needs.getBranch_and_commitHash.outputs.output1}}-${{needs.getBranch_and_commitHash.outputs.output2}}

- name: Push image to Amazon ECR
run: |
docker push $REGISTRY/$DOCKER_REPOSITORY:${{needs.getBranch_and_commitHash.outputs.output1}}-${{needs.getBranch_and_commitHash.outputs.output2}}

testDockerImage:
runs-on: ubuntu-latest
needs: [getBranch_and_commitHash, build_and_push]

steps:

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1

- name: Login to AWS ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Pull Docker Image
run: |
docker pull ${{ env.REGISTRY }}/${{ env.DOCKER_REPOSITORY }}:${{needs.getBranch_and_commitHash.outputs.output1}}-${{needs.getBranch_and_commitHash.outputs.output2}}
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@master
with:
image-ref: ${{ env.REGISTRY }}/${{ env.DOCKER_REPOSITORY }}:${{needs.getBranch_and_commitHash.outputs.output1}}-${{needs.getBranch_and_commitHash.outputs.output2}}
format: 'sarif'
output: 'trivy-results-docker.sarif'
severity: 'CRITICAL,HIGH'

- name: Debug output
run: |
echo "#########TRIVY################"
cat trivy-results-docker.sarif

- name: Upload Trivy scan results to Github security tab
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: 'trivy-results-docker.sarif'


deployToDevEnv:

runs-on: ubuntu-latest
needs: [getBranch_and_commitHash, build_and_push, testDockerImage]
env:
GH_USER: div1127
CLUSTER_REPO: CelestialSystem/mcs-poc-001-k8s.git
GIT_USER_EMAIL: d.srivastav@celestialsys.com
GIT_USER_NAME: Divyansh Srivastav
ARGOCD_APP: frontend-app
REPO_CONTEXT: /home/runner/work/mcs-poc-001-k8s
SVC_CONTEXT: /home/runner/work/mcs-poc-001-k8s/k8s/frontend


steps:

- name: Install yq (YAML Processor) & Git
run: |
sudo add-apt-repository ppa:rmescandon/yq
sudo apt update
sudo apt install yq git -y
- name: Install ArgoCD CLI
run: |
curl -sSL -o argocd-linux-amd64 https://github.com/argoproj/argo-cd/releases/latest/download/argocd-linux-amd64
sudo install -m 555 argocd-linux-amd64 /usr/local/bin/argocd
rm argocd-linux-amd64
- name: GitOps - Update deployment repository with new Image Version
uses: nick-fields/retry@v2
with:
timeout_minutes: 10
max_attempts: 4
retry_on: error
command: |
cd /home/runner/work
git clone "https://${{ env.GH_USER }}:${{ secrets.GH_TOKEN }}@github.com/${{ env.CLUSTER_REPO }}"
cd ${{ env.SVC_CONTEXT }}
yq eval '.spec.template.spec.containers[0].image = "${{ env.REGISTRY }}/${{ env.DOCKER_REPOSITORY }}:${{needs.getBranch_and_commitHash.outputs.output1}}-${{needs.getBranch_and_commitHash.outputs.output2}}"' -i deployment.yaml
cd ${{ env.REPO_CONTEXT }}
git config --global user.email ${{ env.GIT_USER_EMAIL }}
git config --global user.name ${{ env.GIT_USER_NAME }}
git add ${{ env.SVC_CONTEXT}}/deployment.yaml
git commit -m "Updated Image Version."
git push

- name: Synk ArgoCD app
run: |
argocd login ${{ secrets.ARGOCD_SERVER }} --username ${{ secrets.ARGOCD_USER }} --password ${{ secrets.ARGOCD_PASSWORD }} --insecure --grpc-web
argocd app sync ${{ env.ARGOCD_APP }} --grpc-web
Loading