1- name : Create and publish Docker Compose stack to GHCR
1+ name : Create and publish Docker Compose stack to ghcr
22
33on :
44 push :
1010
1111env :
1212 REGISTRY : ghcr.io
13- IMAGE_NAME : ${{ github.repository }}
13+ BACKEND_IMAGE_NAME : ${{ github.repository }}-backend
14+ FRONTEND_IMAGE_NAME : ${{ github.repository }}-frontend
1415
1516jobs :
16- build-and-push-stack :
17+ build-and-push-images :
1718 runs-on : ubuntu-latest
1819 permissions :
1920 contents : read
@@ -27,128 +28,72 @@ jobs:
2728 with :
2829 fetch-depth : 0
2930
30- - name : Get version info
31+ - name : Get latest tag and commit distance
3132 id : get_version_info
3233 run : |
34+ # Get the latest tag
3335 LATEST_TAG=$(git describe --tags --abbrev=0)
36+
37+ # Remove 'v' prefix from latest tag
3438 BASE_VERSION=${LATEST_TAG#v}
39+
40+ # Get the number of commits since the last tag
3541 COMMIT_DISTANCE=$(git rev-list --count ${LATEST_TAG}..HEAD)
36- NEW_VERSION="v${BASE_VERSION}.${COMMIT_DISTANCE}"
42+
43+ # Construct new version with build number
44+ NEW_VERSION="${BASE_VERSION}.${COMMIT_DISTANCE}"
45+
3746 echo "version=${NEW_VERSION}" >> $GITHUB_OUTPUT
3847 echo "Generated version: ${NEW_VERSION}"
3948
40- - name : Log in to GHCR
49+ - name : Log in to the Container registry
4150 uses : docker/login-action@v3
4251 with :
4352 registry : ${{ env.REGISTRY }}
4453 username : ${{ github.actor }}
4554 password : ${{ secrets.GITHUB_TOKEN }}
4655
47- - name : Set up Docker Buildx
48- uses : docker/setup-buildx-action@v3
56+ # Build and push backend image
57+ - name : Extract metadata for Backend
58+ id : meta-backend
59+ uses : docker/metadata-action@v5
60+ with :
61+ images : ${{ env.REGISTRY }}/${{ env.BACKEND_IMAGE_NAME }}
62+ tags : |
63+ type=raw,value=${{ steps.get_version_info.outputs.version }}
64+ type=raw,value=latest
4965
50- # Build and push backend
51- - name : Build and push backend
66+ - name : Build and push Backend image
67+ id : push- backend
5268 uses : docker/build-push-action@v5
5369 with :
5470 context : ./backend/api
5571 file : ./backend/api/Dockerfile
5672 push : true
57- tags : ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}/backend:${{ steps.get_version_info.outputs.version }}
73+ tags : ${{ steps.meta-backend.outputs.tags }}
74+ labels : ${{ steps.meta-backend.outputs.labels }}
75+ build-args : |
76+ DATABASE_URL=postgresql://admin:saisab@postgres:5432/rust_sqlx?schema=public
77+
78+ # Build and push frontend image
79+ - name : Extract metadata for Frontend
80+ id : meta-frontend
81+ uses : docker/metadata-action@v5
82+ with :
83+ images : ${{ env.REGISTRY }}/${{ env.FRONTEND_IMAGE_NAME }}
84+ tags : |
85+ type=raw,value=${{ steps.get_version_info.outputs.version }}
86+ type=raw,value=latest
5887
59- # Build and push frontend
60- - name : Build and push frontend
88+ - name : Build and push Frontend image
89+ id : push- frontend
6190 uses : docker/build-push-action@v5
6291 with :
6392 context : .
6493 file : ./Dockerfile
6594 push : true
66- tags : ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}/frontend:${{ steps.get_version_info.outputs.version }}
67-
68- # Create and push the stack configuration
69- - name : Create and push stack configuration
70- env :
71- VERSION : ${{ steps.get_version_info.outputs.version }}
72- REPO : ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
73- run : |
74- # Create production docker-compose file
75- cat > docker-compose.prod.yml << EOL
76- services:
77- postgres:
78- image: postgres:latest
79- container_name: postgres_prod
80- ports:
81- - "6500:5432"
82- volumes:
83- - progresDB:/var/lib/postgresql/data
84- env_file:
85- - ./.env
86- networks:
87- - app_network_prod
88-
89- pgAdmin:
90- image: dpage/pgadmin4
91- container_name: pgAdmin_prod
92- env_file:
93- - ./.env
94- ports:
95- - "5050:80"
96- networks:
97- - app_network_prod
98-
99- backend:
100- image: $REPO/backend:$VERSION
101- container_name: cardanoapiio_backend
102- ports:
103- - "8000:8000"
104- depends_on:
105- - postgres
106- environment:
107- DATABASE_URL: postgresql://admin:saisab@postgres:5432/rust_sqlx?schema=public
108- networks:
109- - app_network_prod
110-
111- frontend:
112- image: $REPO/frontend:$VERSION
113- container_name: nextjs_frontend_prod
114- ports:
115- - "3000:3000"
116- environment:
117- API_URL: http://backend:8000
118- NODE_ENV: production
119- restart: always
120- depends_on:
121- - backend
122- networks:
123- - app_network_prod
124-
125- networks:
126- app_network_prod:
127- driver: bridge
128-
129- volumes:
130- progresDB:
131- EOL
132-
133- # Create a dummy .env file if it doesn't exist
134- touch .env
135-
136- # Package and push the stack
137- tar -czf stack.tar.gz docker-compose.prod.yml .env
138-
139- # Create and push the stack image
140- docker buildx build --push \
141- --tag $REPO/stack:$VERSION \
142- --label "org.opencontainers.image.source=https://github.com/${{ github.repository }}" \
143- --platform linux/amd64 \
144- --file - . << EOF
145- FROM scratch
146- COPY stack.tar.gz /
147- EOF
148-
149- # - name: Generate stack attestation
150- # uses: actions/attest-build-provenance@v1
151- # with:
152- # subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}/stack
153- # subject-digest: sha256:${{ steps.push.outputs.digest }}
154- # push-to-registry: true
95+ tags : ${{ steps.meta-frontend.outputs.tags }}
96+ labels : ${{ steps.meta-frontend.outputs.labels }}
97+ build-args : |
98+ API_URL=http://backend:8000
99+ NODE_ENV=production
0 commit comments