Skip to content

Commit

Permalink
Add CI pipeline (#21)
Browse files Browse the repository at this point in the history
* Add CI

* Add Java version

* Add install go for lint and test

* Lint only generated files

* Check Go version

* Lint and test only generated files

* Change branch for testing

* Fix typo

* Trigger pipeline

* Readd .git folder

* Remove multiple file generation

* Readd multiple file generation

* Remove check-sync-tidy

* Rename variables

* Remove unused variable

* Rename SDK repo clone path

* Change description

* Rename variable

* Remove .git from clone before push

* Remove test echo

* Rename action

* Move SDK generation to separate action

* Add shell

* Fix SDK folder path

* Remove test branch

* Fix using old SDK path

* Change name of SDK repo folder

* Remove lint

---------

Co-authored-by: Henrique Santos <[email protected]>
  • Loading branch information
hcsa73 and Henrique Santos authored Oct 10, 2023
1 parent e4f4302 commit 2277bf9
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .github/actions/build/action.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Build
description: "Build pipeline"
description: "Build tools"
inputs:
go-version:
description: "Go version to install"
Expand Down
15 changes: 15 additions & 0 deletions .github/actions/generate-sdk/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Generate SDK
description: "Generates the SDK"
inputs:
go-version:
description: "Go version to install"
required: true
runs:
using: "composite"
steps:
- name: Download OAS
shell: bash
run: make download-oas
- name: Generate SDK
shell: bash
run: make generate-sdk
46 changes: 46 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: CI Workflow

on: [pull_request, workflow_dispatch]

env:
GO_VERSION_BUILD: '1.20'
JAVA_VERSION: '11'

jobs:
main:
name: CI
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
go-version: ['1.18', '1.19', '1.20']
runs-on: ${{ matrix.os }}
steps:
- name: Install SSH Key
uses: shimataro/ssh-key-action@v2
with:
key: ${{ secrets.SSH_PRIVATE_KEY }}
known_hosts: ${{ vars.SSH_KNOWN_HOSTS }}
- name: Install Java
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: ${{ env.JAVA_VERSION }}
- name: Checkout
uses: actions/checkout@v3
- name: Build
uses: ./.github/actions/build
with:
go-version: ${{ env.GO_VERSION_BUILD }}
- name: Generate SDK
uses: ./.github/actions/generate-sdk
- name: Install Go ${{ matrix.go-version }}
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
- name: Lint
if: ${{ matrix.go-version == '1.19' || matrix.go-version == '1.20' }}
working-directory: ./sdk-repo-updated
run: make lint skip-non-generated-files=true
- name: Test
working-directory: ./sdk-repo-updated
run: make test skip-non-generated-files=true
4 changes: 1 addition & 3 deletions .github/workflows/sdk-pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@ jobs:
uses: ./.github/actions/build
with:
go-version: ${{ env.GO_VERSION }}
- name: Download OAS
run: make download-oas
- name: Generate SDK
run: make generate-sdk
uses: ./.github/actions/generate-sdk
- name: Push SDK
env:
GH_REPO: 'stackitcloud/stackit-sdk-go'
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
bin/

# Folders to support generation
sdk/
sdk-repo-updated/
oas/

# Generated files
Expand Down
60 changes: 28 additions & 32 deletions scripts/generate-sdk/generate-sdk.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#!/bin/bash
# This script generates the SDK API modules
# This script clones the SDK repo and updates it with the generated API modules
# Pre-requisites: Java, goimports, Go
set -eo pipefail

ROOT_DIR=$(git rev-parse --show-toplevel)
GENERATOR_PATH="${ROOT_DIR}/scripts/bin"
GENERATOR_LOG_LEVEL="error" # Must be a Java log level (error, warn, info...)
PREPARE_SDK_PATH="${ROOT_DIR}/prepare-sdk"
SDK_PATH="${ROOT_DIR}/sdk"
SDK_REPO="https://github.com/stackitcloud/stackit-sdk-go.git"
SDK_REPO_LOCAL_PATH="${ROOT_DIR}/sdk-repo-updated"
SDK_REPO_URL="https://github.com/stackitcloud/stackit-sdk-go.git"
SDK_GO_VERSION="1.18"
OAS_REPO=https://github.com/stackitcloud/stackit-api-specifications

Expand All @@ -24,7 +23,6 @@ if [[ ! ${sdk_services_backup_dir} || -d {sdk_services_backup_dir} ]]; then
fi

cleanup() {
rm -rf ${SDK_PATH}/.git
rm -rf ${sdk_services_backup_dir}
go env -w GOPRIVATE=${goprivate_backup}
}
Expand Down Expand Up @@ -66,35 +64,34 @@ else
echo "Download done."
fi

# Get latest version of SDK
if [ -d ${ROOT_DIR}/sdk ]; then
echo "Old sdk was found, it will be removed"
rm -rf ${ROOT_DIR}/sdk
mkdir ${ROOT_DIR}/sdk
# Clone SDK repo
if [ -d ${SDK_REPO_LOCAL_PATH} ]; then
echo "Old SDK repo clone was found, it will be removed"
rm -rf ${SDK_REPO_LOCAL_PATH}
fi
git clone ${SDK_REPO} ${SDK_PATH}
git clone ${SDK_REPO_URL} ${SDK_REPO_LOCAL_PATH}

# Install SDK project tools
cd ${SDK_PATH}
cd ${SDK_REPO_LOCAL_PATH}
make project-tools

# Set GOPRIVATE to download the SDK Go module
goprivate_backup=$(go env GOPRIVATE)
go env -w GOPRIVATE="github.com/stackitcloud"

# Save and remove SDK services/
cp -a "${SDK_PATH}/services/." ${sdk_services_backup_dir}
rm -rf ${SDK_PATH}/services
rm ${SDK_PATH}/go.work
if [ -f "${SDK_PATH}/go.work.sum" ]; then
rm ${SDK_PATH}/go.work.sum
cp -a "${SDK_REPO_LOCAL_PATH}/services/." ${sdk_services_backup_dir}
rm -rf ${SDK_REPO_LOCAL_PATH}/services
rm ${SDK_REPO_LOCAL_PATH}/go.work
if [ -f "${SDK_REPO_LOCAL_PATH}/go.work.sum" ]; then
rm ${SDK_REPO_LOCAL_PATH}/go.work.sum
fi

# Cleanup after we are done
trap cleanup EXIT

echo "go ${SDK_GO_VERSION}" >${SDK_PATH}/go.work
cd ${SDK_PATH}/core
echo "go ${SDK_GO_VERSION}" >${SDK_REPO_LOCAL_PATH}/go.work
cd ${SDK_REPO_LOCAL_PATH}/core
go work use .
for service_json in ${ROOT_DIR}/oas/*.json; do
service="${service_json##*/}"
Expand Down Expand Up @@ -123,44 +120,43 @@ for service_json in ${ROOT_DIR}/oas/*.json; do
cd ${ROOT_DIR}

GO_POST_PROCESS_FILE="gofmt -w" \
mkdir -p ${SDK_PATH}/services/${service}/
cp ${ROOT_DIR}/scripts/generate-sdk/.openapi-generator-ignore ${SDK_PATH}/services/${service}/
mkdir -p ${SDK_REPO_LOCAL_PATH}/services/${service}/
cp ${ROOT_DIR}/scripts/generate-sdk/.openapi-generator-ignore ${SDK_REPO_LOCAL_PATH}/services/${service}/
java -Dlog.level=${GENERATOR_LOG_LEVEL} -jar ${jar_path} generate \
--generator-name go \
--input-spec ${service_json} \
--output ${SDK_PATH}/services/${service} \
--output ${SDK_REPO_LOCAL_PATH}/services/${service} \
--package-name ${service} \
--template-dir ${ROOT_DIR}/templates/ \
--enable-post-process-file \
--git-user-id stackitcloud \
--git-repo-id stackit-sdk-go \
--global-property apis,models,modelTests=true,modelDocs=false,apiDocs=false,supportingFiles \
--additional-properties=isGoSubmodule=true
rm ${SDK_PATH}/services/${service}/.openapi-generator-ignore
rm ${SDK_PATH}/services/${service}/.openapi-generator/FILES
rm ${SDK_REPO_LOCAL_PATH}/services/${service}/.openapi-generator-ignore
rm ${SDK_REPO_LOCAL_PATH}/services/${service}/.openapi-generator/FILES

# Move tests to the service folder
cp ${SDK_PATH}/services/${service}/test/* ${SDK_PATH}/services/${service}
rm -r ${SDK_PATH}/services/${service}/test/
cp ${SDK_REPO_LOCAL_PATH}/services/${service}/test/* ${SDK_REPO_LOCAL_PATH}/services/${service}
rm -r ${SDK_REPO_LOCAL_PATH}/services/${service}/test/

# If the service has a wait package files, move them inside the service folder
if [ -d ${sdk_services_backup_dir}/${service}/wait ]; then
echo "Found ${service}/wait package"
cp -r ${sdk_services_backup_dir}/${service}/wait ${SDK_PATH}/services/${service}/wait
cp -r ${sdk_services_backup_dir}/${service}/wait ${SDK_REPO_LOCAL_PATH}/services/${service}/wait
fi

cd ${SDK_PATH}/services/${service}
cd ${SDK_REPO_LOCAL_PATH}/services/${service}
go work use .
go mod tidy
done

# Add examples to workspace
for example_dir in ${SDK_PATH}/examples/*; do
for example_dir in ${SDK_REPO_LOCAL_PATH}/examples/*; do
cd ${example_dir}
go work use .
done

# Cleanup after SDK generation
cd ${SDK_PATH}
goimports -w ${SDK_PATH}/services/
make lint
cd ${SDK_REPO_LOCAL_PATH}
goimports -w ${SDK_REPO_LOCAL_PATH}/services/
14 changes: 9 additions & 5 deletions scripts/sdk-create-pr.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set -eo pipefail
ROOT_DIR=$(git rev-parse --show-toplevel)
COMMIT_NAME="SDK Generator Bot"
COMMIT_EMAIL="[email protected]"
FOLDER_TO_PUSH_PATH="${ROOT_DIR}/sdk" # Comes from generate-sdk.sh
SDK_REPO_LOCAL_PATH="${ROOT_DIR}/sdk-repo-updated" # Comes from generate-sdk.sh
REPO_URL_SSH="[email protected]:stackitcloud/stackit-sdk-go.git"
REPO_BRANCH="main"

Expand All @@ -14,7 +14,7 @@ if [ $# -ne 4 ]; then
exit 1
fi

if [ ! -d ${FOLDER_TO_PUSH_PATH} ]; then
if [ ! -d ${SDK_REPO_LOCAL_PATH} ]; then
echo "sdk to commit not found in root. Please run make generate-sdk"
exit 1
fi
Expand All @@ -29,8 +29,12 @@ fi
# Delete temp directory on exit
trap "rm -rf ${work_dir}" EXIT

# Where the git repo will be created
mkdir ${work_dir}/git_repo
mkdir ${work_dir}/git_repo # Where the git repo will be created
mkdir ${work_dir}/sdk_to_push # Copy of SDK to push

# Prepare SDK to push
cp -a ${SDK_REPO_LOCAL_PATH}/. ${work_dir}/sdk_to_push
rm -rf ${work_dir}/sdk_to_push/.git

# Initialize git repo
cd ${work_dir}/git_repo
Expand All @@ -42,7 +46,7 @@ git config user.email "${COMMIT_EMAIL}"
# Removal of pulled data is necessary because the old version may have files
# that were deleted in the new version
rm -rf ./*
cp -a ${FOLDER_TO_PUSH_PATH}/. ./
cp -a ${work_dir}/sdk_to_push/. ./

# Create PR with new SDK if there are changes
git switch -c "$1"
Expand Down

0 comments on commit 2277bf9

Please sign in to comment.