-
Notifications
You must be signed in to change notification settings - Fork 0
169 lines (144 loc) · 5.54 KB
/
release-build-and-deploy.yml
File metadata and controls
169 lines (144 loc) · 5.54 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
161
162
163
164
165
166
167
168
169
name: Release, Build, and Deploy
on:
push:
tags:
- '*'
permissions:
contents: write
packages: write
jobs:
release:
name: Create GitHub Release
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Extract release notes from changelog
id: changelog
run: |
# Extract release notes for the current tag from CHANGELOG.md
TAG_NAME="${{ github.ref_name }}"
# Find the section for this version in the changelog
awk -v tag="$TAG_NAME" '
BEGIN { found=0; notes=""; skip_deps=0 }
$0 ~ "^## \\[" tag "\\]" { found=1; next }
found && /^## \[/ { exit }
found && /^### Dependencies/ { skip_deps=1; next }
found && /^### / { skip_deps=0 }
found && !skip_deps && !/^\[.*\]:/ {
if (notes != "") notes = notes "\n"
notes = notes $0
}
END {
gsub(/\n+$/, "", notes)
print notes
}
' CHANGELOG.md > release_notes.txt
# Set the release notes as output
echo "notes<<EOF" >> $GITHUB_OUTPUT
cat release_notes.txt >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Create Release
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ github.ref_name }}
body: ${{ steps.changelog.outputs.notes }}
draft: false
prerelease: ${{ contains(github.ref_name, 'alpha') || contains(github.ref_name, 'beta') || contains(github.ref_name, 'rc') }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
build-and-push:
name: Build Container Image and Push
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
install: true
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ghcr.io/${{ github.repository }}
tags: |
type=semver,pattern={{version}},priority=1000
type=semver,pattern={{major}}.{{minor}},priority=900
type=semver,pattern={{major}},priority=800
type=ref,event=branch,priority=700
type=raw,value=latest,priority=600
type=sha,priority=500
- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: .
push: true
platforms: linux/amd64,linux/arm64
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
set-deployment-variables:
name: Set Deployment Variables
runs-on: ubuntu-latest
outputs:
app_url: ${{ steps.vars.outputs.app_url }}
aws_region: ${{ steps.vars.outputs.aws_region }}
domain_name: ${{ steps.vars.outputs.domain_name }}
subdomain: ${{ steps.vars.outputs.subdomain }}
steps:
- id: vars
run: |
AWS_REGION="${{ vars.AWS_REGION || 'us-east-1' }}"
DOMAIN_NAME="${{ vars.DOMAIN_NAME || 'suhailskhan.com' }}"
SUBDOMAIN="${{ vars.SUBDOMAIN || 'ai-usage-log' }}"
APP_URL="https://${SUBDOMAIN}.${DOMAIN_NAME}"
echo "aws_region=$AWS_REGION" >> $GITHUB_OUTPUT
echo "domain_name=$DOMAIN_NAME" >> $GITHUB_OUTPUT
echo "subdomain=$SUBDOMAIN" >> $GITHUB_OUTPUT
echo "app_url=$APP_URL" >> $GITHUB_OUTPUT
echo "App will be deployed to: $APP_URL"
deploy:
name: Deploy to AWS
needs: [build-and-push, set-deployment-variables]
runs-on: ubuntu-latest
environment:
name: dev
url: ${{ needs.set-deployment-variables.outputs.app_url }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up OpenTofu
uses: opentofu/setup-opentofu@v1
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ needs.set-deployment-variables.outputs.aws_region }}
- name: Initialize Tofu
run: |
tofu -chdir=infra init
- name: Tofu Plan
run: |
tofu -chdir=infra plan \
-var="aws_region=${{ needs.set-deployment-variables.outputs.aws_region }}" \
-var="environment=dev" \
-var="cloudflare_api_token=${{ secrets.CLOUDFLARE_API_TOKEN }}" \
-var="cloudflare_zone_id=${{ secrets.CLOUDFLARE_ZONE_ID }}" \
-var="domain_name=${{ needs.set-deployment-variables.outputs.domain_name }}" \
-var="subdomain=${{ needs.set-deployment-variables.outputs.subdomain }}"
- name: Tofu Apply
run: |
tofu -chdir=infra apply -auto-approve \
-var="aws_region=${{ needs.set-deployment-variables.outputs.aws_region }}" \
-var="environment=dev" \
-var="cloudflare_api_token=${{ secrets.CLOUDFLARE_API_TOKEN }}" \
-var="cloudflare_zone_id=${{ secrets.CLOUDFLARE_ZONE_ID }}" \
-var="domain_name=${{ needs.set-deployment-variables.outputs.domain_name }}" \
-var="subdomain=${{ needs.set-deployment-variables.outputs.subdomain }}"