diff --git a/.github/scripts/dispatch_new_version_event.py b/.github/scripts/dispatch_new_version_event.py deleted file mode 100644 index 7c3cb64..0000000 --- a/.github/scripts/dispatch_new_version_event.py +++ /dev/null @@ -1,69 +0,0 @@ -import os -import requests -import sys - -def trigger_github_workflow(org, repo, event_type, payload, github_token): - """ - Trigger a GitHub Actions workflow using the repository_dispatch event. - - :param org: GitHub organization or user name (e.g., 'Checkmarx') - :param repo: Repository name (e.g., 'ast-github-action') - :param event_type: The type of event to trigger - :param payload: The client payload to send with the event - :param github_token: Personal Access Token (PAT) with appropriate permissions - """ - url = f'https://api.github.com/repos/{org}/{repo}/dispatches' - headers = { - 'Accept': 'application/vnd.github.v3+json', - 'Authorization': f'token {github_token}' - } - data = { - 'event_type': event_type, - 'client_payload': payload - } - - response = requests.post(url, json=data, headers=headers) - if response.status_code == 204: - print(f'Success: Dispatched {event_type} event to {org}/{repo}.') - else: - print(f'Error: Failed to dispatch event to {org}/{repo}. Status code: {response.status_code}') - print(f'Response: {response.json()}') - sys.exit(1) - -if __name__ == "__main__": - GH_ORG = os.getenv('GH_ORG') - GH_REPO = os.getenv('GH_REPO') - CLI_VERSION = os.getenv('CLI_VERSION', '') - GITHUB_TOKEN = os.getenv('GITHUB_TOKEN') - IS_CLI_RELEASE = os.getenv('IS_CLI_RELEASE', 'false').lower() == 'true' - IS_JS_RELEASE = os.getenv('IS_JS_RELEASE', 'false').lower() == 'true' - IS_JS_RUNTIME_RELEASE = os.getenv('IS_JS_RUNTIME_RELEASE', 'false').lower() == 'true' - IS_JAVA_RELEASE = os.getenv('IS_JAVA_RELEASE', 'false').lower() == 'true' - - if not GH_ORG or not GH_REPO or not GITHUB_TOKEN: - raise ValueError("GH_ORG, GH_REPO, and GITHUB_TOKEN environment variables must be set.") - - if IS_CLI_RELEASE: - event_type = 'cli-version-update' - payload = { - 'cli_version': CLI_VERSION - } - elif IS_JS_RELEASE: - event_type = 'js-wrapper-version-update' - payload = { - 'cli_version': CLI_VERSION - } - elif IS_JS_RUNTIME_RELEASE: - event_type = 'js-runtime-wrapper-version-update' - payload = { - 'cli_version': CLI_VERSION - } - elif IS_JAVA_RELEASE: - event_type = 'java-wrapper-version-update' - payload = { - 'cli_version': CLI_VERSION - } - else: - raise ValueError("At least one release type environment variable must be set to true.") - - trigger_github_workflow(GH_ORG, GH_REPO, event_type, payload, GITHUB_TOKEN) diff --git a/.github/workflows/dispatch-workflow.yml b/.github/workflows/dispatch-workflow.yml index 95c9b59..5b74865 100644 --- a/.github/workflows/dispatch-workflow.yml +++ b/.github/workflows/dispatch-workflow.yml @@ -1,212 +1,177 @@ -name: Dispatch CLI Version Update +name: Dispatch Plugin Version Update +# Called by the manual "Update dependent plugins" workflow of ast-cli and the +# three wrappers, after their release is published. Sends a repository_dispatch +# event to every repo that consumes the released component, waits for the +# update PRs and lists them in the job summary. on: workflow_call: inputs: - is_cli_release: - type: boolean - default: true - description: 'Workflow will update all repos that directly consume the CLI' - is_js_release: - type: boolean - default: false - description: 'Workflow will update all repos that directly consume the JavaScript Wrapper' - is_js_runtime_release: - type: boolean - default: false - description: 'Workflow will update all repos that directly consume the JavaScript Runtime Wrapper' - is_java_release: - type: boolean - default: false - description: 'Workflow will update all repos that directly consume the Java Wrapper' - cli_version: - description: 'CLI version to update (no input will take latest)' - required: false - default: '' - type: string - secrets: - DISPATCH_GH_TOKEN: + tag: + description: 'Published release tag of the calling repo (e.g. 2.3.71 or v0.0.167)' required: true + type: string permissions: contents: read jobs: - # Group CLI-related updates - update_cli_consumers: - name: Update CLI Consumers + dispatch: + name: Dispatch ${{ inputs.tag }} to consumers runs-on: cx-public-ubuntu-x64 - if: ${{ inputs.is_cli_release == true }} - strategy: - matrix: - repo: [ - { org: 'CheckmarxDev', name: 'ast-cli-java-wrapper' }, - { org: 'Checkmarx', name: 'ast-github-action' }, - { org: 'CheckmarxDev', name: 'ast-cli-javascript-wrapper' }, - { org: 'CheckmarxDev', name: 'ast-cli-javascript-wrapper-runtime-cli' }, - { org: 'Checkmarx', name: 'ast-teamcity-plugin' }, - { org: 'Checkmarx', name: 'ast-visual-studio-extension' } - ] - steps: - - name: Check Out Repository - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0 - with: - persist-credentials: false - - - name: Check Out External Repository - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0 - with: - repository: Checkmarx/plugins-release-workflow - path: plugins-release-workflow - persist-credentials: false - - - name: Set Up Python - uses: actions/setup-python@7f4fc3e22c37d6ff65e88745f38bd3157c663f7c # v4.9.1 - with: - python-version: '3.x' - - - name: Install Dependencies + environment: release + timeout-minutes: 30 + permissions: + contents: read # read the calling repo's release + id-token: write # for configure-aws-credentials to assume the AWS role via OIDC + concurrency: + group: plugins-dispatch-${{ github.repository }}-${{ inputs.tag }} + cancel-in-progress: false + defaults: + run: shell: bash - run: | - python -m pip install --upgrade pip - pip install requests - - - name: Trigger Update Event on ${{ matrix.repo.name }} - env: - GH_ORG: ${{ matrix.repo.org }} - GH_REPO: ${{ matrix.repo.name }} - CLI_VERSION: ${{ inputs.cli_version }} - IS_CLI_RELEASE: 'true' - GITHUB_TOKEN: ${{ secrets.DISPATCH_GH_TOKEN }} - run: python plugins-release-workflow/.github/scripts/dispatch_new_version_event.py - - # Group JavaScript Wrapper-related updates - update_js_wrapper_consumers: - name: Update JavaScript Wrapper Consumers - runs-on: cx-public-ubuntu-x64 - if: ${{ inputs.is_js_release == true }} - strategy: - matrix: - repo: [ - { org: 'Checkmarx', name: 'ast-vscode-extension' } - ] + env: + TAG: ${{ inputs.tag }} steps: - - name: Check Out Repository - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0 - with: - persist-credentials: false - - - name: Check Out External Repository - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0 - with: - repository: Checkmarx/plugins-release-workflow - path: plugins-release-workflow - persist-credentials: false - - - name: Set Up Python - uses: actions/setup-python@7f4fc3e22c37d6ff65e88745f38bd3157c663f7c # v4.9.1 - with: - python-version: '3.x' - - - name: Install Dependencies - shell: bash + - name: Resolve consumers + id: targets run: | - python -m pip install --upgrade pip - pip install requests - - - name: Trigger Update Event on ${{ matrix.repo.name }} + if [ "${GITHUB_REF_NAME}" != "main" ]; then + echo "::error::Run this workflow from main (current ref: ${GITHUB_REF_NAME})." + exit 1 + fi + if ! [[ "${TAG}" =~ ^v?[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + echo "::error::'${TAG}' is not a release tag (expected 1.2.3 or v1.2.3)." + exit 1 + fi + case "${GITHUB_REPOSITORY}" in + Checkmarx/ast-cli) + event=cli-version-update + repos=ast-cli-java-wrapper,ast-cli-javascript-wrapper,ast-cli-javascript-wrapper-runtime-cli,ast-github-action,ast-teamcity-plugin,ast-visual-studio-extension ;; + Checkmarx/ast-cli-javascript-wrapper) + event=js-wrapper-version-update + repos=ast-vscode-extension ;; + Checkmarx/ast-cli-javascript-wrapper-runtime-cli) + event=js-runtime-wrapper-version-update + repos=ast-azure-plugin ;; + Checkmarx/ast-cli-java-wrapper) + event=java-wrapper-version-update + repos=ast-eclipse-plugin,ast-jetbrains-plugin,ast-cli-maven-plugin ;; + *) + echo "::error::${GITHUB_REPOSITORY} has no consumers configured." + exit 1 ;; + esac + { + echo "event=${event}" + echo "repos=${repos}" + } >> "${GITHUB_OUTPUT}" + + - name: Check the release is published and immutable env: - GH_ORG: ${{ matrix.repo.org }} - GH_REPO: ${{ matrix.repo.name }} - CLI_VERSION: ${{ inputs.cli_version }} - IS_JS_RELEASE: 'true' - GITHUB_TOKEN: ${{ secrets.DISPATCH_GH_TOKEN }} - run: python plugins-release-workflow/.github/scripts/dispatch_new_version_event.py - - # Group JavaScript Runtime Wrapper-related updates - update_js_runtime_wrapper_consumers: - name: Update JavaScript Runtime Wrapper Consumers - runs-on: cx-public-ubuntu-x64 - if: ${{ inputs.is_js_runtime_release == true }} - strategy: - matrix: - repo: [ - { org: 'Checkmarx', name: 'ast-azure-plugin' } - ] - steps: - - name: Check Out Repository - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0 - with: - persist-credentials: false - - - name: Check Out External Repository - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0 + GH_TOKEN: ${{ github.token }} + run: | + if ! release=$(gh api "repos/${GITHUB_REPOSITORY}/releases/tags/${TAG}" 2>/dev/null); then + echo "::error::No published release ${TAG} in ${GITHUB_REPOSITORY}. Publish the release first (drafts are not accepted)." + exit 1 + fi + if [ "$(jq -r '.draft or .prerelease' <<< "${release}")" != "false" ]; then + echo "::error::Release ${TAG} is a draft or a pre-release." + exit 1 + fi + if [ "$(jq -r '.immutable' <<< "${release}")" != "true" ]; then + echo "::error::Release ${TAG} is not immutable yet." + exit 1 + fi + echo "Release ${TAG} is published and immutable." + + - name: Configure AWS credentials + uses: step-security/configure-aws-credentials@1448016543d4432bd608e0de7871c7bf9e9be925 # v6.2.1 with: - repository: Checkmarx/plugins-release-workflow - path: plugins-release-workflow - persist-credentials: false + role-to-assume: ${{ secrets.PLUGINS_APP_ROLE_ARN }} + aws-region: ${{ secrets.PLUGINS_APP_AWS_REGION }} + role-session-name: plugins-release-dispatch - - name: Set Up Python - uses: actions/setup-python@7f4fc3e22c37d6ff65e88745f38bd3157c663f7c # v4.9.1 + - name: Fetch GitHub App private key + uses: step-security/aws-secretsmanager-get-secrets@102b6b2a2528747bcc321bb99ef326d8be7ef58f # v3.0.1 with: - python-version: '3.x' + secret-ids: PLUGINS_APP_KEY,${{ secrets.PLUGINS_APP_SECRET_NAME }} - - name: Install Dependencies - shell: bash + - name: Mask each line of the private key run: | - python -m pip install --upgrade pip - pip install requests - - - name: Trigger Update Event on ${{ matrix.repo.name }} - env: - GH_ORG: ${{ matrix.repo.org }} - GH_REPO: ${{ matrix.repo.name }} - CLI_VERSION: ${{ inputs.cli_version }} - IS_JS_RUNTIME_RELEASE: 'true' - GITHUB_TOKEN: ${{ secrets.DISPATCH_GH_TOKEN }} - run: python plugins-release-workflow/.github/scripts/dispatch_new_version_event.py - - # Group Java Wrapper-related updates - update_java_wrapper_consumers: - name: Update Java Wrapper Consumers - runs-on: cx-public-ubuntu-x64 - if: ${{ inputs.is_java_release == true }} - strategy: - matrix: - repo: [ - { org: 'Checkmarx', name: 'ast-jetbrains-plugin' }, - { org: 'Checkmarx', name: 'ast-eclipse-plugin' }, - { org: 'jenkinsci', name: 'checkmarx-ast-scanner-plugin' } - ] - steps: - - name: Check Out Repository - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0 - with: - persist-credentials: false - - - name: Check Out External Repository - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0 + while IFS= read -r line; do + line="${line%$'\r'}" + if [ -n "${line}" ]; then + echo "::add-mask::${line}" + fi + done <<< "${PLUGINS_APP_KEY}" + + - name: Create GitHub App token + id: app-token + uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0 with: - repository: Checkmarx/plugins-release-workflow - path: plugins-release-workflow - persist-credentials: false + client-id: ${{ secrets.PLUGINS_APP_CLIENT_ID }} + private-key: ${{ env.PLUGINS_APP_KEY }} + owner: Checkmarx + repositories: ${{ steps.targets.outputs.repos }} + permission-contents: write # send repository_dispatch events + permission-pull-requests: read # find the update PRs - - name: Set Up Python - uses: actions/setup-python@7f4fc3e22c37d6ff65e88745f38bd3157c663f7c # v4.9.1 - with: - python-version: '3.x' + - name: Remove the private key from the environment + run: echo "PLUGINS_APP_KEY=" >> "${GITHUB_ENV}" - - name: Install Dependencies - shell: bash + - name: Send ${{ steps.targets.outputs.event }} events + env: + GH_TOKEN: ${{ steps.app-token.outputs.token }} + EVENT: ${{ steps.targets.outputs.event }} + REPOS: ${{ steps.targets.outputs.repos }} run: | - python -m pip install --upgrade pip - pip install requests - - - name: Trigger Update Event on ${{ matrix.repo.name }} + IFS=',' read -ra targets <<< "${REPOS}" + for repo in "${targets[@]}"; do + gh api -X POST "repos/Checkmarx/${repo}/dispatches" \ + -f event_type="${EVENT}" \ + -f "client_payload[version]=${TAG}" \ + -f "client_payload[source_repo]=${GITHUB_REPOSITORY}" > /dev/null + echo "Sent ${EVENT} ${TAG} to Checkmarx/${repo}" + done + + - name: Wait for the update PRs env: - GH_ORG: ${{ matrix.repo.org }} - GH_REPO: ${{ matrix.repo.name }} - CLI_VERSION: ${{ inputs.cli_version }} - IS_JAVA_RELEASE: 'true' - GITHUB_TOKEN: ${{ secrets.DISPATCH_GH_TOKEN }} - run: python plugins-release-workflow/.github/scripts/dispatch_new_version_event.py + GH_TOKEN: ${{ steps.app-token.outputs.token }} + REPOS: ${{ steps.targets.outputs.repos }} + BOT: ${{ steps.app-token.outputs.app-slug }}[bot] + run: | + version="${TAG#v}" + IFS=',' read -ra targets <<< "${REPOS}" + declare -A pr=() + for attempt in $(seq 1 40); do + for repo in "${targets[@]}"; do + if [ -z "${pr[${repo}]:-}" ]; then + url=$(gh api "repos/Checkmarx/${repo}/pulls?state=open&per_page=100" \ + --jq "[.[] | select(.user.login == \"${BOT}\" and (.head.ref | contains(\"${version}\")))][0].html_url // empty") + if [ -n "${url}" ]; then + pr[${repo}]="${url}" + fi + fi + done + if [ "${#pr[@]}" -eq "${#targets[@]}" ]; then + break + fi + echo "Attempt ${attempt}: ${#pr[@]}/${#targets[@]} PRs found, waiting 30s" + sleep 30 + done + { + echo "### Update PRs for ${GITHUB_REPOSITORY} ${TAG}" + echo "" + echo "Each PR merges automatically after 2 approvals from cx-maintainers." + echo "" + echo "| Repository | Pull request |" + echo "|---|---|" + for repo in "${targets[@]}"; do + echo "| ${repo} | ${pr[${repo}]:-not found} |" + done + } >> "${GITHUB_STEP_SUMMARY}" + missing=$(( ${#targets[@]} - ${#pr[@]} )) + if [ "${missing}" -gt 0 ]; then + echo "::error::${missing} update PR(s) did not appear within 20 minutes. Check the update workflow runs in the repos marked 'not found'." + exit 1 + fi diff --git a/.github/workflows/test-auto-release-dispatcher.yml b/.github/workflows/test-auto-release-dispatcher.yml deleted file mode 100644 index 8830384..0000000 --- a/.github/workflows/test-auto-release-dispatcher.yml +++ /dev/null @@ -1,42 +0,0 @@ -name: Test Auto Release Dispatcher Workflow - -on: - workflow_dispatch: - inputs: - cli_version: - description: 'Version to release' - required: false - default: '' - is_cli_release: - description: 'Is this a CLI release?' - required: false - default: true - type: boolean - is_js_release: - description: 'Is this a JavaScript Wrapper release?' - required: false - default: false - type: boolean - is_js_runtime_release: - description: 'Is this a JavaScript Runtime Wrapper release?' - required: false - default: false - type: boolean - is_java_release: - description: 'Is this a Java Wrapper release?' - required: false - default: false - type: boolean - -permissions: - contents: read - -jobs: - test-new-release: - uses: ./.github/workflows/dispatch-workflow.yml - with: - cli_version: ${{ inputs.cli_version }} - is_cli_release: ${{ inputs.is_cli_release }} - is_js_release: ${{ inputs.is_js_release }} - is_js_runtime_release: ${{ inputs.is_js_runtime_release }} - is_java_release: ${{ inputs.is_java_release }} diff --git a/README.md b/README.md index 029e690..9390716 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,38 @@ # plugins-release-workflow Automates the release workflow across all components, starting with the CLI, followed by the Wrappers, and concluding with the Plugins. This streamlined process ensures consistent and efficient deployment across the entire ecosystem. + +## Dispatch plugin version updates +`.github/workflows/dispatch-workflow.yml` is called, after a release is published, by the manual +"Update dependent plugins" workflow of `ast-cli`, `ast-cli-java-wrapper`, `ast-cli-javascript-wrapper` +and `ast-cli-javascript-wrapper-runtime-cli`. It: +1. runs in the caller's `release` environment, from `main` only; +2. refuses a release that is a draft, a pre-release or not immutable; +3. sends a `repository_dispatch` event (`client_payload.version` = the tag) to every consumer repo; +4. waits for the update PRs and lists them in the job summary. + +| Released repo | Event | Consumers | +|---|---|---| +| ast-cli | `cli-version-update` | ast-cli-java-wrapper, ast-cli-javascript-wrapper, ast-cli-javascript-wrapper-runtime-cli, ast-github-action, ast-teamcity-plugin, ast-visual-studio-extension | +| ast-cli-javascript-wrapper | `js-wrapper-version-update` | ast-vscode-extension | +| ast-cli-javascript-wrapper-runtime-cli | `js-runtime-wrapper-version-update` | ast-azure-plugin | +| ast-cli-java-wrapper | `java-wrapper-version-update` | ast-eclipse-plugin, ast-jetbrains-plugin, ast-cli-maven-plugin | + +Caller example: +```yaml +on: + workflow_dispatch: + inputs: + tag: + description: Published release tag + required: true +permissions: + contents: read +jobs: + update-dependent-plugins: + permissions: + contents: read + id-token: write + uses: Checkmarx/plugins-release-workflow/.github/workflows/dispatch-workflow.yml@ + with: + tag: ${{ inputs.tag }} +```