├── .github └── workflows │ └── workflow.yaml ├── .gitignore ├── Dockerfile ├── LICENSE ├── README.md ├── config ├── deployment.yml └── nginx.conf └── site └── index.html /.github/workflows/workflow.yaml: -------------------------------------------------------------------------------- 1 | on: 2 | push: 3 | branches: 4 | - main 5 | paths: 6 | - 'config/**' 7 | - 'site/**' 8 | - 'Dockerfile' 9 | - '.github/workflows/**' 10 | 11 | jobs: 12 | 13 | build: 14 | name: Build, push, and deploy 15 | runs-on: ubuntu-latest 16 | steps: 17 | 18 | - name: Checkout main 19 | uses: actions/checkout@v2 20 | 21 | - name: Update SHA 22 | run: echo $GITHUB_SHA > $GITHUB_WORKSPACE/site/_meta 23 | 24 | - name: Build container image 25 | run: docker build -t registry.digitalocean.com/asb/static-example:$(echo $GITHUB_SHA | head -c7) . 26 | 27 | - name: Install doctl 28 | uses: digitalocean/action-doctl@v2 29 | with: 30 | token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }} 31 | 32 | - name: Log in to DigitalOcean Container Registry with short-lived credentials 33 | run: doctl registry login --expiry-seconds 600 34 | 35 | - name: Push image to DigitalOcean Container Registry 36 | run: docker push registry.digitalocean.com/asb/static-example:$(echo $GITHUB_SHA | head -c7) 37 | 38 | - name: Update deployment file 39 | run: TAG=$(echo $GITHUB_SHA | head -c7) && sed -i 's||registry.digitalocean.com/asb/static-example:'${TAG}'|' $GITHUB_WORKSPACE/config/deployment.yml 40 | 41 | - name: Save DigitalOcean kubeconfig with short-lived credentials 42 | run: doctl kubernetes cluster kubeconfig save --expiry-seconds 600 actions-example-k8s-1-18 43 | 44 | - name: Deploy to DigitalOcean Kubernetes 45 | run: kubectl apply -f $GITHUB_WORKSPACE/config/deployment.yml 46 | 47 | - name: Verify deployment 48 | run: kubectl rollout status deployment/static-example 49 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | site/_meta 2 | 3 | .vscode/ -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM nginx:alpine 2 | 3 | COPY config/nginx.conf /etc/nginx/conf.d/default.conf 4 | 5 | WORKDIR /usr/share/nginx/html 6 | COPY site . 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 DigitalOcean and contributors 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GitHub Actions for DigitalOcean Example 2 | 3 | This repository contains an example workflow using the [GitHub Action for DigitalOcean](https://github.com/digitalocean/action-doctl) to build, tag, and deploy a container image to a DigitalOcean Kubernetes cluster. 4 | 5 | ## Workflow 6 | 7 | The [example workflow](.github/workflows/workflow.yaml) will trigger on every push to this repo's `master` branch. For push, the workflow will: 8 | 9 | * Build the image from [the included `Dockerfile`](Dockerfile) 10 | * Tag and push the image to a private DigitalOcean container registry 11 | * Retrieve the `kubeconfig` file for a DigitalOcean Kubernetes cluster 12 | * Create a deployment using [config/deployment.yml](config/deployment.yml) 13 | 14 | ### Notes 15 | 16 | * This example is using a Kubernetes cluster running v1.18.x with `action-doctl@v2`. (For older versions, see the [v1 tag](https://github.com/do-community/example-doctl-action/tree/v1).) 17 | * Your Kubernetes cluster must have access to your private DigitalOcean container registry (`doctl kubernetes cluster registry add `). 18 | * This example uses `external-dns` [installed via Helm](https://github.com/helm/charts/tree/master/stable/external-dns). This is an optional requirement, but you will need to adjust your `config/deployment.yml` file if it is not in use. 19 | -------------------------------------------------------------------------------- /config/deployment.yml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 2 | kind: Deployment 3 | metadata: 4 | name: static-example 5 | labels: 6 | app: static-example 7 | spec: 8 | replicas: 1 9 | selector: 10 | matchLabels: 11 | app: static-example 12 | strategy: 13 | rollingUpdate: 14 | maxSurge: 1 15 | maxUnavailable: 1 16 | minReadySeconds: 5 17 | template: 18 | metadata: 19 | labels: 20 | app: static-example 21 | spec: 22 | containers: 23 | - name: static-example 24 | image: 25 | ports: 26 | - containerPort: 5000 27 | resources: 28 | requests: 29 | cpu: 100m 30 | limits: 31 | cpu: 100m 32 | imagePullSecrets: 33 | - name: asb 34 | --- 35 | apiVersion: v1 36 | kind: Service 37 | metadata: 38 | name: static-example-service 39 | annotations: 40 | external-dns.alpha.kubernetes.io/hostname: "doctl-action.do-api.dev" 41 | service.beta.kubernetes.io/do-loadbalancer-name: "doctl-action.do-api.dev" 42 | service.beta.kubernetes.io/do-loadbalancer-certificate-id: "913a4f25-dd44-47e0-8588-eb09f095c7e2" 43 | service.beta.kubernetes.io/do-loadbalancer-redirect-http-to-https: "true" 44 | spec: 45 | type: LoadBalancer 46 | ports: 47 | - name: https 48 | protocol: TCP 49 | port: 443 50 | targetPort: 5000 51 | selector: 52 | app: static-example 53 | -------------------------------------------------------------------------------- /config/nginx.conf: -------------------------------------------------------------------------------- 1 | server { 2 | listen 5000; 3 | server_name localhost; 4 | 5 | location / { 6 | root /usr/share/nginx/html; 7 | index index.html index.htm; 8 | } 9 | 10 | error_page 500 502 503 504 /50x.html; 11 | location = /50x.html { 12 | root /usr/share/nginx/html; 13 | } 14 | 15 | location = /_meta { 16 | alias /usr/share/nginx/html/_meta; 17 | default_type text/plain; 18 | } 19 | 20 | } -------------------------------------------------------------------------------- /site/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 72 | 73 | 74 |
75 | 93 |
94 | 95 |
96 | Sammy 97 | 98 |

Hello from DigitalOcean Kubernetes

99 |

This example was deployed using GitHub Actions.

100 | View README 101 |
102 | 103 | 104 | --------------------------------------------------------------------------------