Skip to content

Commit 120bb00

Browse files
authored
Initial implementation (#1)
1 parent 3b104ff commit 120bb00

File tree

17 files changed

+584
-1
lines changed

17 files changed

+584
-1
lines changed

.dockerignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.git
2+
.idea
3+
*.iml
4+
.editorconfig
5+
build-harness
6+
.build-harness

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Override for Makefile
2+
[{Makefile, makefile, GNUmakefile}]
3+
indent_style = tab
4+
indent_size = 4
5+
6+
[Makefile.*]
7+
indent_style = tab
8+
indent_size = 4
9+
10+
[*.yaml]
11+
indent_style = spaces
12+
indent_size = 2

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
release/*
2+
.build-harness
3+
build-harness/
4+
.idea
5+
*.iml

Dockerfile

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
FROM golang:alpine3.8 AS builder
2+
3+
# Copy source into builder
4+
ADD . /src
5+
6+
# Build the app
7+
RUN cd /src && \
8+
go build -o example-app
9+
10+
# Build the final image
11+
FROM alpine:3.8 as final
12+
13+
# Install the cloudposse alpine repository
14+
ADD https://apk.cloudposse.com/ops@cloudposse.com.rsa.pub /etc/apk/keys/
15+
RUN echo "@cloudposse https://apk.cloudposse.com/3.8/vendor" >> /etc/apk/repositories
16+
RUN apk add --update bash variant@cloudposse
17+
18+
# Expose port of the app
19+
EXPOSE 8080
20+
21+
# Set the runtime working directory
22+
WORKDIR /app
23+
24+
# Copy the helmfile deployment configuration
25+
COPY deploy/ /deploy/
26+
COPY public/ /app/public/
27+
28+
# Install the app
29+
COPY --from=builder /src/example-app /app/
30+
31+
# Define the entrypoint
32+
ENTRYPOINT ["./example-app"]

Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
DOCKER_IMAGE_NAME ?= example-app
2+
SHELL = /bin/bash
3+
4+
PATH:=$(PATH):$(GOPATH)/bin
5+
6+
-include $(shell curl -sSL -o .build-harness "https://git.io/build-harness"; echo .build-harness)
7+
8+
build: go/build
9+
@exit 0
10+
11+
run:
12+
docker run -it -p 8080:8080 --rm $(DOCKER_IMAGE_NAME)

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
# example-app
2-
Example application for CI/CD demonstrations
2+
3+
Example application for Codefresh CI/CD demonstrations.
4+
5+
Contact <hello@cloudposse.com>

codefresh/build.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
version: '1.0'
2+
3+
stages:
4+
- Prepare
5+
- Build
6+
- Push
7+
8+
steps:
9+
main_clone:
10+
title: "Clone repository"
11+
type: git-clone
12+
stage: Prepare
13+
description: "Initialize"
14+
repo: ${{CF_REPO_OWNER}}/${{CF_REPO_NAME}}
15+
git: CF-default
16+
revision: ${{CF_REVISION}}
17+
18+
build_image:
19+
title: Build image
20+
stage: Build
21+
type: build
22+
description: Build image
23+
image_name: ${{CF_REPO_NAME}}
24+
dockerfile: Dockerfile
25+
26+
push_image_commit:
27+
title: Push image with commit tag
28+
stage: Push
29+
type: push
30+
candidate: ${{build_image}}
31+
tags:
32+
- "${{CF_REVISION}}"

codefresh/deploy.yaml

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
version: '1.0'
2+
3+
stages:
4+
- Prepare
5+
- Deploy
6+
7+
steps:
8+
main_clone:
9+
title: "Create Context"
10+
stage: Prepare
11+
image: alpine
12+
commands:
13+
- cf_export NAMESPACE=${{STAGE}}
14+
- cf_export IMAGE_NAME=${{CF_DOCKER_REPO_URL}}/${{CF_REPO_NAME}}
15+
- cf_export IMAGE_TAG=${{CF_RELEASE_TAG}}
16+
17+
set_github_deployment_status_to_pending:
18+
title: Set GitHub deployment status to "pending"
19+
stage: Deploy
20+
image: cloudposse/github-status-updater
21+
environment:
22+
- GITHUB_ACTION=update_state
23+
- GITHUB_TOKEN=${{GITHUB_TOKEN}}
24+
- GITHUB_OWNER=${{CF_REPO_OWNER}}
25+
- GITHUB_REPO=${{CF_REPO_NAME}}
26+
- GITHUB_REF=${{CF_REVISION}}
27+
- GITHUB_CONTEXT=${{STAGE}}-environment
28+
- GITHUB_STATE=pending
29+
- GITHUB_DESCRIPTION=Deploying changes to ${{NAMESPACE}} namespace
30+
- GITHUB_TARGET_URL=${{ATLANTIS_ATLANTIS_URL}}
31+
when:
32+
condition:
33+
all:
34+
githubNotificationsEnabled: "'${{GITHUB_NOTIFICATIONS_ENABLED}}' == 'true'"
35+
36+
deploy_helmfile:
37+
title: "Deploy with helmfile"
38+
stage: "Deploy"
39+
image: "${{CF_DOCKER_REPO_URL}}/${{CF_REPO_NAME}}:${{CF_REVISION}}"
40+
working_directory: /deploy/
41+
commands:
42+
# Announce the release version
43+
- "echo 'Preparing to deploy version ${{CF_RELEASE_TAG}}'"
44+
# Fetch the build-harness
45+
- "curl -sSL -o Makefile https://git.io/build-harness"
46+
# Initialize the build-harness
47+
- "make init"
48+
# Install or upgrade tiller
49+
- "make helm/toolbox/upsert"
50+
# Deploy chart to cluster using helmfile
51+
- "helmfile --namespace ${{NAMESPACE}} --selector color=blue sync"
52+
53+
set_github_deployment_status_to_success:
54+
title: Set GitHub deployment status to "success"
55+
stage: "Deploy"
56+
image: cloudposse/github-status-updater
57+
environment:
58+
- GITHUB_ACTION=update_state
59+
- GITHUB_TOKEN=${{GITHUB_TOKEN}}
60+
- GITHUB_OWNER=${{CF_REPO_OWNER}}
61+
- GITHUB_REPO=${{CF_REPO_NAME}}
62+
- GITHUB_REF=${{CF_REVISION}}
63+
- GITHUB_CONTEXT=${{STAGE}}-environment
64+
- GITHUB_STATE=success
65+
- GITHUB_DESCRIPTION=Deployed to ${{NAMESPACE}} namespace
66+
- GITHUB_TARGET_URL=${{ATLANTIS_ATLANTIS_URL}}
67+
when:
68+
condition:
69+
all:
70+
githubNotificationsEnabled: "'${{GITHUB_NOTIFICATIONS_ENABLED}}' == 'true'"

codefresh/release.yaml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
version: '1.0'
2+
3+
stages:
4+
- Prepare
5+
- Promote
6+
- Deploy
7+
8+
steps:
9+
main_clone:
10+
title: "Create Context"
11+
stage: "Prepare"
12+
image: alpine
13+
commands:
14+
# Extract the postfix of a semver (e.g. 0.0.0-stage => stage)
15+
- cf_export STAGE=$(echo ${{CF_RELEASE_TAG}} | sed -E 's/^[^-]+-?//')
16+
17+
pull_image_sha:
18+
title: Pull image with commit sha
19+
stage: "Promote"
20+
image: "${{CF_DOCKER_REPO_URL}}/${{CF_REPO_NAME}}:${{CF_REVISION}}"
21+
commands:
22+
- "true"
23+
24+
push_image_tag:
25+
title: Push image with release tag
26+
stage: "Promote"
27+
type: push
28+
image_name: ${{CF_REPO_NAME}}
29+
candidate: "${{CF_DOCKER_REPO_URL}}/${{CF_REPO_NAME}}:${{CF_REVISION}}"
30+
tags:
31+
- "${{CF_RELEASE_TAG}}"
32+
33+
deploy:
34+
title: Deploy Release
35+
stage: "Deploy"
36+
image: 'codefresh/cli:latest'
37+
commands:
38+
- codefresh run ${{CF_REPO_OWNER}}/${{CF_REPO_NAME}}/deploy-${{STAGE}} -d -b=${{CF_BRANCH}} -v CF_RELEASE_TAG=${{CF_RELEASE_TAG}} -v CF_PRERELEASE_FLAG=${{CF_PRERELEASE_FLAG}} -v STAGE=${{STAGE}}
39+
when:
40+
condition:
41+
all:
42+
stageDefined: "'${{STAGE}}' != ''"

deploy/ctl

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env variant
2+
# vim:set ft=yaml
3+
4+
parameters:
5+
- name: config
6+
default: .color
7+
description: "Config file that keeps track of the current color"
8+
9+
- name: namespace
10+
default: "default"
11+
description: "Kubernetes namespace"
12+
13+
tasks:
14+
15+
# https://istio.io/docs/tasks/traffic-management/ingress/#determining-the-ingress-ip-and-ports-when-using-an-external-load-balancer
16+
istio-ingress:
17+
description: Output the FQHN of the Istio Ingress Gateway
18+
script: |
19+
kubectl --namespace istio-system get service istio-ingressgateway -o jsonpath='{.status.loadBalancer.ingress[0].hostname}'
20+
21+
# https://istio.io/docs/setup/kubernetes/quick-start/
22+
istio-injection:
23+
description: Enable Istio Sidecar Injection for a namespace
24+
script: |
25+
kubectl label namespace {{ get "namespace" }} istio-injection=enabled --overwrite=true
26+
27+
use-context:
28+
description: "Configure kube-context"
29+
script: |
30+
kubectl config use-context ${KUBE_CONTEXT}
31+
32+
deps:
33+
description: "Install alpine dependencies"
34+
script: |
35+
! which apk >/dev/null || apk add --update curl make bash git kubectl@cloudposse helm@cloudposse helmfile@cloudposse
36+
helm init --client-only
37+
38+
color:
39+
description: "Lookup the current color"
40+
script: |
41+
config=${CF_VOLUME_PATH:-.}/{{ get "config" }}
42+
if [ -f ${config} ]; then
43+
cat ${config}
44+
else
45+
echo "blue" | tee ${config}
46+
fi
47+
48+
blue-green:
49+
description: "Trigger a blue-green deployment"
50+
parameters:
51+
- name: color
52+
type: string
53+
description: "Selected color to deploy"
54+
55+
- name: blue
56+
type: string
57+
default: green
58+
description: "Flip blue color to this color"
59+
60+
- name: green
61+
type: string
62+
default: blue
63+
description: "Flip green color to this color"
64+
65+
steps:
66+
- task: deps
67+
- task: use-context
68+
- task: istio-injection
69+
- script: |
70+
config=${CF_VOLUME_PATH:-.}/{{ get "config" }}
71+
cur_color={{ get "color" }}
72+
new_color={{ get "color" | get }}
73+
echo "Config file is ${config}"
74+
echo "Updating [${KUBE_CONTEXT}] context {{ get "namespace" }} namespace: $cur_color => $new_color"
75+
export COLOR=$new_color
76+
# Deploy the app and update istio virtual service
77+
helmfile --namespace {{ get "namespace" }} sync
78+
echo "$new_color" > ${config}

0 commit comments

Comments
 (0)