Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions .github/workflows/ai-deploy-request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
###########################
###########################
## Deployment AI generation testing ##
###########################
###########################

name: Hackathon Deployment Request

######################################################
# Start the job on a "deployment" to hackathon-test or other branch #
######################################################

on:
workflow_dispatch:
inputs:
target_branch:
description: "Target branch to send PR to (e.g., main, stage, etc)"
type: string
required: false
default: "hackathon-test"

###############
# Set the Job #
###############

jobs:
ai-metadata-update:
name: AI Metadata Update on Deployment
uses: AdobeDocs/adp-devsite-workflow/.github/workflows/ai-deploy-metadata.yml@app-builder-test
with:
FILE_NAME: "all_pages_content.txt"
secrets:
AZURE_OPENAI_ENDPOINT: ${{ secrets.AZURE_OPENAI_ENDPOINT }}
AZURE_OPENAI_API_KEY: ${{ secrets.AZURE_OPENAI_API_KEY }}

lint:
name: Lint
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Lint
run: npx --yes github:AdobeDocs/adp-devsite-utils#add-lint-check runLint -v
40 changes: 40 additions & 0 deletions .github/workflows/ai-pr-request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
###########################
###########################
## Pull request testing ##
###########################
###########################
name: Hackathon Pull Request

# Documentation:
# - Workflow: https://help.github.com/en/articles/workflow-syntax-for-github-actions
# - SuperLinter: https://github.com/github/super-linter
# - Markdown linter: https://github.com/DavidAnson/markdownlint
# - Link validation: https://github.com/remarkjs/remark-validate-links

######################################################
# Start the job on a pull request to the hackathon-test branch #
######################################################
on:
pull_request:
branches: [hackathon-test]
paths:
- 'src/pages/**'

###############
# Set the Job #
###############
jobs:
call_reusable_workflow:
name: Generate AI Metadata
# Skip if PR title starts with "[AI PR] Metadata Update" or branch name starts with "ai-metadata"
if: >-
!startsWith(github.event.pull_request.title, '[AI PR] Metadata Update') ||
!startsWith(github.event.pull_request.head.ref, 'ai-metadata')
uses: AdobeDocs/adp-devsite-workflow/.github/workflows/ai-pr-request-metadata.yml@app-builder-test
with:
PR_ID: ${{ github.event.pull_request.number }}
FILE_NAME: "pr_content.txt"
secrets:
AZURE_OPENAI_ENDPOINT: ${{ secrets.AZURE_OPENAI_ENDPOINT }}
AZURE_OPENAI_API_KEY: ${{ secrets.AZURE_OPENAI_API_KEY }}
8 changes: 8 additions & 0 deletions src/pages/hackathon-new-files/adobe-express.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
127 changes: 127 additions & 0 deletions src/pages/hackathon-new-files/barcode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# Lesson 2: Writing a Serverless Action
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Lesson 2: Writing a Serverless Action
---
title: Lesson 2: Writing a Serverless Action
description: Learn how to write a serverless action to generate Code128 barcodes using the bwip-js library in a serverless environment, and deploy it with Adobe I/O Runtime.
keywords:
- serverless action
- bwip-js barcode
- Adobe I/O Runtime
- Code128 barcode
- App Builder deployment
faqs:
- question: What is bwip-js and why is it used in this lesson?
answer: bwip-js is an npm package for generating barcodes in JavaScript. It is used here because it works well in serverless environments to create Code128 barcodes.
- question: How do you pass data to the barcode generator in the action?
answer: The barcode data is passed through the `value` parameter, which the action uses to generate the barcode image.
- question: How can I run and test the serverless action locally?
answer: You can run the action locally using the CLI command `aio app run --local`, which starts a Docker-based OpenWhisk stack and lets you test the action via a local URL.
- question: What steps are necessary before deploying the action to Adobe I/O Runtime?
answer: Set Adobe I/O Runtime secrets in the `.env` file, disable built-in authentication in `manifest.yml`, and then deploy using `aio app deploy` or `aio app run`.
- question: How do I verify that the generated barcode is correct?
answer: You can verify the barcode by using a barcode reader or scanner app to test the image generated from the action's output.
---
# Lesson 2: Writing a Serverless Action


