From f5aad49ba7d2fc159b71f91e8ebf37c012643c09 Mon Sep 17 00:00:00 2001 From: Elliot DeNolf Date: Tue, 3 Dec 2024 23:06:32 -0500 Subject: [PATCH] ci: template bump workflow (#9733) This create a workflow that will trigger upon every release and do the following: - Re-generate all template lockfiles as needed (only blank and website need them for payload cloud) - Re-generate all postgres migrations for any pg-based template - Commit changes - Create PR --- .github/workflows/post-release-templates.yml | 37 ++++++++------------ scripts/generate-template-variations.ts | 27 ++++++++++---- 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/.github/workflows/post-release-templates.yml b/.github/workflows/post-release-templates.yml index ae80a5152..ed9f2bd75 100644 --- a/.github/workflows/post-release-templates.yml +++ b/.github/workflows/post-release-templates.yml @@ -1,15 +1,14 @@ name: post-release-templates on: - # release: - # types: - # - published + release: + types: + - published workflow_dispatch: inputs: tag: description: 'Release tag to process (optional)' required: true - # default: '' env: NODE_VERSION: 22.6.0 @@ -23,6 +22,10 @@ jobs: permissions: contents: write pull-requests: write + env: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + POSTGRES_DB: payloadtests steps: - name: Checkout uses: actions/checkout@v4 @@ -48,6 +51,7 @@ jobs: psql "postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:5432/$POSTGRES_DB" -c "CREATE ROLE runner SUPERUSER LOGIN;" psql "postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:5432/$POSTGRES_DB" -c "SELECT version();" echo "POSTGRES_URL=postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:5432/$POSTGRES_DB" >> $GITHUB_ENV + echo "DATABASE_URI=postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:5432/$POSTGRES_DB" >> $GITHUB_ENV - name: Start MongoDB uses: supercharge/mongodb-github-action@1.11.0 @@ -76,33 +80,20 @@ jobs: set -ex git config --global user.name "github-actions[bot]" git config --global user.email "github-actions[bot]@users.noreply.github.com" - export BRANCH_NAME=chore/templates-${{ steps.determine_tag.outputs.release_tag }} - git checkout -b $BRANCH_NAME - git add -A - # If no files have changed, exit early with success - git diff --cached --quiet --exit-code && exit 0 + git diff --name-only - git commit -m "chore(templates): bump lockfiles after ${{ steps.determine_tag.outputs.release_tag }}" - git push origin $BRANCH_NAME - echo "committed=true" >> "$GITHUB_OUTPUT" + export BRANCH_NAME=templates/bump-${{ steps.determine_tag.outputs.release_tag }} echo "branch=$BRANCH_NAME" >> "$GITHUB_OUTPUT" - - name: Debug Branches - run: | - echo "Target Commitish: ${{ github.event.release.target_commitish }}" - echo "Branch: ${{ steps.commit.outputs.branch }}" - echo "Ref: ${{ github.ref }}" - - name: Create pull request uses: peter-evans/create-pull-request@v7 - if: steps.commit.outputs.committed == 'true' with: token: ${{ secrets.GITHUB_TOKEN }} labels: 'area: templates' author: github-actions[bot] - commit-message: 'Automated update after release' + commit-message: 'templates: bump templates for ${{ steps.determine_tag.outputs.release_tag }}' branch: ${{ steps.commit.outputs.branch }} - base: ${{ github.event_name != 'workflow_dispatch' && github.event.release.target_commitish || github.ref }} - title: 'chore(templates): bump lockfiles after ${{ steps.determine_tag.outputs.release_tag }}' - body: 'Automated bump of template lockfiles after release ${{ steps.determine_tag.outputs.release_tag }}' + base: main + title: 'templates: bump for ${{ steps.determine_tag.outputs.release_tag }}' + body: 'Automated bump of templates for ${{ steps.determine_tag.outputs.release_tag }}' diff --git a/scripts/generate-template-variations.ts b/scripts/generate-template-variations.ts index ae664c31e..32e69cee8 100644 --- a/scripts/generate-template-variations.ts +++ b/scripts/generate-template-variations.ts @@ -129,6 +129,7 @@ async function main() { name: 'blank', dirname: 'blank', db: 'mongodb', + generateLockfile: true, storage: 'localDisk', sharp: true, // The blank template is used as a base for create-payload-app functionality, @@ -187,6 +188,7 @@ async function main() { await writeEnvExample({ destDir, envNames, + dbType: db, }) } @@ -234,7 +236,7 @@ async function main() { ...process.env, PAYLOAD_SECRET: 'asecretsolongnotevensantacouldguessit', BLOB_READ_WRITE_TOKEN: 'vercel_blob_rw_TEST_asdf', - DATABASE_URI: 'postgres://localhost:5432/payloadtests', + DATABASE_URI: process.env.POSTGRES_URL || 'postgres://localhost:5432/payloadtests', }, }) } @@ -287,22 +289,35 @@ ${description} async function writeEnvExample({ destDir, envNames, + dbType, }: { destDir: string envNames?: TemplateVariations['envNames'] + dbType: DbType }) { const envExamplePath = path.join(destDir, '.env.example') const envFileContents = await fs.readFile(envExamplePath, 'utf8') + const fileContents = envFileContents .split('\n') .filter((e) => e) - .map((l) => - envNames?.dbUri && l.startsWith('DATABASE_URI') - ? l.replace('DATABASE_URI', envNames.dbUri) - : l, - ) + .map((l) => { + if (l.startsWith('DATABASE_URI')) { + // Use db-appropriate connection string + if (dbType.includes('postgres')) { + l = 'DATABASE_URI=postgresql://127.0.0.1:5432/payloadtests' + } + + // Replace DATABASE_URI with the correct env name if set + if (envNames?.dbUri) { + l = l.replace('DATABASE_URI', envNames.dbUri) + } + } + return l + }) .join('\n') + console.log(`Writing to ${envExamplePath}`) await fs.writeFile(envExamplePath, fileContents) }