Skip to content

Commit

Permalink
Get swagger docs from docker image
Browse files Browse the repository at this point in the history
  • Loading branch information
Jozef Volak committed Feb 7, 2024
1 parent 9a77d73 commit b9aaa76
Showing 1 changed file with 80 additions and 46 deletions.
126 changes: 80 additions & 46 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ name: Docker

on:
push:
# Publish `master` as Docker `latest` image.
branches:
- master
- 'master' # produce latest image
- '[0-9]+.[0-9]+-stable' # produce stable-latest image

# Publish `vfrinx2.0.3` tags as releases.
tags:
- vfrinx2*
- 'vfrinx-[0-9]+.[0-9]+.[0-9]' # produce release image (6.0.0)
- 'vfrinx-[0-9]+.[0-9]+.[0-9]-stable' # produce stable release image (5.1.0)

workflow_dispatch:

env:
# TODO: Change variable to your image's name.
Expand All @@ -17,55 +19,87 @@ env:
jobs:
# Push image to GitHub Packages.
# See also https://docs.docker.com/docker-hub/builds/
push-v2:
build-image-v2:
runs-on: ubuntu-latest
if: github.event_name == 'push'

# Dependency for swagger export
services:
postgres:
image: postgres
ports:
- 5432:5432
env:
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
fetch-depth: 0
submodules: recursive

- name: Build image
run: docker build . --file docker/server/Dockerfile --tag $IMAGE_NAME --build-arg git_commit=$(git rev-parse HEAD)
submodules: recursive # fetch submodules

- name: Log into docker hub
run: echo "${{ secrets.DOCKER_HUB_PASSWORD }}" | docker login -u ${{ secrets.DOCKER_HUB_USERNAME }} --password-stdin

- name: Push image
- name: Build image
run: docker build . --file docker/server/Dockerfile --tag $IMAGE_NAME:latest --build-arg git_commit=$(git rev-parse HEAD)

# Start conductor for swagger export with network to postgres service
- name: Run conductor image
run: docker run -d --network "$(docker network ls --filter type=custom --format '{{ .Name }}')" --publish 8080:8080 $IMAGE_NAME:latest

#### Set tags based on source type ####

- name: Set tag for latest image
if: github.ref_type == 'branch' && startsWith(github.ref, 'refs/heads/master')
run: |
echo "IMAGE_TAG=$(echo 'latest')" >> "$GITHUB_ENV"
- name: Set tag for stable-latest image
if: github.ref_type == 'branch' && endsWith(github.ref, '-stable')
run: |
echo "IMAGE_TAG=$(echo 'stable-latest')" >> "$GITHUB_ENV"
- name: Set tag for stable-release image
if: github.ref_type == 'tag' && endsWith(github.ref, '-stable')
run: |
echo "IMAGE_TAG=$(echo $GITHUB_REF | cut -d / -f 3 | sed -e 's/^vfrinx-//' | sed -e 's/-stable//')" >> "$GITHUB_ENV"
- name: Set tag for release image
if: ${{ github.ref_type == 'tag' && !endsWith(github.ref, '-stable') }}
run: |
echo "IMAGE_TAG=$(echo $GITHUB_REF | cut -d / -f 3 | sed -e 's/^vfrinx-//')" >> "$GITHUB_ENV"
# Tag and publish docker image
- name: Push stable-release image
docker tag $IMAGE_NAME:latest $IMAGE_NAME:${IMAGE_TAG}
docker push $IMAGE_NAME:${IMAGE_TAG}

#### OpenAPI publish ####

- name: Get conductor openapi spec
run: curl http://localhost:8080/v3/api-docs -o openapi.json

- name: Check out swagger_docs branch
uses: actions/checkout@v4
with:
ref: swagger_docs
path: swagger_docs
fetch-depth: 2
token: ${{ secrets.GH_PAT_TOKEN }}

- name: Copy files to swagger_docs
run: |
IMAGE_ID=$IMAGE_NAME
# Change all uppercase to lowercase
IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]')
# Strip git ref prefix from version
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
# Strip "v" prefix from tag name
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^vfrinx//')
# Use Docker `latest` tag convention
[ "$VERSION" == "master" ] && VERSION=latest
echo IMAGE_ID=$IMAGE_ID
echo VERSION=$VERSION
docker tag $IMAGE_NAME $IMAGE_ID:$VERSION
docker push $IMAGE_ID:$VERSION
# Add latest tag if we are on master and github.ref points to a tag
if [[ "${{ github.ref }}" == "refs/tags/"* ]]; then
MASTER=$(git show-ref --hash origin/master)
echo "master: $MASTER"
HEAD=$(git rev-parse HEAD)
echo "head: $HEAD"
echo "github.ref ${{ github.ref }}"
if [[ $MASTER == $HEAD ]]; then
VERSION=latest
echo VERSION=$VERSION
docker tag $IMAGE_NAME $IMAGE_ID:$VERSION
docker push $IMAGE_ID:$VERSION
fi
fi
GIT_HASH=$(git rev-parse --short $GITHUB_SHA)
jq . openapi.json > swagger_docs/${IMAGE_TAG}.json
pushd swagger_docs || exit 1
git config -l --show-scope
git config --global user.email "[email protected]"
git config --global user.name "FRINXrepo"
git add *.json
(git commit -am "Updated swagger for ${GITHUB_REF_NAME}/${GIT_HASH}" && git push) || true
popd

0 comments on commit b9aaa76

Please sign in to comment.