Skip to content

Commit 04e17ee

Browse files
authored
Merge pull request #2 from loginwashere/GH-1
GH-1 Configure build deploy
2 parents b609daf + 2d7d178 commit 04e17ee

39 files changed

+889
-55
lines changed

.sample.env

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
POSTGRES_DB=demo
2-
POSTGRES_USER=demouser
3-
POSTGRES_PASSWORD=qwerty
1+
POSTGRES_DB=postgres
2+
POSTGRES_USER=postgres
3+
POSTGRES_PASSWORD=
4+
POSTGRES_PORT=5432

.semaphore/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*-secret.yml

.semaphore/client-deploy-build.yml

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Use the latest stable version of Semaphore 2.0 YML syntax:
2+
version: v1.0
3+
4+
# Name your pipeline. In case you connect multiple pipelines with promotions,
5+
# the name will help you differentiate between, for example, a CI build phase
6+
# and delivery phases.
7+
name: Semaphore JavaScript Example Pipeline
8+
9+
# An agent defines the environment in which your code runs.
10+
# It is a combination of one of available machine types and operating
11+
# system images.
12+
# See https://docs.semaphoreci.com/article/20-machine-types
13+
# and https://docs.semaphoreci.com/article/32-ubuntu-1804-image
14+
agent:
15+
machine:
16+
type: e1-standard-2
17+
os_image: ubuntu1804
18+
19+
# Blocks are the heart of a pipeline and are executed sequentially.
20+
# Each block has a task that defines one or more jobs. Jobs define the
21+
# commands to execute.
22+
# See https://docs.semaphoreci.com/article/62-concepts
23+
blocks:
24+
- name: Install dependencies
25+
task:
26+
# Set environment variables that your project requires.
27+
# See https://docs.semaphoreci.com/article/66-environment-variables-and-secrets
28+
env_vars:
29+
- name: NODE_ENV
30+
value: production
31+
- name: CI
32+
value: 'true'
33+
secrets:
34+
- name: client-production-env-secret
35+
# This block runs two jobs in parallel and they both share common
36+
# setup steps. We can group them in a prologue.
37+
# See https://docs.semaphoreci.com/article/50-pipeline-yaml#prologue
38+
prologue:
39+
commands:
40+
# Get the latest version of our source code from GitHub:
41+
- checkout
42+
43+
# Use the version of Node.js specified in .nvmrc.
44+
# Semaphore provides nvm preinstalled.
45+
- nvm use
46+
- node --version
47+
- npm --version
48+
jobs:
49+
# First parallel job:
50+
- name: client npm install and cache
51+
commands:
52+
- cd src/client
53+
54+
# Copy production configs we linked using secrets
55+
- cp /home/semaphore/client-production.env .env
56+
57+
# Restore dependencies from cache.
58+
# For more info on caching, see https://docs.semaphoreci.com/article/68-caching-dependencies
59+
- cache restore client-node-modules-$SEMAPHORE_GIT_BRANCH-$(checksum package-lock.json),client-node-modules-$SEMAPHORE_GIT_BRANCH,client-node-modules-master
60+
61+
- npm run build
62+
63+
# Store the latest version of client build in cache to reuse in further blocks:
64+
- cache store client-build-$SEMAPHORE_WORKFLOW_ID build
65+
66+
# The deployment pipeline is defined to run on manual approval from the UI.
67+
# Semaphore will the time and the name of the person who promotes each
68+
# deployment.
69+
#
70+
# You could, for example, add another promotion to a pipeline that
71+
# automatically deploys to a staging environment from branches named
72+
# after a certain pattern.
73+
# https://docs.semaphoreci.com/article/50-pipeline-yaml#promotions
74+
promotions:
75+
- name: Deploy Client
76+
pipeline_file: client-deploy.yml
77+
auto_promote_on:
78+
- result: passed

