From 2277bf9735c7c93095efe93b1bd1a5eb556ba84b Mon Sep 17 00:00:00 2001 From: Henrique Santos <118177985+hcsa73@users.noreply.github.com> Date: Tue, 10 Oct 2023 12:31:42 +0100 Subject: [PATCH] Add CI pipeline (#21) * 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 --- .github/actions/build/action.yaml | 2 +- .github/actions/generate-sdk/action.yaml | 15 ++++++ .github/workflows/ci.yaml | 46 ++++++++++++++++++ .github/workflows/sdk-pr.yaml | 4 +- .gitignore | 2 +- scripts/generate-sdk/generate-sdk.sh | 60 +++++++++++------------- scripts/sdk-create-pr.sh | 14 ++++-- 7 files changed, 101 insertions(+), 42 deletions(-) create mode 100644 .github/actions/generate-sdk/action.yaml create mode 100644 .github/workflows/ci.yaml diff --git a/.github/actions/build/action.yaml b/.github/actions/build/action.yaml index 666295f..28bc8bb 100644 --- a/.github/actions/build/action.yaml +++ b/.github/actions/build/action.yaml @@ -1,5 +1,5 @@ name: Build -description: "Build pipeline" +description: "Build tools" inputs: go-version: description: "Go version to install" diff --git a/.github/actions/generate-sdk/action.yaml b/.github/actions/generate-sdk/action.yaml new file mode 100644 index 0000000..84d3ad6 --- /dev/null +++ b/.github/actions/generate-sdk/action.yaml @@ -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 \ No newline at end of file diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..4197d59 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -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 \ No newline at end of file diff --git a/.github/workflows/sdk-pr.yaml b/.github/workflows/sdk-pr.yaml index f0e939c..ea3f9cd 100644 --- a/.github/workflows/sdk-pr.yaml +++ b/.github/workflows/sdk-pr.yaml @@ -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' diff --git a/.gitignore b/.gitignore index 0959f8b..32ba444 100644 --- a/.gitignore +++ b/.gitignore @@ -6,7 +6,7 @@ bin/ # Folders to support generation -sdk/ +sdk-repo-updated/ oas/ # Generated files diff --git a/scripts/generate-sdk/generate-sdk.sh b/scripts/generate-sdk/generate-sdk.sh index 33a8180..34a2b65 100755 --- a/scripts/generate-sdk/generate-sdk.sh +++ b/scripts/generate-sdk/generate-sdk.sh @@ -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 @@ -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} } @@ -66,16 +64,15 @@ 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 @@ -83,18 +80,18 @@ 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##*/}" @@ -123,12 +120,12 @@ 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 \ @@ -136,31 +133,30 @@ for service_json in ${ROOT_DIR}/oas/*.json; do --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/ diff --git a/scripts/sdk-create-pr.sh b/scripts/sdk-create-pr.sh index da75975..98667a9 100755 --- a/scripts/sdk-create-pr.sh +++ b/scripts/sdk-create-pr.sh @@ -5,7 +5,7 @@ set -eo pipefail ROOT_DIR=$(git rev-parse --show-toplevel) COMMIT_NAME="SDK Generator Bot" COMMIT_EMAIL="noreply@stackit.de" -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="git@github.com:stackitcloud/stackit-sdk-go.git" REPO_BRANCH="main" @@ -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 @@ -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 @@ -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"