There are many existing npm packages to display a barcode. Some don't play well in serverless environments.
For this Code Lab, we'll use [bwip-js](https://www.npmjs.com/package/bwip-js/) to render a code128 barcode.

## Barcode action

First, install the dependency with:

```bash
npm i bwip-js --save
```

Then import the dependency into your action:

```javascript
const bwipjs = require('bwip-js');
```

Now we can use the library and generate a barcode buffer in the exported main function:

```javascript
const buffer = await bwipjs.toBuffer({
bcid: 'code128',
text: params.value,
scale: 3,
height: 10,
includetext: false,
});
```

Notice that we defined a `value` parameter to be passed to the barcode generator configuration. This is the actual data that the barcode will be holding.

Then we can return the image representation of the buffer with:

```javascript
return {
headers: { 'Content-Type': 'image/png' },
statusCode: 200,
body: buffer.toString('base64')
};
```

Finally we can add checks to verify the requested `value` parameter, add logging and appropriate error handling to obtain this action:

```javascript
const { Core } = require('@adobe/aio-sdk');
const { errorResponse, stringParameters, checkMissingRequestInputs } = require('../utils');
const bwipjs = require('bwip-js');

// main function that will be executed by Adobe I/O Runtime
async function main (params) {
// create a Logger
const logger = Core.Logger('main', { level: params.LOG_LEVEL || 'info' });

try {
// 'info' is the default level if not set
logger.info('Calling the main action');

// log parameters, only if params.LOG_LEVEL === 'debug'
logger.debug(stringParameters(params));

// check for missing request input parameters and headers
const requiredParams = ['value'];
const errorMessage = checkMissingRequestInputs(params, requiredParams);
if (errorMessage) {
// return and log client errors
return errorResponse(400, errorMessage, logger);
}

const buffer = await bwipjs.toBuffer({
bcid: 'code128',
text: params.value,
scale: 3,
height: 10,
includetext: false,
backgroundcolor: 'ffffff'
});

return {
headers: { 'Content-Type': 'image/png' },
statusCode: 200,
body: buffer.toString('base64')
};
} catch (error) {
// log any server errors
logger.error(error);
// return with 500
return errorResponse(500, error.message, logger);
}
}

exports.main = main;
```

You can run the action locally using the CLI using:

```bash
aio app run --local
```

This will:

1. Start a local [OpenWhisk](https://openwhisk.apache.org/) stack on Docker
2. Package and deploy the Runtime action and its dependencies using a built-in webpack configuration
3. Start a local development environment and provide the action url e.g. `http://localhost:3233/api/v1/web/guest/my-barcode-app-0.0.1/barcode` for testing and debugging

Note that we'll cover how to do debug an App Builder app in a different Code Lab.

Nowadd the value parameter, e.g. `?value=test`, to the url so the action will generate a barcode:

![barcode](assets/barcode-test.png)

## Deploying

You can deploy an App Builder Headless app with `aio app run` or `aio app deploy`. This will deploy the actions to Adobe I/O Runtime.
`aio app deploy` would have deployed the UI to a CDN, but since we don't have a UI, that step is ignored. We'll have a separate Code Lab to guide you through building an App Builder App with UI.

Be sure to set your Adobe I/O Runtime secrets (namespace and auth) in the `.env` file. Also, turn off the built-in authentication by setting `require-adobe-auth: false` in the `manifest.yml`. The security topic will be covered in a dedicated Code Lab.

Entering deploy in the CLI will output the deployed action URL:

![deploy](assets/deploy.png)

**Congratulations! Your first App Builder Headless App is live.**

How can we test that the passed value is actually rendered as a barcode? Fortunately, there are barcode readers that we can use in our tests.
Loading