new #12
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: api-ci | |
on: | |
push: | |
branches: | |
- 'main' | |
- 'dev-api' | |
paths: | |
- 'apps/api/**' | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v3 | |
- name: Login to Docker Hub | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v2 | |
- id: get_version | |
name: get API version from package.json | |
run: | | |
version=`cat ./apps/api/package.json | jq -r '.version'` | |
# end of optional handling for multi line json | |
echo "version=$version" >> $GITHUB_OUTPUT | |
- id: dev_image_tag | |
name: Set tag for dev env | |
run: | | |
echo "IMAGE=platform-api-dev" >> $GITHUB_ENV | |
echo "NAME=dev-api" >> $GITHUB_ENV | |
echo "PORT=8010" >> $GITHUB_ENV | |
if: ${{ github.ref_name == 'dev-api' }} | |
- id: stage_image_tag | |
name: Set tag for stage env | |
run: | | |
echo "IMAGE=platform-api-stage" >> $GITHUB_ENV | |
echo "NAME=stage-api" >> $GITHUB_ENV | |
echo "PORT=8000" >> $GITHUB_ENV | |
if: ${{ github.ref_name == 'main' }} | |
- id: image_tag | |
name: Get the final tags for deployment outputs | |
run: | | |
echo "container_image=${{env.IMAGE}}" >> $GITHUB_OUTPUT | |
echo "container_name=${{env.NAME}}" >> $GITHUB_OUTPUT | |
echo "container_port=${{env.PORT}}" >> $GITHUB_OUTPUT | |
- name: Build and push | |
id: build_and_push | |
uses: docker/build-push-action@v4 | |
with: | |
context: . | |
file: ./apps/api/Dockerfile | |
push: true | |
tags: ${{ secrets.DOCKERHUB_USERNAME }}/${{ steps.image_tag.outputs.container_image }}:latest, ${{ secrets.DOCKERHUB_USERNAME }}/${{ steps.image_tag.outputs.container_image }}:v${{steps.get_version.outputs.version}} | |
outputs: | |
container_image: ${{ steps.image_tag.outputs.container_image }} | |
container_name: ${{ steps.image_tag.outputs.container_name }} | |
container_port: ${{ steps.image_tag.outputs.container_port }} | |
container_tag: v${{steps.get_version.outputs.version}} | |
deploy-stage: | |
runs-on: ubuntu-latest | |
needs: build | |
env: | |
CONTAINER_IMAGE: ${{needs.build.outputs.container_image}} | |
CONTAINER_NAME: ${{needs.build.outputs.container_name}} | |
CONTAINER_TAG: ${{needs.build.outputs.container_tag}} | |
CONTAINER_PORT: ${{needs.build.outputs.container_port}} | |
steps: | |
- name: deploy via ssh | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.STAGE_SSH_HOST }} | |
username: ${{ secrets.STAGE_SSH_USERNAME }} | |
key: ${{ secrets.STAGE_SSH_KEY }} | |
envs: CONTAINER_NAME,CONTAINER_PORT,CONTAINER_TAG,CONTAINER_IMAGE | |
passphrase: ${{ secrets.STAGE_SSH_PASSPHRASE }} | |
script: | | |
# pull image | |
docker pull ${{ secrets.DOCKERHUB_USERNAME }}/$(echo $CONTAINER_IMAGE):$(echo $CONTAINER_TAG) | |
# stop existing one | |
docker stop $(echo $CONTAINER_NAME) || echo 'no container to stop' | |
# Remove old container | |
docker rm $(echo $CONTAINER_NAME) || echo 'no container to remove' | |
# Run a new container from a new image | |
docker run -d --restart always --name $(echo $CONTAINER_NAME) -p $(echo $CONTAINER_PORT):8000 --env-file ~/.env.$(echo $CONTAINER_NAME) ${{ secrets.DOCKERHUB_USERNAME }}/$(echo $CONTAINER_IMAGE):$(echo $CONTAINER_TAG) |