Skip to content

Commit e06e13c

Browse files
committed
devops CI/CD digitalocean registry and k8s
1 parent 7ff0c57 commit e06e13c

File tree

6 files changed

+78
-1220
lines changed

6 files changed

+78
-1220
lines changed

.github/workflows/go.yml

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,66 @@ name: Go
22
on:
33
push:
44
branches:
5-
- sonarcloud
5+
- digitalocean
66
pull_request:
77
types: [opened, synchronize, reopened]
88
jobs:
9-
sonarcloud:
10-
name: SonarCloud
9+
# This workflow contains a single job called "build".
10+
build:
11+
# The type of runner that the job will run on.
1112
runs-on: ubuntu-latest
13+
14+
# Steps represent a sequence of tasks that will be executed as part of the job
1215
steps:
13-
- uses: actions/checkout@v2
14-
with:
15-
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
16-
- name: SonarCloud Scan
17-
uses: SonarSource/sonarcloud-github-action@master
18-
env:
19-
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} # Needed to get PR information, if any
20-
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
16+
17+
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it.
18+
- name: Checkout master
19+
uses: actions/checkout@main
20+
21+
- name: Setup-Golang
22+
uses: actions/setup-go@v4
23+
with:
24+
go-version: '1.23'
25+
check-latest: true
26+
- run: go version
27+
- name: Build-Golang-App
28+
run: GOOS=linux go build web.go
29+
- name: Display-Build-Golang-App
30+
run: ls -l web
31+
32+
# Install doctl.
33+
- name: Install doctl
34+
uses: digitalocean/action-doctl@v2
35+
with:
36+
token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
37+
38+
# Build a Docker image of your application in your registry and tag the image with the $GITHUB_SHA.
39+
- name: Build container image
40+
run: docker build -t ${{ secrets.REGISTRY_NAME }}/golang-web:$(echo $GITHUB_SHA | head -c7) .
41+
42+
- name: Log in to DigitalOcean Container Registry with short-lived credentials
43+
run: doctl registry login --expiry-seconds 1200
44+
45+
- name: Push image to DigitalOcean Container Registry
46+
run: docker push ${{ secrets.REGISTRY_NAME }}/golang-web:$(echo $GITHUB_SHA | head -c7)
47+
48+
# Deploy to DigitalOCcean Kubernetes.
49+
- name: Update deployment file
50+
run: TAG=$(echo $GITHUB_SHA | head -c7) && sed -i 's|<IMAGE>|${{ secrets.REGISTRY_NAME }}/golang-web:'${TAG}'|' $GITHUB_WORKSPACE/deployment.yml
51+
52+
- name: Save DigitalOcean kubeconfig with short-lived credentials
53+
run: doctl kubernetes cluster kubeconfig save --expiry-seconds 600 ${{ secrets.CLUSTER_NAME }}
54+
55+
# If you haven't already integrated with your registry and only want workloads in a particular namespace to be able to pull
56+
# from your registry, uncomment the next two commands.
57+
#- name: Upload credentials of your registry to your DigitalOcean Kubernetes cluster
58+
# run: doctl registry kubernetes-manifest | kubectl apply -f -
59+
60+
#- name: Reference the secret you just uploaded as an imagePullSecrets
61+
# run: "kubectl patch serviceaccount default -p '{\"imagePullSecrets\": [{\"name\": \"registry-<your-registry-name>`\"}]}'"
62+
63+
- name: Deploy to DigitalOcean Kubernetes
64+
run: kubectl apply -f $GITHUB_WORKSPACE/deployment.yml
65+
66+
- name: Verify deployment
67+
run: kubectl rollout status deployment/golang-web

deployment.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@ spec:
1818
labels:
1919
app: golang-web
2020
spec:
21-
imagePullSecrets:
22-
- name: do-registry
2321
containers:
2422
- name: hello-app
25-
image: giuliohome/web.golang:v1.2
23+
image: <IMAGE>
2624
ports:
2725
- containerPort: 8080
2826
resources:

home.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<h1>Web Golang</h1>
2+
3+
4+
<a href="/view/a1">a1 demo text</a>

0 commit comments

Comments
 (0)