Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release/11.0 #2

Merged
merged 19 commits into from
May 1, 2024
Merged
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
31 changes: 31 additions & 0 deletions .github/workflows/feature.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Node.js CI

on:
push:
branches:
- feature/**
- AFG-**
- bug/**
- GAP-**
pull_request:
branches:
- feature/**
- AFG-**
- bug/**
- GAP-**

env:
node-version: 18.x

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ env.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ env.node-version }}
- run: npm install
39 changes: 39 additions & 0 deletions .github/workflows/promoteToProd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Promote to Prod Workflow

on:
push:
branches:
- main
paths-ignore:
- "*.md"

env:
AWS_REGION: eu-west-2
ECR_REPO_NAME: gap-lambda-update-open-search

jobs:
promoteToProd:
environment: AWS
runs-on: ubuntu-latest

permissions:
id-token: write
contents: read

steps:
- name: Setup AWS credentials
uses: aws-actions/configure-aws-credentials@v3
with:
role-to-assume: ${{ secrets.AWS_ROLE_ARN }}
role-session-name: ${{ env.ECR_REPO_NAME }}
aws-region: ${{ env.AWS_REGION }}

- name: Login to AWS ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Add prod tag to existing qa image
# Based on steps described here - https://docs.aws.amazon.com/AmazonECR/latest/userguide/image-retag.html
run: |
MANIFEST=$(aws ecr batch-get-image --repository-name ${{ env.ECR_REPO_NAME }} --image-ids imageTag=qa --output json | jq --raw-output --join-output '.images[0].imageManifest')
aws ecr put-image --repository-name ${{ env.ECR_REPO_NAME }} --image-tag prod --image-manifest "$MANIFEST"
94 changes: 94 additions & 0 deletions .github/workflows/pushImage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
name: Push Image Workflow

on:
create:
push:
branches:
- release/**
- develop
paths-ignore:
- "*.md"

env:
AWS_REGION: eu-west-2
node-version: 18.x
ECR_REPO_NAME: gap-lambda-update-open-search

jobs:
build:
runs-on: ubuntu-latest

# Need to check here as create event can't be filtered by branch name...
if: github.ref == 'refs/heads/develop' || startsWith(github.ref, 'refs/heads/release')

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ env.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ env.node-version }}
- run: npm install

imageBuild:
needs: build

permissions:
id-token: write
contents: read

environment: AWS
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
with:
# Fetch all commits since we use the total commit count to determine the build version
fetch-depth: 0

- name: Use Node.js ${{ env.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ env.node-version }}

- name: Setup AWS credentials
uses: aws-actions/configure-aws-credentials@v3
with:
role-to-assume: ${{ secrets.AWS_ROLE_ARN }}
role-session-name: ${{ env.ECR_REPO_NAME }}
aws-region: ${{ env.AWS_REGION }}

- name: Login to AWS ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Determine & set BUILD_VERSION
run: |
GIT_COUNT=$(git rev-list $GITHUB_SHA --count)
echo "BUILD_VERSION=b_$GIT_COUNT" >> $GITHUB_ENV
echo BUILD_VERSION is ${{ env.BUILD_VERSION }}

- run: npm install

- name: Build, tag and push Docker image to AWS ECR
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
run: |
docker build --tag $ECR_REGISTRY/${{ env.ECR_REPO_NAME }}:${{ env.BUILD_VERSION }} .
docker push $ECR_REGISTRY/${{ env.ECR_REPO_NAME }}:${{ env.BUILD_VERSION }}

- name: Create env tag
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
run: |
ENV_TAG=${{ (github.ref == 'refs/heads/develop' && 'develop') || (startsWith(github.ref, 'refs/heads/release') && 'qa') }}
docker tag $ECR_REGISTRY/${{ env.ECR_REPO_NAME }}:${{ env.BUILD_VERSION }} $ECR_REGISTRY/${{ env.ECR_REPO_NAME }}:$ENV_TAG
docker push $ECR_REGISTRY/${{ env.ECR_REPO_NAME }}:$ENV_TAG

- name: Create release tag - if we are committing to a release branch
if: ${{ startsWith(github.ref, 'refs/heads/release/') }}
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
run: |
RELEASE_VERSION=V_${GITHUB_REF##*/}
docker tag $ECR_REGISTRY/${{ env.ECR_REPO_NAME }}:${{ env.BUILD_VERSION }} $ECR_REGISTRY/${{ env.ECR_REPO_NAME }}:$RELEASE_VERSION
docker push $ECR_REGISTRY/${{ env.ECR_REPO_NAME }}:$RELEASE_VERSION
File renamed without changes.
49 changes: 27 additions & 22 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fetch from 'node-fetch';
import contentful from 'contentful-management';
import fetch from 'node-fetch';

