summaryrefslogtreecommitdiff
path: root/.github/workflows/ci-cd.yml
blob: 205bb0f667d461bfe6d1369925a6f7d3600866ac (plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
name: ci-cd

on:
  push:
    branches: [ main ]

env:
  DOCKERHUB_USER: hildan
  DOCKER_IMAGE_NAME: seven-wonders-server
  DOCKER_IMAGE_TAG_SHA: sha-${{ github.sha }}
  DOCKER_IMAGE_TAG_BUILD: build-${{ github.run_id }}

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: Set up JDK 15
        uses: actions/setup-java@v1.4.3
        with:
          java-version: 15

      - name: Gradle build cache
        uses: actions/cache@v2
        with:
          path: ~/.gradle/caches
          key: ${{ runner.os }}-gradle-build-${{ hashFiles('**/*.gradle*') }}
          restore-keys: ${{ runner.os }}-gradle-build-

      - name: Gradle wrapper cache
        uses: actions/cache@v2
        with:
          path: ~/.gradle/wrapper
          key: ${{ runner.os }}-gradle-wrapper-${{ hashFiles('**/gradle-wrapper.properties') }}

      - name: Build with Gradle
        run: ./gradlew build

      # technically optional, brings support for more platform to Docker Buildx
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v1

      # required for Docker build/push
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1

      - name: Login to DockerHub
        uses: docker/login-action@v1
        with:
          username: ${{ env.DOCKERHUB_USER }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v1
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.CONTAINER_REGISTRY_TOKEN }}

      - name: Build and push to DockerHub and GitHub Container Registry
        uses: docker/build-push-action@v2
        with:
          context: ./sw-server
          file: ./sw-server/Dockerfile
          push: true
          tags: |
            ${{ env.DOCKERHUB_USER }}/${{ env.DOCKER_IMAGE_NAME }}:latest
            ${{ env.DOCKERHUB_USER }}/${{ env.DOCKER_IMAGE_NAME }}:${{ env.DOCKER_IMAGE_TAG_SHA }}
            ${{ env.DOCKERHUB_USER }}/${{ env.DOCKER_IMAGE_NAME }}:${{ env.DOCKER_IMAGE_TAG_BUILD }}
            ghcr.io/${{ github.repository_owner }}/${{ env.DOCKER_IMAGE_NAME }}:latest
            ghcr.io/${{ github.repository_owner }}/${{ env.DOCKER_IMAGE_NAME }}:${{ env.DOCKER_IMAGE_TAG_SHA }}
            ghcr.io/${{ github.repository_owner }}/${{ env.DOCKER_IMAGE_NAME }}:${{ env.DOCKER_IMAGE_TAG_BUILD }}

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

      # Short-lived credentials (5 min) to avoid accumulating tokens
      - name: Setup DigitalOcean kubeconfig
        run: doctl kubernetes cluster kubeconfig save --expiry-seconds 300 bro-cluster
        
      - name: Update deployment file
        run: TAG=${{ env.DOCKER_IMAGE_TAG_BUILD }} && sed -i 's|:latest|:'${TAG}'|' kubernetes/server.yml

      - name: Notify deploy start
        env:
          DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK_SEVEN_WONDERS }}
        uses: Ilshidur/action-discord@0.3.0
        with:
          args: "[Build #${{github.run_id}}](https://github.com/${{github.repository}}/actions/runs/${{github.run_id}}): Deploying new version `${{ env.DOCKERHUB_USER }}/${{ env.DOCKER_IMAGE_NAME }}:${{ env.DOCKER_IMAGE_TAG_BUILD }}` ([see changes]({{ EVENT_PAYLOAD.compare }}))..."

      - name: Deploy to DigitalOcean Kubernetes
        run: kubectl apply -f kubernetes/server.yml

      - name: Verify deployment
        run: kubectl rollout status --namespace seven-wonders deployment/seven-wonders

      - name: Notify deploy success
        env:
          DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK_SEVEN_WONDERS }}
        uses: Ilshidur/action-discord@0.3.0
        with:
          args: 'Deployment successful!'
bgstack15