-
Notifications
You must be signed in to change notification settings - Fork 0
77 lines (62 loc) · 2.49 KB
/
deploy.yml
File metadata and controls
77 lines (62 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
name: CI/CD Pipeline
on:
push:
branches: ["main", "staging", "feature/CICD"]
permissions:
contents: write
packages: write
concurrency:
group: cicd-${{ github.ref_name }}
cancel-in-progress: false
jobs:
build-and-deploy:
runs-on: ubuntu-latest
env:
REGISTRY: ${{ secrets.REGISTRY_NAME }}
CLUSTER_NAME: ${{ secrets.CLUSTER_NAME }}
IMAGE_NAME: codeval
DOCKERFILE: Dockerfile
K8S_NAMESPACE: default
DEPLOYMENT_NAME: codeeval
CONTAINER_NAME: codeeval
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install doctl
uses: digitalocean/action-doctl@v2
with:
token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
- name: Log in to DigitalOcean Container Registry (short-lived)
run: doctl registry login --expiry-seconds 1200
# Compute a readable tag with DATE + branch + run number
- name: Compute image tag and namespace
id: meta
shell: bash
run: |
DATE=$(date +'%m%d%Y')
SAFE_BRANCH=$(echo "${{ github.ref_name }}" | tr '[:upper:]' '[:lower:]' | tr -cs 'a-z0-9.-' '-')
TAG="${SAFE_BRANCH}-${DATE}-r${{ github.run_number }}"
echo "tag=$TAG" >> "$GITHUB_OUTPUT"
echo "namespace=$SAFE_BRANCH" >> "$GITHUB_OUTPUT"
- name: Build image
run: |
docker build \
-f "${{ env.DOCKERFILE }}" \
-t "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.tag }}" .
- name: Push image
run: docker push "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.tag }}"
- name: Tag on GitHub for backup
if: github.ref == 'refs/heads/staging' || github.ref == 'refs/heads/main' || github.ref == 'refs/heads/feature/CICD'
run: |
git tag "${{ steps.meta.outputs.tag }}"
git push origin "${{ steps.meta.outputs.tag }}"
- name: Save kubeconfig with short-lived credentials
run: doctl kubernetes cluster kubeconfig save --expiry-seconds 600 "${{ env.CLUSTER_NAME }}"
- name: Set image in Kubernetes
run: |
kubectl -n "${{ env.K8S_NAMESPACE }}" set image deployment/${{ env.DEPLOYMENT_NAME }} \
${{ env.CONTAINER_NAME }}=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.tag }} \
--record
kubectl -n "${{ env.K8S_NAMESPACE }}" rollout status deployment/${{ env.DEPLOYMENT_NAME }} --timeout=120s