From b9aaa76096fe324cf6b61b81508b22d54911475e Mon Sep 17 00:00:00 2001 From: Jozef Volak Date: Mon, 5 Feb 2024 16:44:39 +0100 Subject: [PATCH] Get swagger docs from docker image --- .github/workflows/docker-publish.yml | 126 +++++++++++++++++---------- 1 file changed, 80 insertions(+), 46 deletions(-) diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml index 30b04215db..90049dc7b9 100644 --- a/.github/workflows/docker-publish.yml +++ b/.github/workflows/docker-publish.yml @@ -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. @@ -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 "support@frinx.io" + git config --global user.name "FRINXrepo" + git add *.json + (git commit -am "Updated swagger for ${GITHUB_REF_NAME}/${GIT_HASH}" && git push) || true + popd