diff --git a/.github/workflows/example-build-docker.yml b/.github/workflows/example-build-docker.yml new file mode 100644 index 0000000..69390d3 --- /dev/null +++ b/.github/workflows/example-build-docker.yml @@ -0,0 +1,49 @@ +name: Build and Push Docker Image to ECR + +on: + push: + branches: + - 'example/pipeline-cherry' # or specify any branch you want to trigger this workflow on + +permissions: + id-token: write + contents: read + +jobs: + build-and-push: + runs-on: ubuntu-latest + + steps: + - name: Checkout the code + uses: actions/checkout@v3 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v3 + with: + role-to-assume: arn:aws:iam::697698820969:role/GithubActionAssumeRole + aws-region: us-east-1 + + - name: Log in to Amazon ECR + id: login-ecr + uses: aws-actions/amazon-ecr-login@v2 + + - name: Build, tag, and push Docker image + env: + ECR_URI: "697698820969.dkr.ecr.us-east-1.amazonaws.com/web-app" + IMAGE_TAG: latest-cherry-ecr + run: | + yarn --frozen-lockfile + yarn nx build api-flowaccount-workshop + docker build -t $ECR_URI:$IMAGE_TAG . + docker push $ECR_URI:$IMAGE_TAG + + # - name: Image digest + # run: | + # IMAGE_DIGEST=$(docker inspect --format='{{index .RepoDigests 0}}' ${{ env.REPO_URI }}:${{ github.sha }}) + # echo "Docker image pushed: $IMAGE_DIGEST" diff --git a/apps/api/flowaccount-workshop/project.json b/apps/api/flowaccount-workshop/project.json index c24da65..b0ca6eb 100644 --- a/apps/api/flowaccount-workshop/project.json +++ b/apps/api/flowaccount-workshop/project.json @@ -11,6 +11,7 @@ "outputPath": "dist/apps/api/flowaccount-workshop", "main": "apps/api/flowaccount-workshop/src/main.ts", "tsConfig": "apps/api/flowaccount-workshop/tsconfig.app.json", + "webpackConfig": "apps/api/flowaccount-workshop/webpack.config.ts", "assets": [ "apps/api/flowaccount-workshop/src/assets", "apps/api/flowaccount-workshop/src/README.md" @@ -19,7 +20,7 @@ "configurations": {} }, "serve": { - "executor": "@nx/node:node", + "executor": "@nx/js:node", "options": { "buildTarget": "api-flowaccount-workshop:build" } diff --git a/apps/api/flowaccount-workshop/src/main.ts b/apps/api/flowaccount-workshop/src/main.ts index 8011986..9a654a7 100644 --- a/apps/api/flowaccount-workshop/src/main.ts +++ b/apps/api/flowaccount-workshop/src/main.ts @@ -37,4 +37,4 @@ app.use(function (err, req, res, next) { res.render('error'); }); -app.listen(process.env.SERVER_PORT || 3000, () => { console.log('listening!')}); \ No newline at end of file +app.listen(process.env.SERVER_PORT || 8081, () => { console.log('listening!')}); \ No newline at end of file diff --git a/apps/api/flowaccount-workshop/webpack.config.ts b/apps/api/flowaccount-workshop/webpack.config.ts new file mode 100644 index 0000000..483c6a0 --- /dev/null +++ b/apps/api/flowaccount-workshop/webpack.config.ts @@ -0,0 +1,42 @@ +const { NxWebpackPlugin } = require('@nx/webpack'); +const nodeExternals = require('webpack-node-externals'); +const { withExternals } = require('./with-externals'); +const { composePlugins } = require('@nx/webpack'); + +module.exports = composePlugins( + (config, { options, context }) => { + return { + target: 'node', + node: { + __dirname: true + }, + module: { + rules: [ + { + test: /\.(json)$/, + type: 'src/config', + } + ], + }, + output: { + globalObject: 'this', + }, + plugins: [ + new NxWebpackPlugin({ + tsConfig: options.tsConfig, + compiler: 'swc', + main: options.main, + outputHashing: false, + ssr: true, + sourceMap: true, + generatePackageJson: options.generatePackageJson, + assets: options.assets, + outputPath: options.outputPath + }), + ], + + externals: [withExternals([/^aws-cdk-lib\//,/aws-cdk-stack\//,/^aws-cdk-core\//,/^nx-aws-cdk\//]),] + } + }); + + diff --git a/apps/api/flowaccount-workshop/with-externals.ts b/apps/api/flowaccount-workshop/with-externals.ts new file mode 100644 index 0000000..0e7630f --- /dev/null +++ b/apps/api/flowaccount-workshop/with-externals.ts @@ -0,0 +1,29 @@ +import { NxComposableWebpackPlugin } from '@nx/webpack'; +import type { Configuration } from 'webpack'; + +// @example withExternals([/^@aws-sdk\//, /^@aws-lambda-powertools\//]) +export function withExternals(externals: RegExp[]): NxComposableWebpackPlugin { + return function configure(config: Configuration): Configuration { + config.externals = Array.isArray(config.externals) + ? config.externals + : config.externals + ? [config.externals] + : []; + config.externals.push(function ( + ctx, + callback: ( + err?: null | Error, + result?: string | boolean | string[] | { [index: string]: any }, + ) => void, + ) { + if (externals.some((e) => e.test(ctx.request))) { + // not bundled + return callback(null, `commonjs ${ctx.request}`); + } + // bundled + callback(); + }); + + return config; + }; +} \ No newline at end of file diff --git a/dockerfile b/dockerfile new file mode 100644 index 0000000..ec8e668 --- /dev/null +++ b/dockerfile @@ -0,0 +1,14 @@ +FROM node:fermium + +ARG GITCOMMIT="" +ENV GIT_COMMIT_HASH=${GITCOMMIT} + +WORKDIR /app + +COPY dist/apps/api/flowaccount-workshop/. . + +#RUN npm install + +CMD ["node", "main.js"] + +EXPOSE 8081