-
Notifications
You must be signed in to change notification settings - Fork 0
160 lines (139 loc) · 4.94 KB
/
deploy.yml
File metadata and controls
160 lines (139 loc) · 4.94 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
name: Build and Deploy
on:
push:
branches: [main, master]
tags:
- 'v*'
workflow_dispatch:
inputs:
runner:
description: 'Choose runner'
default: 'self-hosted'
type: choice
options:
- self-hosted
- ubuntu-latest
permissions:
contents: read
packages: write
jobs:
setup:
runs-on: ubuntu-latest
outputs:
runner: ${{ steps.set-runner.outputs.runner }}
steps:
- name: Determine runner
id: set-runner
run: |
SELECTED_RUNNER="${{ github.event.inputs.runner }}"
if [ -z "$SELECTED_RUNNER" ]; then
SELECTED_RUNNER="self-hosted"
fi
echo "runner=$SELECTED_RUNNER" >> $GITHUB_OUTPUT
build-and-push:
needs: setup
runs-on: ${{ needs.setup.outputs.runner }}
permissions:
contents: write
packages: write
outputs:
image_tag: ${{ steps.meta.outputs.tags }}
version: ${{ steps.version.outputs.version }}
publish_tag: ${{ steps.publish_tag.outputs.publish_tag }}
sha_tag: ${{ steps.sha_tag.outputs.sha_tag }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Determine version from pyproject
id: version
run: |
VERSION=$(grep -E '^version *= *"' pyproject.toml | head -n1 | sed -E 's/version *= *"([^"]+)"/\1/')
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "Version: $VERSION"
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Compute image repo
id: image
run: |
echo "repo=ghcr.io/statpan/assemblymcp" >> $GITHUB_OUTPUT
echo "dockerhub_repo=statpan/assemblymcp" >> $GITHUB_OUTPUT
- name: SHA tag
id: sha_tag
run: echo "sha_tag=sha-${GITHUB_SHA::7}" >> $GITHUB_OUTPUT
- name: Choose publish tag (use repo version)
id: publish_tag
run: |
if [[ -n "${{ steps.version.outputs.version }}" ]]; then
echo "publish_tag=${{ steps.version.outputs.version }}" >> $GITHUB_OUTPUT
else
echo "publish_tag=${{ steps.sha_tag.outputs.sha_tag }}" >> $GITHUB_OUTPUT
fi
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: statpan
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: |
${{ steps.image.outputs.repo }}
${{ steps.image.outputs.dockerhub_repo }}
tags: |
type=raw,value=${{ steps.version.outputs.version }}
type=ref,event=branch
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=sha,prefix=sha-
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/arm64,linux/amd64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
# One-time: after the first push, set the package to public:
# gh api -X PATCH /user/packages/container/assemblymcp/visibility -f visibility=public
- name: Create release (on tag)
if: startsWith(github.ref, 'refs/tags/v')
uses: softprops/action-gh-release@v1
with:
generate_release_notes: true
trigger-deployment:
needs: [setup, build-and-push]
runs-on: ${{ needs.setup.outputs.runner }}
if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master'
steps:
- name: Trigger infrastructure deployment
run: |
IMAGE_TAG="${{ needs.build-and-push.outputs.publish_tag }}"
curl -X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.INFRA_REPO_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/StatPan/dlfrnaos19-infra/dispatches \
-d "{
\"event_type\": \"deploy-assemblymcp\",
\"client_payload\": {
\"image_tag\": \"$IMAGE_TAG\",
\"repository\": \"${{ github.repository }}\",
\"sha\": \"${{ github.sha }}\",
\"ref\": \"${{ github.ref }}\",
\"actor\": \"${{ github.actor }}\",
\"env\": \"production\"
}
}"
echo "Triggered deployment with image tag: $IMAGE_TAG"