.semaphore/client-deploy.yml

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# This pipeline runs after semaphore.yml
2+
version: v1.0
3+
name: Client deploy
4+
agent:
5+
machine:
6+
# Use a machine type with more RAM and CPU power for faster container
7+
# builds:
8+
type: e1-standard-4
9+
os_image: ubuntu1804
10+
blocks:
11+
- name: Build
12+
task:
13+
# Mount a secret which defines DOCKER_USERNAME and DOCKER_PASSWORD
14+
# environment variables.
15+
# For info on creating secrets, see:
16+
# https://docs.semaphoreci.com/article/66-environment-variables-and-secrets
17+
secrets:
18+
- name: gh-pages-secret
19+
prologue:
20+
commands:
21+
# Get the latest version of our source code from GitHub:
22+
- checkout --use-cache
23+
jobs:
24+
- name: Deploy to GitHub Pages
25+
commands:
26+
- cd src/client
27+
28+
# Restore dependencies from cache.
29+
# For more info on caching, see https://docs.semaphoreci.com/article/68-caching-dependencies
30+
- cache restore client-node-modules-$SEMAPHORE_GIT_BRANCH-$(checksum package-lock.json),client-node-modules-$SEMAPHORE_GIT_BRANCH,client-node-modules-master
31+
32+
# Restore build from cache.
33+
- cache restore client-build-$SEMAPHORE_WORKFLOW_ID
34+
35+
# Config ssh and git to push new build files into gh-pages branch of app repository
36+
- ssh-keyscan -H github.com >> ~/.ssh/known_hosts
37+
- chmod 600 ~/.ssh/id_rsa_semaphoreci_ghpages_deploy
38+
- ssh-add ~/.ssh/id_rsa_semaphoreci_ghpages_deploy
39+
- git config --global url."[email protected]:".insteadOf "https://github.com/"
40+
- git config --global user.name '$GH_USERNAME'
41+
- git config --global user.email '$GH_EMAIL'
42+
43+
- npm run deploy
44+
45+
# Restore change to global git repository url rewrite to previous value
46+
- git config --global url."https://github.com/".insteadOf "[email protected]:"