const openSearchConfig = {
username: process.env.OPEN_SEARCH_USERNAME,
Expand Down Expand Up @@ -28,26 +28,20 @@ const client = contentful.createClient(
);

const getGrantById = async (contentfulEntryId) => {
const { items } = await client.entry.getPublished({
query: {
'sys.id': contentfulEntryId,
content_type: 'grantDetails',
},
});
const grant = await client.entry.get({ entryId: contentfulEntryId });

if (!items || !items.length) {
throw new Error(`No published grant found in contentful with id ${id}`);
if (!grant) {
throw new Error(
`No published grant found in contentful with id ${contentfulEntryId}`
);
}

// Should always return one result as the id is unique
console.log(`Found ${items.length} published grant(s) in contentful`);
return items[0];
return grant;
};

const updateElasticIndex = async (contentfulEntry, action) => {
const auth = openSearchConfig.username + ':' + openSearchConfig.password;
const authHeader = 'Basic ' + btoa(auth);
const url = `${openSearchConfig.url}${openSearchConfig.domain}/_doc/${contentfulEntry.sys.id}`;
const url = `${openSearchConfig.url}/${openSearchConfig.domain}/_doc/${contentfulEntry.sys.id}`;

const ACTIONS = {
ADD: 'PUT',
Expand All @@ -61,7 +55,7 @@ const updateElasticIndex = async (contentfulEntry, action) => {
const body = JSON.stringify(contentfulEntry);

console.log(
`Updating elastic index for grant ${contentfulEntry.fields.grantName['en-US']}, with contentful entry: \n ${body}`
`Updating elastic index for grant: '${contentfulEntry.fields.grantName['en-US']}', with contentful entry: \n ${body}`
);
const response = await fetch(url, {
method: method,
Expand All @@ -75,19 +69,30 @@ const updateElasticIndex = async (contentfulEntry, action) => {
if (!response || !response.ok) {
const errorMessage = await response.text();
throw new Error(
`Failed to create an elastic index entry for ad ${contentfulEntry.sys.id} in open search: ${errorMessage}`
`Failed to create/delete an elastic index entry for ad: '${contentfulEntry.sys.id}' in open search: ${errorMessage}`
);
} else {
console.log(
`Successfully updated elastic index for grant ${contentfulEntry.fields.grantName['en-US']}`
`${action} successful for elastic index for grant: '${contentfulEntry.fields.grantName['en-US']}'`
);
}
};

export const handler = async (message) => {
const contentfulEntryId = message.contentfulEntryId;
const grant = await getGrantById(contentfulEntryId);
await updateElasticIndex(grant, message.type);
export const handler = async (data) => {
console.log('SQS Message: ', data);
for (const record of data.Records) {
const message = JSON.parse(record?.body);
if (!message.contentfulEntryId || !message.type) {
console.log(`Invalid Message: ${message}`);
throw new Error(`Invalid Message: ${record.body}`);
}

console.log(`contentful entry ID: ${message.contentfulEntryId}`);
console.log(`type: ${message.type}`);

return { statusCode: 200 };
const grant = await getGrantById(message.contentfulEntryId);
await updateElasticIndex(grant, message.type);

return { statusCode: 200 };
}
};
Loading