diff --git a/gitlab/ci/.gitlab-ci.yml b/gitlab/ci/.gitlab-ci.yml new file mode 100644 index 0000000..8c44c89 --- /dev/null +++ b/gitlab/ci/.gitlab-ci.yml @@ -0,0 +1,57 @@ +image: docker:latest + +services: + - docker:dind + +stages: +- build_test_release +- deploy_staging +- deploy_prod + +variables: + CONTAINER_TEST_IMAGE: gregbkr/flask_test + CONTAINER_RELEASE_IMAGE: gregbkr/flask + +before_script: + - docker login -u gregbkr -p Geneva2017$ + +build_test_release: + stage: build_test_release + script: + - docker build --pull -t $CONTAINER_TEST_IMAGE flask/src + - docker push $CONTAINER_TEST_IMAGE + - docker run --name=flask -d -p 5000:5000 $CONTAINER_TEST_IMAGE + - docker run -i --rm --link=flask:flask pstauffer/curl sh < flask/src/test.sh + - docker rm -f -v flask + - docker pull $CONTAINER_TEST_IMAGE + - docker tag $CONTAINER_TEST_IMAGE $CONTAINER_RELEASE_IMAGE + - docker push $CONTAINER_RELEASE_IMAGE + +deploy_staging: + stage: deploy_staging + script: + - cd /usr/local/bin + - curl -O https://storage.googleapis.com/kubernetes-release/release/v1.5.1/bin/linux/amd64/kubectl + - chmod +x kubectl + - cd /builds/root/k8-ci + - kubectl config set-cluster kargo --server=https://159.100.252.54:8443 --certificate-authority=kubectl/ca.pem + - kubectl config set-credentials kadmin --certificate-authority=kubectl/ca.pem --client-key=kubectl/admin-node1-key.pem --client-certificate=kubectl/admin-node1.pem + - kubectl config set-context kargo --cluster=kargo --user=kadmin + - kubectl config use-context kargo + - kubectl delete -f flask/flask-deployment.yaml + - kubectl apply -f flask/flask-deployment.yaml + +deploy_prod: + stage: deploy_prod + when: manual + script: + - cd /usr/local/bin + - curl -O https://storage.googleapis.com/kubernetes-release/release/v1.5.1/bin/linux/amd64/kubectl + - chmod +x kubectl + - cd /builds/root/k8-ci + - kubectl config set-cluster kargo --server=https://159.100.252.54:8443 --certificate-authority=kubectl/ca.pem + - kubectl config set-credentials kadmin --certificate-authority=kubectl/ca.pem --client-key=kubectl/admin-node1-key.pem --client-certificate=kubectl/admin-node1.pem + - kubectl config set-context kargo --cluster=kargo --user=kadmin + - kubectl config use-context kargo + - kubectl delete -f flask/flask-deployment.yaml + - kubectl apply -f flask/flask-deployment.yaml diff --git a/gitlab/ci/flask/flask-deployment.yaml b/gitlab/ci/flask/flask-deployment.yaml new file mode 100644 index 0000000..6dab6e0 --- /dev/null +++ b/gitlab/ci/flask/flask-deployment.yaml @@ -0,0 +1,17 @@ +apiVersion: extensions/v1beta1 +kind: Deployment +metadata: + name: flask +spec: + replicas: 1 + template: + metadata: + labels: + app: flask + spec: + containers: + - name: flask + image: gregbkr/flask + imagePullPolicy: Always + ports: + - containerPort: 5000 diff --git a/gitlab/ci/flask/flask-service.yaml b/gitlab/ci/flask/flask-service.yaml new file mode 100644 index 0000000..3302333 --- /dev/null +++ b/gitlab/ci/flask/flask-service.yaml @@ -0,0 +1,15 @@ +--- +apiVersion: v1 +kind: Service +metadata: + name: flask + #namespace: default + labels: + app: flask +spec: + type: NodePort + ports: + - port: 5000 + nodePort: 30555 + selector: + app: flask diff --git a/gitlab/ci/flask/src/Dockerfile b/gitlab/ci/flask/src/Dockerfile new file mode 100644 index 0000000..7ff0cdc --- /dev/null +++ b/gitlab/ci/flask/src/Dockerfile @@ -0,0 +1,6 @@ +FROM python:2.7 +ADD . /code +WORKDIR /code +RUN pip install -r requirements.txt +ENTRYPOINT ["python"] +CMD ["app.py"] diff --git a/gitlab/ci/flask/src/app.py b/gitlab/ci/flask/src/app.py new file mode 100644 index 0000000..8504d53 --- /dev/null +++ b/gitlab/ci/flask/src/app.py @@ -0,0 +1,10 @@ +from flask import Flask + +app = Flask(__name__) + +@app.route("/") +def hello_world(): + return "Hello World 1!" + +if __name__ == "__main__": + app.run(debug=True,host='0.0.0.0') diff --git a/gitlab/ci/flask/src/requirements.txt b/gitlab/ci/flask/src/requirements.txt new file mode 100644 index 0000000..1a5dc97 --- /dev/null +++ b/gitlab/ci/flask/src/requirements.txt @@ -0,0 +1,2 @@ +flask +redis diff --git a/gitlab/ci/flask/src/test.sh b/gitlab/ci/flask/src/test.sh new file mode 100755 index 0000000..c62ab6b --- /dev/null +++ b/gitlab/ci/flask/src/test.sh @@ -0,0 +1,8 @@ +sleep 5 +if curl flask:5000 | grep 'Hello World'; then + echo "Tests passed!" + exit 0 +else + echo "Tests failed!" + exit 1 +fi