Skip to content

Commit b7a79b3

Browse files
authored
Add publish-version workflow (#22)
1 parent bb656e5 commit b7a79b3

1 file changed

Lines changed: 136 additions & 0 deletions

File tree

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
name: Publish version to GitHub and ECR
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
version:
7+
type: choice
8+
description: 'Publish type (dev for test image, prod for release from pyproject.toml)'
9+
required: true
10+
options:
11+
- dev
12+
- prod
13+
default: 'dev'
14+
push:
15+
branches:
16+
- main
17+
18+
jobs:
19+
build-and-push:
20+
name: Build and push Docker image
21+
runs-on: ubuntu-latest
22+
23+
steps:
24+
- name: Checkout code
25+
uses: actions/checkout@v3
26+
with:
27+
fetch-depth: 0
28+
29+
- name: Set version type
30+
id: set-version-type
31+
run: |
32+
if [ "${{ github.event_name }}" = "push" ]; then
33+
VERSION_TYPE="prod"
34+
else
35+
VERSION_TYPE="${{ github.event.inputs.version }}"
36+
fi
37+
echo "VERSION_TYPE=$VERSION_TYPE" >> $GITHUB_OUTPUT
38+
39+
- name: Validate branch for dev
40+
if: ${{ steps.set-version-type.outputs.VERSION_TYPE == 'prod' && github.ref != 'refs/heads/main' }}
41+
run: |
42+
echo "Error: Only 'dev' builds are allowed on non-main branches."
43+
exit 1
44+
45+
- name: Set version
46+
id: set-version
47+
run: |
48+
if [ "${{ steps.set-version-type.outputs.VERSION_TYPE }}" = "prod" ]; then
49+
pip install poetry
50+
VERSION=$(poetry version -s)
51+
else
52+
VERSION="dev-${GITHUB_SHA::7}"
53+
fi
54+
echo "VERSION=$VERSION"
55+
echo "VERSION=$VERSION" >> $GITHUB_OUTPUT
56+
57+
- name: Check if release exists (prod)
58+
if: ${{ steps.set-version-type.outputs.VERSION_TYPE == 'prod' }}
59+
id: check-release
60+
env:
61+
VERSION: ${{ steps.set-version.outputs.VERSION }}
62+
run: |
63+
git fetch --tags
64+
if [ -n "$(git tag -l "$VERSION")" ]; then
65+
echo "## ⚠️ Tag $VERSION already exists in git. Skipping publish." >> $GITHUB_STEP_SUMMARY
66+
echo "skip_publish=true" >> $GITHUB_OUTPUT
67+
else
68+
echo "skip_publish=false" >> $GITHUB_OUTPUT
69+
fi
70+
71+
- name: Get Python version
72+
if: steps.check-release.outputs.skip_publish != 'true'
73+
id: get-python-version
74+
run: |
75+
pip install toml
76+
PYTHON_VERSION=$(python -c 'import scripts.vars; scripts.vars.get_python_version()')
77+
echo "Python version: $PYTHON_VERSION"
78+
echo "PYTHON_VERSION=$PYTHON_VERSION" >> $GITHUB_OUTPUT
79+
80+
- name: Configure AWS credentials
81+
if: steps.check-release.outputs.skip_publish != 'true'
82+
uses: aws-actions/configure-aws-credentials@v2
83+
with:
84+
aws-access-key-id: ${{ secrets.ECR_AWS_ACCESS_KEY_ID }}
85+
aws-secret-access-key: ${{ secrets.ECR_AWS_SECRET_ACCESS_KEY }}
86+
aws-region: us-east-1 # required for Public ECR
87+
88+
- name: Login to AWS Public ECR
89+
if: steps.check-release.outputs.skip_publish != 'true'
90+
id: login-ecr
91+
uses: aws-actions/amazon-ecr-login@v1
92+
with:
93+
registry-type: public
94+
95+
- name: Build and push Docker image
96+
if: steps.check-release.outputs.skip_publish != 'true'
97+
env:
98+
VERSION: ${{ steps.set-version.outputs.VERSION }}
99+
ECR_REGISTRY: public.ecr.aws/g0e9g3b1
100+
ECR_REPOSITORY: decode-cloud/user-api
101+
PYTHON_VERSION: ${{ steps.get-python-version.outputs.PYTHON_VERSION }}
102+
run: |
103+
echo "Building image: $ECR_REGISTRY/$ECR_REPOSITORY:$VERSION"
104+
docker build --build-arg PYTHON_VERSION=$PYTHON_VERSION -t $ECR_REGISTRY/$ECR_REPOSITORY:$VERSION .
105+
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$VERSION
106+
107+
if [[ "$VERSION" != dev-* ]]; then
108+
echo "Tagging as latest"
109+
docker tag $ECR_REGISTRY/$ECR_REPOSITORY:$VERSION $ECR_REGISTRY/$ECR_REPOSITORY:latest
110+
docker push $ECR_REGISTRY/$ECR_REPOSITORY:latest
111+
fi
112+
113+
echo "## 🚀 Docker Image Published" >> $GITHUB_STEP_SUMMARY
114+
echo "\`$ECR_REGISTRY/$ECR_REPOSITORY:$VERSION\`" >> $GITHUB_STEP_SUMMARY
115+
if [[ "$VERSION" != dev-* ]]; then
116+
echo "" >> $GITHUB_STEP_SUMMARY
117+
echo "Also tagged as: \`$ECR_REGISTRY/$ECR_REPOSITORY:latest\`" >> $GITHUB_STEP_SUMMARY
118+
fi
119+
120+
- name: Create and push annotated git tag
121+
if: steps.check-release.outputs.skip_publish != 'true' && steps.set-version-type.outputs.VERSION_TYPE == 'prod'
122+
env:
123+
VERSION: ${{ steps.set-version.outputs.VERSION }}
124+
run: |
125+
git config user.name "github-actions"
126+
git config user.email "github-actions@github.com"
127+
git tag -a "$VERSION" -m "Release $VERSION"
128+
git push origin "$VERSION"
129+
130+
- name: Create GitHub release
131+
if: steps.check-release.outputs.skip_publish != 'true' && steps.set-version-type.outputs.VERSION_TYPE == 'prod'
132+
uses: softprops/action-gh-release@v1
133+
with:
134+
tag_name: ${{ steps.set-version.outputs.VERSION }}
135+
name: Release ${{ steps.set-version.outputs.VERSION }}
136+
generate_release_notes: true

0 commit comments

Comments
 (0)