.semaphore/secrets/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*-secret.yml
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copy this file into one without .sample part and then populate it with actual values.
2+
# Then you can create secret, by using command
3+
# `sem create -f path/to/this/file`
4+
# More info https://docs.semaphoreci.com/article/66-environment-variables-and-secrets
5+
apiVersion: v1alpha
6+
kind: Secret
7+
metadata:
8+
# Use this name to create this secret manually
9+
name: client-production-env-secret
10+
data:
11+
files:
12+
# Client production.env file doesn't exist by default, copy src/client/sample.env into src/client/production.env
13+
# and populate with production values
14+
# Then create secret - in the end it should be here - https://<put-your-namespace-here>.semaphoreci.com/secrets
15+
- path: client-production.env
16+
# Could be created by
17+
# - `base64 -w 0 /path/to/file` and put in
18+
# - upload in https://<your-namesace>.semaphoreci.com/secrets
19+
content: PASTE_BASE64_ENCODED_CONTENT_HERE
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copy this file into one without .sample part and then populate it with actual values.
2+
# Then you can create secret, by using command
3+
# `sem create -f path/to/this/file`
4+
# More info https://docs.semaphoreci.com/article/66-environment-variables-and-secrets
5+
apiVersion: v1alpha
6+
kind: Secret
7+
metadata:
8+
# Use this name to create this secret manually
9+
name: gc-k8s-secret
10+
data:
11+
files:
12+
# If you haven't set up k8s on google you need to do it first
13+
# Start with https://cloud.google.com/kubernetes-engine/docs/quickstart
14+
# Play with it and then you can use it with this project.
15+
# Also check out https://docs.semaphoreci.com/article/119-ci-cd-for-microservices-on-kubernetes
16+
# as example of k8s deploy.
17+
# If you have admin of dev ops - contact them to obtain this config
18+
# Also you could try to get this yourself in case you have permissions
19+
# Run command
20+
# `gcloud container clusters get-credentials [CLUSTER_NAME]`
21+
# Then look for `.kube` folder and find config.yaml there.
22+
- path: .kube/gc-k8s.yaml
23+
# Could be created by
24+
# - `base64 -w 0 /path/to/file` and put in
25+
# - upload in https://<your-namesace>.semaphoreci.com/secrets
26+
content: PASTE_BASE64_ENCODED_CONTENT_HERE
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copy this file into one without .sample part and then populate it with actual values.
2+
# Then you can create secret, by using command
3+
# `sem create -f path/to/this/file`
4+
# More info https://docs.semaphoreci.com/article/66-environment-variables-and-secrets
5+
apiVersion: v1alpha
6+
kind: Secret
7+
metadata:
8+
# Use this name to create this secret manually
9+
name: gcr-secret
10+
data:
11+
# If you haven't set up k8s on google you need to do it first
12+
# Start with https://cloud.google.com/kubernetes-engine/docs/quickstart
13+
# Play with it and then you can use it with this project.
14+
# Check out https://docs.semaphoreci.com/article/72-google-container-registry-gcr as an example.
15+
# Also check out https://docs.semaphoreci.com/article/119-ci-cd-for-microservices-on-kubernetes
16+
# as example of k8s deploy.
17+
env_vars:
18+
# Id of your project
19+
# More info here https://cloud.google.com/resource-manager/docs/creating-managing-projects?visit_id=636878590586351739-3388570778&rd=1#identifying_projects
20+
- name: GCP_PROJECT_ID
21+
value: "your-gcp-project-id"
22+
# Default compute zone you've selected
23+
# https://cloud.google.com/compute/docs/regions-zones/#available
24+
- name: GCP_PROJECT_DEFAULT_ZONE
25+
value: "europe-west1-b"
26+
files:
27+
# You need to create service account and export json key file for it here https://console.cloud.google.com/iam-admin/serviceaccounts
28+
# to use in this file.
29+
- path: .secrets.gcp.json
30+
# Could be created by
31+
# - `base64 -w 0 /path/to/file` and put in
32+
# - upload in https://<your-namesace>.semaphoreci.com/secrets
33+
content: PASTE_BASE64_ENCODED_CONTENT_HERE
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copy this file into one without .sample part and then populate it with actual values.
2+
# Then you can create secret, by using command
3+
# `sem create -f path/to/this/file`
4+
# More info https://docs.semaphoreci.com/article/66-environment-variables-and-secrets
5+
apiVersion: v1beta
6+
kind: Secret
7+
metadata:
8+
# Use this name to create this secret manually
9+
name: gh-pages-secret
10+
data:
11+
env_vars:
12+
# Username of github user used for deploy
13+
- name: GH_USERNAME
14+
value: "super secret github username"
15+
# Email of github user used for deploy
16+
- name: GH_EMAIL
17+
value: "super secret github email"
18+
files:
19+
# Generate ssh key https://help.github.com/en/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
20+
# Add it to account you will use to deploy to github here https://github.com/settings/keys
21+
- path: .ssh/id_rsa_semaphoreci_ghpages_deploy
22+
# Could be created by
23+
# - `base64 -w 0 /path/to/file` and put in
24+
# - upload in https://<your-namesace>.semaphoreci.com/secrets
25+
content: PASTE_BASE64_ENCODED_CONTENT_HERE
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copy this file into one without .sample part and then populate it with actual values.
2+
# Then you can create secret, by using command
3+
# `sem create -f path/to/this/file`
4+
# More info https://docs.semaphoreci.com/article/66-environment-variables-and-secrets
5+
apiVersion: v1beta
6+
kind: Secret
7+
metadata:
8+
# Use this name to create this secret manually
9+
name: server-ormconfig-production
10+
data:
11+
files:
12+
# Copy src/server/ormconfig.sample.json into ormconfig.production.json and populate with production values
13+
# You will need production connection settings in this config file.
14+
# In this eample heroku postgres database addon is used.
15+
# You will need to create heroku app and then add addon https://elements.heroku.com/addons/heroku-postgresql
16+
# Then you will be able to view database connection settings like described here https://devcenter.heroku.com/articles/heroku-postgresql#external-connections-ingress
17+
- path: ormconfig.production.json
18+
# Could be created by
19+
# - `base64 -w 0 /path/to/file` and put in
20+
# - upload in https://<your-namesace>.semaphoreci.com/secrets
21+
content: PASTE_BASE64_ENCODED_CONTENT_HERE
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copy this file into one without .sample part and then populate it with actual values.
2+
# Then you can create secret, by using command
3+
# `sem create -f path/to/this/file`
4+
# More info https://docs.semaphoreci.com/article/66-environment-variables-and-secrets
5+
apiVersion: v1beta
6+
kind: Secret
7+
metadata:
8+
# Use this name to create this secret manually
9+
name: server-production-env
10+
data:
11+
files:
12+
# Server production.env file doesn't exist by default, copy src/server/sample.env into src/server/production.env
13+
# and populate with production values
14+
# Then create secret - in the end it should be here - https://<put-your-namespace-here>.semaphoreci.com/secrets
15+
- path: server-production.env
16+
# Could be created by
17+
# - `base64 -w 0 /path/to/file` and put in
18+
# - upload in https://<your-namesace>.semaphoreci.com/secrets
19+
content: PASTE_BASE64_ENCODED_CONTENT_HERE

.semaphore/semaphore.yml

