Skip to content

ci(release-nightly-version): run workflow on branch push for testing #1

ci(release-nightly-version): run workflow on branch push for testing

ci(release-nightly-version): run workflow on branch push for testing #1

name: Create a nightly tag
on:
schedule:
- cron: "0 0 * * 1-5" # Run workflow at 00:00 midnight UTC (05:30 AM IST) every Monday-Friday
workflow_dispatch:
push:
branches:
- ci-nightly-tag-workflow
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
env:
# Allow more retries for network requests in cargo (downloading crates) and
# rustup (installing toolchains). This should help to reduce flaky CI failures
# from transient network timeouts or other issues.
CARGO_NET_RETRY: 10
RUSTUP_MAX_RETRIES: 10
# The branch name that this workflow is allowed to run on.
# If the workflow is run on any other branch, this workflow will fail.
ALLOWED_BRANCH_NAME: main
jobs:
update-postman-collections:
name: Update Postman collection JSON files
runs-on: ubuntu-latest
steps:
- name: Generate GitHub app token
id: generate_app_token
uses: actions/create-github-app-token@v1
with:
app-id: ${{ secrets.HYPERSWITCH_BOT_APP_ID }}
private-key: ${{ secrets.HYPERSWITCH_BOT_APP_PRIVATE_KEY }}
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Check if the workflow is run on an allowed branch
shell: bash
run: |
if [[ "${{github.ref}}" != "refs/heads/${ALLOWED_BRANCH_NAME}" ]]; then
echo "::error::This workflow is expected to be run from the '${ALLOWED_BRANCH_NAME}' branch. Current branch: '${{github.ref}}'"
exit 1
fi
- name: Check if the latest commit is a tag
shell: bash
run: |
if [[ -n "$(git tag --points-at HEAD)" ]]; then
echo "::error::The latest commit on the branch is already a tag"
exit 1
fi
- name: Update Postman collection files from Postman directories
shell: bash
run: |
# maybe we need to move this package.json as we need it in multiple workflows
npm ci
POSTMAN_DIR="postman/collection-dir"
POSTMAN_JSON_DIR="postman/collection-json"
NEWMAN_PATH="$(pwd)/node_modules/.bin"
export PATH="${NEWMAN_PATH}:${PATH}"
# generate Postman collection JSON files for all Postman collection directories
for connector_dir in "${POSTMAN_DIR}"/*
do
connector="$(basename "${connector_dir}")"
newman dir-import "${POSTMAN_DIR}/${connector}" -o "${POSTMAN_JSON_DIR}/${connector}.postman_collection.json"
done
if git add postman && ! git diff --staged --quiet postman; then
echo "POSTMAN_COLLECTION_FILES_UPDATED=true" >> $GITHUB_ENV
echo "Postman collection files have been modified"
else
echo "Postman collection files have no modifications"
fi
- name: Commit updated Postman collections if modified
shell: bash
env:
GH_TOKEN: ${{ steps.generate_app_token.outputs.token }}
if: ${{ env.POSTMAN_COLLECTION_FILES_UPDATED == 'true' }}
run: |
# Obtain current HEAD commit SHA and use that as base tree SHA for creating a new tree
HEAD_COMMIT="$(git rev-parse 'HEAD^{commit}')"
UPDATED_TREE_SHA="${HEAD_COMMIT}"
# Obtain the flags to be passed to the GitHub CLI.
# Each line contains the flags to be used corresponding to the file.
lines="$(
git ls-files \
--format '--raw-field tree[][path]=%(path) --raw-field tree[][mode]=%(objectmode) --raw-field tree[][type]=%(objecttype) --field tree[][content]=@%(path)' \
postman/collection-json
)"
# Create a tree based on the HEAD commit of the current branch, using the contents of the updated Postman collections directory
while IFS= read -r line; do
# Split each line by space to obtain the flags passed to the GitHub CLI as an array
IFS=' ' read -ra flags <<< "${line}"
# Create a tree by updating each collection JSON file.
# The SHA of the created tree is used as the base tree SHA for updating the next collection file.
UPDATED_TREE_SHA="$(
gh api \
--method POST \
--header 'Accept: application/vnd.github+json' \
--header 'X-GitHub-Api-Version: 2022-11-28' \
'/repos/{owner}/{repo}/git/trees' \
--raw-field base_tree="${UPDATED_TREE_SHA}" \
"${flags[@]}" \
--jq '.sha'
)"
done <<< "${lines}"
# Create a commit to point to the tree with all updated collections
NEW_COMMIT_SHA="$(
gh api \
--method POST \
--header 'Accept: application/vnd.github+json' \
--header 'X-GitHub-Api-Version: 2022-11-28' \
'/repos/{owner}/{repo}/git/commits' \
--raw-field "message=chore(postman): update Postman collection files" \
--raw-field "parents[]=${HEAD_COMMIT}" \
--raw-field "tree=${UPDATED_TREE_SHA}" \
--jq '.sha'
)"
# Update the current branch to point to the above created commit.
# We disable forced update so that the workflow will fail if the branch has been updated since the workflow started
# (for example, new commits were pushed to the branch after the workflow execution started).
gh api \
--method PATCH \
--header 'Accept: application/vnd.github+json' \
--header 'X-GitHub-Api-Version: 2022-11-28' \
"/repos/{owner}/{repo}/git/refs/heads/${ALLOWED_BRANCH_NAME}" \
--raw-field "sha=${NEW_COMMIT_SHA}" \
--field 'force=false'
create-nightly-tag:
name: Create a nightly tag
uses: ./.github/workflows/release-nightly-version-reusable.yml
needs:
- update-postman-collections
secrets:
app_id: ${{ secrets.HYPERSWITCH_BOT_APP_ID }}
app_private_key: ${{ secrets.HYPERSWITCH_BOT_APP_PRIVATE_KEY }}