From f281ad17c002b8ea8d9d96580548f40902212965 Mon Sep 17 00:00:00 2001 From: Tim Hansen Date: Fri, 27 May 2022 11:28:57 -0600 Subject: [PATCH] Build default Docker image on release Signed-off-by: Tim Hansen --- .changeset/blue-monkeys-explain.md | 6 ++ .github/workflows/deploy_docker-image.yml | 89 +++++++++++++++++++++ .github/workflows/deploy_packages.yml | 9 ++- .github/workflows/sync_release-manifest.yml | 13 ++- packages/create-app/src/createApp.ts | 8 ++ scripts/create-release-tag.js | 21 +---- 6 files changed, 117 insertions(+), 29 deletions(-) create mode 100644 .changeset/blue-monkeys-explain.md create mode 100644 .github/workflows/deploy_docker-image.yml diff --git a/.changeset/blue-monkeys-explain.md b/.changeset/blue-monkeys-explain.md new file mode 100644 index 0000000000..69382ff11d --- /dev/null +++ b/.changeset/blue-monkeys-explain.md @@ -0,0 +1,6 @@ +--- +'@backstage/create-app': patch +--- + +Adds the ability to define the Backstage app name using a `BACKSTAGE_APP_NAME` +environment variable when running `create-app`. diff --git a/.github/workflows/deploy_docker-image.yml b/.github/workflows/deploy_docker-image.yml new file mode 100644 index 0000000000..b0a2bc5286 --- /dev/null +++ b/.github/workflows/deploy_docker-image.yml @@ -0,0 +1,89 @@ +name: Build and push Docker Hub image +on: + repository_dispatch: + types: [release-published] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: checkout + uses: actions/checkout@v2 + with: + path: backstage + ref: v${{ github.event.client_payload.version }} + + - name: setup-node + uses: actions/setup-node@v1 + with: + node-version: 16.x + registry-url: https://registry.npmjs.org/ + + # Beginning of yarn setup, keep in sync between all workflows. + # TODO(Rugvip): move this to composite action once all features we use are supported + - name: use node.js ${{ matrix.node-version }} + uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node-version }} + registry-url: https://registry.npmjs.org/ # Needed for auth + + # Cache every node_modules folder inside the monorepo + - name: cache all node_modules + id: cache-modules + uses: actions/cache@v2 + with: + path: '**/node_modules' + # We use both yarn.lock and package.json as cache keys to ensure that + # changes to local monorepo packages bust the cache. + key: ${{ runner.os }}-v${{ matrix.node-version }}-node_modules-${{ hashFiles('yarn.lock', '**/package.json') }} + + # If we get a cache hit for node_modules, there's no need to bring in the global + # yarn cache or run yarn install, as all dependencies will be installed already. + + - name: find location of global yarn cache + id: yarn-cache + if: steps.cache-modules.outputs.cache-hit != 'true' + run: echo "::set-output name=dir::$(yarn cache dir)" + + - name: cache global yarn cache + uses: actions/cache@v2 + if: steps.cache-modules.outputs.cache-hit != 'true' + with: + path: ${{ steps.yarn-cache.outputs.dir }} + key: ${{ runner.os }}-yarn-${{ hashFiles('yarn.lock') }} + restore-keys: | + ${{ runner.os }}-yarn- + - name: yarn install + if: steps.cache-modules.outputs.cache-hit != 'true' + run: yarn install --frozen-lockfile + # End of yarn setup + + - name: create-app + run: yarn backstage-create-app + working-directory: ./packages/create-app + env: + BACKSTAGE_APP_NAME: example-app + + - name: yarn build + run: yarn build + working-directory: ./packages/create-app/example-app + + - name: Login to Docker Hub + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKER_HUB_USERNAME }} + password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }} + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + + - name: Build and push + uses: docker/build-push-action@v2 + with: + context: './packages/create-app/example-app' + file: ./packages/create-app/example-app/packages/backend/Dockerfile + push: true + tags: ${{ secrets.DOCKER_HUB_USERNAME }}/backstage:${{ github.event.client_payload.version }} + cache-from: type=registry,ref=${{ secrets.DOCKER_HUB_USERNAME }}/backstage:buildcache + cache-to: type=registry,ref=${{ secrets.DOCKER_HUB_USERNAME }}/backstage:buildcache,mode=max diff --git a/.github/workflows/deploy_packages.yml b/.github/workflows/deploy_packages.yml index b63507df0c..29a3c8d80d 100644 --- a/.github/workflows/deploy_packages.yml +++ b/.github/workflows/deploy_packages.yml @@ -209,7 +209,7 @@ jobs: # Grabs the version in the root package.json and creates a tag on GitHub - name: Create a release tag id: create_tag - run: node scripts/create-release-tag.js --dispatch-workflows + run: node scripts/create-release-tag.js env: GITHUB_TOKEN: ${{ secrets.GH_SERVICE_ACCOUNT_TOKEN }} @@ -219,6 +219,13 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GH_SERVICE_ACCOUNT_TOKEN }} + - name: Dispatch repository event + uses: peter-evans/repository-dispatch@v2 + with: + token: ${{ secrets.GH_SERVICE_ACCOUNT_TOKEN }} + event-type: release-published + client-payload: '{"version": "${{ steps.create_tag.outputs.version }}"}' + # Notify everyone about this great new release :D - name: Discord notification uses: Ilshidur/action-discord@0.3.2 diff --git a/.github/workflows/sync_release-manifest.yml b/.github/workflows/sync_release-manifest.yml index 816a3e632f..8cf6991aaf 100644 --- a/.github/workflows/sync_release-manifest.yml +++ b/.github/workflows/sync_release-manifest.yml @@ -1,10 +1,7 @@ name: Sync Release Manifest on: - workflow_dispatch: - inputs: - version: - description: Version number, without any 'v' prefix - required: true + repository_dispatch: + types: [release-published] jobs: create-new-version: @@ -23,7 +20,7 @@ jobs: with: path: backstage # 'v' prefix is added here for the tag, we keep it out of the manifest logic - ref: v${{ github.event.inputs.version }} + ref: v${{ github.event.client_payload.version }} # Checkout backstage/versions into /backstage/versions, which is where store the output - name: Checkout versions @@ -44,10 +41,10 @@ jobs: cd backstage mkdir -p scripts wget -O scripts/assemble-manifest.js https://raw.githubusercontent.com/backstage/backstage/master/scripts/assemble-manifest.js - node scripts/assemble-manifest.js ${{ github.event.inputs.version }} + node scripts/assemble-manifest.js ${{ github.event.client_payload.version }} cd versions git add . - git commit -am "${{ github.event.inputs.version }}" + git commit -am "${{ github.event.client_payload.version }}" git push - name: Dispatch update-helper update diff --git a/packages/create-app/src/createApp.ts b/packages/create-app/src/createApp.ts index 824d1d967e..7cf589e5f8 100644 --- a/packages/create-app/src/createApp.ts +++ b/packages/create-app/src/createApp.ts @@ -49,6 +49,14 @@ export default async (opts: OptionValues): Promise => { } return true; }, + when: (a: Answers) => { + const envName = process.env.BACKSTAGE_APP_NAME; + if (envName) { + a.name = envName; + return false; + } + return true; + }, }, ]); diff --git a/scripts/create-release-tag.js b/scripts/create-release-tag.js index 637467f285..a2706c578f 100755 --- a/scripts/create-release-tag.js +++ b/scripts/create-release-tag.js @@ -57,22 +57,7 @@ async function createGitTag(octokit, commitSha, tagName) { } } -async function dispatchReleaseWorkflows(octokit, releaseVersion) { - console.log('Dispatching release manifest sync'); - await octokit.actions.createWorkflowDispatch({ - owner: 'backstage', - repo: 'backstage', - workflow_id: 'sync_release-manifest.yml', - ref: 'master', - inputs: { - version: releaseVersion, - }, - }); -} - async function main() { - const shouldDispatch = process.argv.includes('--dispatch-workflows'); - if (!process.env.GITHUB_SHA) { throw new Error('GITHUB_SHA is not set'); } @@ -90,11 +75,7 @@ async function main() { await createGitTag(octokit, commitSha, tagName); console.log(`::set-output name=tag_name::${tagName}`); - - if (shouldDispatch) { - console.log(`Dispatching release workflows for ${tagName}`); - await dispatchReleaseWorkflows(octokit, releaseVersion); - } + console.log(`::set-output name=version::${releaseVersion}`); } main().catch(error => {