+20-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,26 @@ blocks:
153153
commands:
154154
- cd src/server
155155
- cache restore server-node-modules-$SEMAPHORE_GIT_BRANCH-$(checksum package-lock.json),server-node-modules-$SEMAPHORE_GIT_BRANCH,server-node-modules-master
156-
- cp ci.env .env
157156
- cp ormconfig.ci.json ormconfig.json
158157
- npm run migrate:up
159158
- npm run test:e2e
159+
160+
# If all tests pass, we move on to build a Docker image.
161+
# This is a job for a separate pipeline which we link with a promotion.
162+
#
163+
# What happens outside semaphore.yml will not appear in GitHub pull
164+
# request status report.
165+
#
166+
# In this example we run docker build automatically on every branch.
167+
# You may want to limit it by branch name, or trigger it manually.
168+
# For more on such options, see:
169+
# https://docs.semaphoreci.com/article/50-pipeline-yaml#promotions
170+
promotions:
171+
- name: Dockerize server
172+
pipeline_file: server-docker-build.yml
173+
auto_promote_on:
174+
- result: passed
175+
- name: Deploy client
176+
pipeline_file: client-deploy-build.yml
177+
auto_promote_on:
178+
- result: passed

.semaphore/server-deploy-k8s.yml

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# This pipeline runs after docker-build.yml
2+
version: v1.0
3+
name: Deploy server to Kubernetes
4+
agent:
5+
machine:
6+
type: e1-standard-2
7+
os_image: ubuntu1804
8+
blocks:
9+
- name: Deploy server to Kubernetes
10+
task:
11+
# Mount a secret which defines /home/semaphore/.kube/gc-k8s.yaml.
12+
# By mounting it, we make file available in the job environment.
13+
# For info on creating secrets, see:
14+
# https://docs.semaphoreci.com/article/66-environment-variables-and-secrets
15+
secrets:
16+
- name: gc-k8s-secret
17+
- name: gcr-secret
18+
19+
# Define an environment variable which configures kubectl:
20+
env_vars:
21+
- name: KUBECONFIG
22+
value: /home/semaphore/.kube/gc-k8s.yaml
23+
prologue:
24+
commands:
25+
# Authenticate using the file injected from the secret
26+
- gcloud auth activate-service-account --key-file=.secrets.gcp.json
27+
# Don't forget -q to silence confirmation prompts
28+
- gcloud auth configure-docker -q
29+
- gcloud config set project $GCP_PROJECT_ID
30+
- gcloud config set compute/zone $GCP_PROJECT_DEFAULT_ZONE
31+
- checkout
32+
- cd src/server
33+
jobs:
34+
- name: Deploy
35+
commands:
36+
- kubectl get nodes
37+
- kubectl get pods
38+
39+
# Our deployment.yml instructs Kubernetes to pull container image
40+
# named semaphoredemos/semaphore-demo-javascript-server:$SEMAPHORE_WORKFLOW_ID
41+
#
42+
# envsubst is a tool which will replace $SEMAPHORE_WORKFLOW_ID with
43+
# its current value. The same variable was used in server-docker-build.yml
44+
# pipeline to tag and push a container image.
45+
- envsubst < deployment.yml | tee deployment.yml
46+
47+
# Perform declarative deployment:
48+
- kubectl apply -f deployment.yml
49+
50+
# If deployment to production succeeded, let's create a new version of
51+
# our `latest` Docker image.
52+
- name: Tag latest release
53+
task:
54+
secrets:
55+
- name: gcr-secret
56+
prologue:
57+
commands:
58+
# Authenticate using the file injected from the secret
59+
- gcloud auth activate-service-account --key-file=.secrets.gcp.json
60+
# Don't forget -q to silence confirmation prompts
61+
- gcloud auth configure-docker -q
62+
- gcloud config set project $GCP_PROJECT_ID
63+
- gcloud config set compute/zone $GCP_PROJECT_DEFAULT_ZONE
64+
- whereis gcloud
65+
- checkout
66+
- cd src/server
67+
jobs:
68+
- name: docker tag latest
69+
commands:
70+
- docker pull "gcr.io/$GCP_PROJECT_ID/semaphore-demo-javascript-server:$SEMAPHORE_WORKFLOW_ID"
71+
- docker tag "gcr.io/$GCP_PROJECT_ID/semaphore-demo-javascript-server:$SEMAPHORE_WORKFLOW_ID" "gcr.io/$GCP_PROJECT_ID/semaphore-demo-javascript-server:latest"
72+
- docker push "gcr.io/$GCP_PROJECT_ID/semaphore-demo-javascript-server:latest"

0 commit comments

Comments
 (0)