Created
April 13, 2021 14:19
-
-
Save r4rohan/fc39f69b83fb18b92e7e3f72a3173618 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# enter Google Cloud Platform Project ID in environment section and update GitHub URL in git clone stage | |
pipeline { | |
agent { | |
kubernetes { | |
defaultContainer 'jnlp' | |
yaml """ | |
apiVersion: v1 | |
kind: Pod | |
metadata: | |
labels: | |
run: jnlp | |
spec: | |
containers: | |
- name: git | |
image: gcr.io/cloud-builders/git | |
command: | |
- cat | |
tty: true | |
- name: python-382 | |
image: registry.hub.docker.com/library/python:3.8.2 | |
command: | |
- cat | |
tty: true | |
- name: busybox | |
image: busybox | |
command: | |
- cat | |
tty: true | |
- name: gcloud | |
image: google/cloud-sdk:latest | |
command: | |
- cat | |
tty: true | |
volumeMounts: | |
- name: kaniko-secret | |
mountPath: /secret | |
env: | |
- name: GOOGLE_APPLICATION_CREDENTIALS | |
value: /secret/kaniko-secret.json | |
- name: kaniko | |
image: gcr.io/kaniko-project/executor:debug | |
command: | |
- /busybox/cat | |
tty: true | |
volumeMounts: | |
- name: kaniko-secret | |
mountPath: /secret | |
env: | |
- name: GOOGLE_APPLICATION_CREDENTIALS | |
value: /secret/kaniko-secret.json | |
volumes: | |
- name: kaniko-secret | |
secret: | |
secretName: kaniko-secret | |
imagePullSecrets: | |
- name: jenkins-image-pull-secret | |
""" | |
} | |
} | |
environment { | |
GCP_PROJECT_ID = "enter-your-GCP-Project-ID" | |
} | |
stages { | |
stage ('git clone - main') { | |
when { | |
branch 'main' | |
} | |
steps{ | |
container('git'){ | |
git branch: 'main', | |
credentialsId: 'Github-Account-Credentials', | |
url: 'enter-GitHub-repo' | |
} | |
} | |
} | |
stage ('git clone - prod') { | |
when { | |
branch 'prod' | |
} | |
steps{ | |
container('git'){ | |
git branch: 'prod', | |
credentialsId: 'Github-Account-Credentials', | |
url: 'enter-GitHub-repo' | |
} | |
} | |
} | |
stage ('building docker image - main') { | |
when { | |
branch 'main' | |
} | |
steps { | |
dir ('omegatrade-app/backend') { | |
container(name: 'kaniko', shell: '/busybox/sh') { | |
sh 'pwd' | |
sh """ | |
#!/busybox/sh | |
/kaniko/executor --dockerfile dockerfile.local.emulator --context `pwd`/ --verbosity debug --insecure --skip-tls-verify --destination gcr.io/${GCP_PROJECT_ID}/omega-trade/dev/backend:$BUILD_NUMBER --destination gcr.io/${GCP_PROJECT_ID}/omega-trade/dev/backend:latest | |
""" | |
} | |
} | |
} | |
} | |
stage ('building docker image - prod') { | |
when { | |
branch 'prod' | |
} | |
steps { | |
dir ('omegatrade-app/backend') { | |
container(name: 'kaniko', shell: '/busybox/sh') { | |
sh 'pwd' | |
sh """ | |
#!/busybox/sh | |
/kaniko/executor --dockerfile dockerfile.prod --context `pwd`/ --verbosity debug --insecure --skip-tls-verify --destination gcr.io/${GCP_PROJECT_ID}/omega-trade/prod/backend:$BUILD_NUMBER --destination gcr.io/${GCP_PROJECT_ID}/omega-trade/prod/backend:latest | |
""" | |
} | |
} | |
} | |
} | |
stage ('deploy to cloud run - main') { | |
when { | |
branch 'main' | |
} | |
steps { | |
container('gcloud'){ | |
sh 'kubectl version' | |
sh 'gcloud auth activate-service-account --key-file=/secret/kaniko-secret.json' | |
sh 'gcloud run deploy omegatrade-backend-dev-jenkins --image=gcr.io/${GCP_PROJECT_ID}/omega-trade/dev/backend:$BUILD_NUMBER --platform=managed --region=us-west1 --allow-unauthenticated --project=${GCP_PROJECT_ID} --port 9020' | |
} | |
} | |
} | |
stage ('deploy to cloud run - prod') { | |
when { | |
branch 'prod' | |
} | |
steps { | |
container('gcloud'){ | |
sh 'kubectl version' | |
sh 'gcloud auth activate-service-account --key-file=/secret/kaniko-secret.json' | |
sh 'gcloud run deploy omegatrade-backend-prod-jenkins --image=gcr.io/${GCP_PROJECT_ID}/omega-trade/prod/backend:$BUILD_NUMBER --platform=managed --region=us-west1 --allow-unauthenticated --project=${GCP_PROJECT_ID} --port 9020 --set-env-vars=PROJECTID=${GCP_PROJECT_ID} --set-env-vars=INSTANCE=spanner_id --set-env-vars=DATABASE=spanner_database --set-env-vars=JWT_SECRET=abc123 --set-env-vars=EXPIRE_IN=2d' | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment