Skip to content

Commit

Permalink
devops CI/CD digitalocean registry and k8s
Browse files Browse the repository at this point in the history
  • Loading branch information
giuliohome committed Aug 28, 2024
1 parent 7ff0c57 commit e06e13c
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 1,220 deletions.
69 changes: 58 additions & 11 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,66 @@ name: Go
on:
push:
branches:
- sonarcloud
- digitalocean
pull_request:
types: [opened, synchronize, reopened]
jobs:
sonarcloud:
name: SonarCloud
# This workflow contains a single job called "build".
build:
# The type of runner that the job will run on.
runs-on: ubuntu-latest

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it.
- name: Checkout master
uses: actions/checkout@main

- name: Setup-Golang
uses: actions/setup-go@v4
with:
go-version: '1.23'
check-latest: true
- run: go version
- name: Build-Golang-App
run: GOOS=linux go build web.go
- name: Display-Build-Golang-App
run: ls -l web

# Install doctl.
- name: Install doctl
uses: digitalocean/action-doctl@v2
with:
token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}

# Build a Docker image of your application in your registry and tag the image with the $GITHUB_SHA.
- name: Build container image
run: docker build -t ${{ secrets.REGISTRY_NAME }}/golang-web:$(echo $GITHUB_SHA | head -c7) .

- name: Log in to DigitalOcean Container Registry with short-lived credentials
run: doctl registry login --expiry-seconds 1200

- name: Push image to DigitalOcean Container Registry
run: docker push ${{ secrets.REGISTRY_NAME }}/golang-web:$(echo $GITHUB_SHA | head -c7)

# Deploy to DigitalOCcean Kubernetes.
- name: Update deployment file
run: TAG=$(echo $GITHUB_SHA | head -c7) && sed -i 's|<IMAGE>|${{ secrets.REGISTRY_NAME }}/golang-web:'${TAG}'|' $GITHUB_WORKSPACE/deployment.yml

- name: Save DigitalOcean kubeconfig with short-lived credentials
run: doctl kubernetes cluster kubeconfig save --expiry-seconds 600 ${{ secrets.CLUSTER_NAME }}

# If you haven't already integrated with your registry and only want workloads in a particular namespace to be able to pull
# from your registry, uncomment the next two commands.
#- name: Upload credentials of your registry to your DigitalOcean Kubernetes cluster
# run: doctl registry kubernetes-manifest | kubectl apply -f -

#- name: Reference the secret you just uploaded as an imagePullSecrets
# run: "kubectl patch serviceaccount default -p '{\"imagePullSecrets\": [{\"name\": \"registry-<your-registry-name>`\"}]}'"

- name: Deploy to DigitalOcean Kubernetes
run: kubectl apply -f $GITHUB_WORKSPACE/deployment.yml

- name: Verify deployment
run: kubectl rollout status deployment/golang-web
4 changes: 1 addition & 3 deletions deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ spec:
labels:
app: golang-web
spec:
imagePullSecrets:
- name: do-registry
containers:
- name: hello-app
image: giuliohome/web.golang:v1.2
image: <IMAGE>
ports:
- containerPort: 8080
resources:
Expand Down
4 changes: 4 additions & 0 deletions home.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<h1>Web Golang</h1>


<a href="/view/a1">a1 demo text</a>
Loading

0 comments on commit e06e13c

Please sign in to comment.