aboutsummaryrefslogtreecommitdiffstats
path: root/.github/workflows/deploy.yaml
blob: 82903f8efe2f858c94db758d6bf832c4cd5723c9 (plain) (blame)
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
on:
  workflow_call:
    inputs:
      artifact:
        required: true
        type: string
      version:
        required: true
        type: string
    secrets:
      KUBECONFIG:
        required: true

jobs:
  deploy:
    name: Build, push, & deploy
    runs-on: ubuntu-latest

    steps:
      - name: Download image artifact
        uses: actions/download-artifact@v2
        with:
          name: ${{ inputs.artifact }}

      # Load the image to make use of common layers during the final build.
      - name: Load image from archive
        run: docker load -i ${{ inputs.artifact }}.tar

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1

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

      - name: Checkout code
        uses: actions/checkout@v2
        with:
          # The version script relies on history. Fetch 100 commits to be safe.
          fetch-depth: 100

      # Build the final production image and push it to GHCR.
      # Tag it with both the short commit SHA and 'latest'.
      - name: Build final image
        uses: docker/build-push-action@v2
        with:
          context: .
          file: ./Dockerfile
          push: true
          cache-from: |
            ghcr.io/python-discord/snekbox-base:latest
            ghcr.io/python-discord/snekbox-venv:latest
            ghcr.io/python-discord/snekbox:latest
          cache-to: type=inline
          tags: |
            ghcr.io/python-discord/snekbox:latest
            ghcr.io/python-discord/snekbox:${{ inputs.version }}

      # Deploy to Kubernetes.
      - name: Authenticate with Kubernetes
        uses: azure/k8s-set-context@v1
        with:
          method: kubeconfig
          kubeconfig: ${{ secrets.KUBECONFIG }}

      - name: Deploy to Kubernetes
        uses: Azure/k8s-deploy@v1
        with:
          manifests: deployment.yaml
          images: 'ghcr.io/python-discord/snekbox:${{ inputs.version }}'
          kubectl-version: 'latest'

      # Push the base image to GHCR, with an inline cache manifest.
      - name: Push base image
        uses: docker/build-push-action@v2
        with:
          context: .
          file: ./Dockerfile
          target: base
          push: true
          cache-from: ghcr.io/python-discord/snekbox-base:latest
          cache-to: type=inline
          tags: |
            ghcr.io/python-discord/snekbox-base:latest
            ghcr.io/python-discord/snekbox-base:${{ inputs.version }}

      # Push the venv image to GHCR, with an inline cache manifest.
      - name: Push venv image
        uses: docker/build-push-action@v2
        with:
          context: .
          file: ./Dockerfile
          target: venv
          push: true
          cache-from: |
            ghcr.io/python-discord/snekbox-base:latest
            ghcr.io/python-discord/snekbox-venv:latest
          cache-to: type=inline
          tags: |
            ghcr.io/python-discord/snekbox-venv:latest
            ghcr.io/python-discord/snekbox-venv:${{ inputs.version }}