diff --git a/.circleci/config.yml b/.circleci/config.yml index 8fb5c1b2..97f404e0 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,4 +1,18 @@ version: 2.1 + +parameters: + run-workflow: + type: boolean + default: true + release-version: + type: string + default: "" + description: "Version to release (e.g., 1.2.3)" + dry-run: + type: boolean + default: false + description: "If true, show what would happen but don't make actual changes" + commands: test_bindings: description: "Test the bindings for a language" @@ -72,11 +86,157 @@ jobs: - run: name: "Run pre-commit" command: pre-commit run --all-files + update_version: + description: "Update the API version in configuration files" + docker: + - image: cimg/base:stable-20.04 + steps: + - checkout + - run: + name: Update Version + command: | + if [ -z "<< pipeline.parameters.release-version >>" ]; then + echo "Error: release-version parameter is required" + exit 1 + fi + + sed -i "s/package_version=.*/package_version=\"<< pipeline.parameters.release-version >>\"/" scripts/common.sh + + echo "Updated package_version to << pipeline.parameters.release-version >> in scripts/common.sh" + - persist_to_workspace: + root: . + paths: + - . + generate_bindings: + description: "Generate bindings with the updated version" + machine: + image: ubuntu-2004:current + steps: + - attach_workspace: + at: . + - run: + name: Generate Bindings + command: | + ./scripts/build.sh + - persist_to_workspace: + root: . + paths: + - . + create_pr: + description: "Create a PR with the updated bindings" + docker: + - image: cimg/base:stable-20.04 + steps: + - attach_workspace: + at: . + - run: + name: Install GitHub CLI + command: | + curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg + echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null + sudo apt update + sudo apt install gh -y + - run: + name: Add github.com to known hosts + command: | + mkdir -p ~/.ssh + echo 'github.com ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCj7ndNxQowgcQnjshcLrqPEiiphnt+VTTvDP6mHBL9j1aNUkY4Ue1gvwnGLVlOhGeYrnZaMgRK6+PKCUXaDbC7qtbW8gIkhL7aGCsOr/C56SJMy/BCZfxd1nWzAOxSDPgVsmerOBYfNqltV9/hWCqBywINIR+5dIg6JTJ72pcEpEjcYgXkE2YEFXV1JHnsKgbLWNlhScqb2UmyRkQyytRLtL+38TGxkxCflmO+5Z8CSSNY7GidjMIZ7Q4zMjA2n1nGrlTDkzwDCsw+wqFPGQA179cnfGWOWRVruj16z6XyvxvjJwbz0wQZ75XK5tKSb7FNyeIEs4TT4jk+S4dhPeAUC5y+bDYirYgM4GC7uEnztnZyaVWQ7B381AK4Qdrwt51ZqExKbQpTUNn+EjqoTwvqNj4kqx5QUCI0ThS/YkOxJCXmPUWZbhjpCg56i+2aB6CmK2JGhn57K5mj0MNdBXA4/WnwH6XoPWJzK5Nyu2zB3nAZp+S5hpQs+p1vN1/wsjk=' >> ~/.ssh/known_hosts + - run: + name: Configure SSH for write access + command: | + # Create SSH config to use identity file only + mkdir -p ~/.ssh + echo -e "Host *\n IdentitiesOnly=yes" > ~/.ssh/config + chmod 600 ~/.ssh/config + + # Remove all existing keys from ssh-agent + ssh-add -D || true + + # Add CI_SSH_KEY if available + if [ -n "$CI_SSH_KEY" ]; then + echo "$CI_SSH_KEY" > ~/.ssh/id_rsa + chmod 600 ~/.ssh/id_rsa + ssh-add ~/.ssh/id_rsa + fi + - run: + name: Extract Package Version and Create PR + command: | + set -x + + # Debug environment + echo "GH_TOKEN is set: $([[ -n $GH_TOKEN ]] && echo 'YES' || echo 'NO')" + echo "GH_USER is set: $([[ -n $GH_USER ]] && echo 'YES' || echo 'NO')" + + # Get API version from common.sh + echo "Extracting package_version from scripts/common.sh" + PACKAGE_VERSION=$(sed -n 's/package_version="\(.*\)"/\1/p' scripts/common.sh) + + # If sed fails, try method 2: Using cut and grep + if [ -z "$PACKAGE_VERSION" ]; then + echo "Sed extraction failed, trying alternative method" + PACKAGE_VERSION=$(grep "package_version=" scripts/common.sh | cut -d'"' -f2) + fi + + # Verify we got the version + if [ -z "$PACKAGE_VERSION" ]; then + echo "Failed to extract package_version from scripts/common.sh" + echo "Content of scripts/common.sh:" + cat scripts/common.sh + exit 1 + fi + + echo "Found package_version: $PACKAGE_VERSION" + + # Set up git user identity + git config --global user.email "${GH_USER}@users.noreply.github.com" + git config --global user.name "${GH_USER}" + + # Check for dry run mode + if [ "${DRY_RUN}" == "true" ]; then + echo "🔍 DRY RUN MODE: Would perform the following actions:" + echo "- Create branch: update-api-bindings-v${PACKAGE_VERSION}" + echo "- Commit and push changes" + echo "- Create PR with title: Update API bindings to version ${PACKAGE_VERSION}" + echo "- PR body: This PR updates the API bindings to version ${PACKAGE_VERSION}." + echo "- Target repo: cloudsmith-io/cloudsmith-api" + echo "⚠️ No actual changes will be made in dry run mode" + exit 0 + fi + + # Create branch + BRANCH_NAME="update-api-bindings-v${PACKAGE_VERSION}" + echo "Creating branch: $BRANCH_NAME" + git checkout -b ${BRANCH_NAME} + + # Check if there are changes to commit + echo "Checking for changes" + git diff --stat + + if git diff --quiet; then + echo "No changes detected. Skipping PR creation." + exit 0 + fi + + # Commit changes + echo "Committing changes" + git add . + git commit -m "Update API bindings to version ${PACKAGE_VERSION}" + + # Push branch to github + git push --set-upstream origin ${BRANCH_NAME} + + # Create the PR + gh pr create --base main --head ${BRANCH_NAME} --title "Update API bindings to version ${PACKAGE_VERSION}" --body "This PR updates the API bindings to version ${PACKAGE_VERSION}." --repo cloudsmith-io/cloudsmith-api + environment: + GH_TOKEN: ${GH_TOKEN} + GH_USER: ${GH_USER} + DRY_RUN: << pipeline.parameters.dry-run >> workflows: version: 2 test_and_deploy: + when: << pipeline.parameters.run-workflow >> jobs: - pre_commit - test: @@ -128,3 +288,18 @@ workflows: name: "Deploy Python" language: python docker_image: cimg/python:3.7 + + release_api_bindings: + when: + and: + - << pipeline.parameters.run-workflow >> + - not: + equal: [ "", << pipeline.parameters.release-version >> ] + jobs: + - update_version + - generate_bindings: + requires: + - update_version + - create_pr: + requires: + - generate_bindings \ No newline at end of file