Skip to content

Commit

Permalink
Add image tagging to latest
Browse files Browse the repository at this point in the history
  • Loading branch information
ionwyn committed Feb 13, 2025
1 parent 04c8cfb commit 51c53b3
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 50 deletions.
18 changes: 11 additions & 7 deletions .github/workflows/hmr-build-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: "12"
node-version: "18"
- name: Set Version
run: echo "VERSION=v1.0.${{ github.run_number }}" >> $GITHUB_ENV
- name: Build
run: |
oc version
oc login --token=${{ secrets.OPENSHIFT_TOKEN}} --server=${{ secrets.OPENSHIFT_SERVER_URL }}
npm ci
DEBUG=* npm run build -- --pr=${{ github.event.pull_request.number }} --git.branch.name=${{github.head_ref}} --git.ref=${{github.head_ref}}
DEBUG=* npm run build -- --pr=${{ github.event.pull_request.number }} --git.branch.name=${{github.head_ref}} --git.ref=${{github.head_ref}} --version=${VERSION}
deploy-to-dev:
runs-on: ubuntu-22.04
Expand All @@ -47,13 +49,15 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: "12"
node-version: "18"
- name: Set Version
run: echo "VERSION=v1.0.${{ github.run_number }}" >> $GITHUB_ENV
- name: Deploy the image
run: |
oc version
oc login --token=${{ secrets.OPENSHIFT_TOKEN}} --server=${{ secrets.OPENSHIFT_SERVER_URL }}
npm ci
DEBUG=* npm run deploy -- --pr=${{ github.event.pull_request.number }} --env=dev --git.branch.name=${{github.head_ref}} --git.ref=${{github.head_ref}}
DEBUG=* npm run deploy -- --pr=${{ github.event.pull_request.number }} --env=dev --git.branch.name=${{github.head_ref}} --git.ref=${{github.head_ref}} --version=${VERSION}
deploy-to-test:
needs: [build, deploy-to-dev]
Expand All @@ -67,7 +71,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: "12"
node-version: "18"
- name: Deploy the image
run: |
oc version
Expand All @@ -87,7 +91,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: "12"
node-version: "18"
- name: Deploy the image
run: |
oc version
Expand All @@ -107,7 +111,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: "12"
node-version: "18"
- name: Deploy the image
run: |
oc version
Expand Down
8 changes: 5 additions & 3 deletions .pipeline/lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ module.exports = (settings) => {
const templatesLocalBaseUrl = oc.toFileUrl(
path.resolve(__dirname, "../../openshift")
);
const githubRunNumber = process.env.GITHUB_RUN_NUMBER || "manual";
const version = options.version || `v1.0.${githubRunNumber}`;

objects.push(
...oc.processDeploymentTemplate(
Expand All @@ -21,7 +23,7 @@ module.exports = (settings) => {
param: {
NAME: `${settings.phases[phase].name}-client`,
SUFFIX: settings.phases[phase].suffix,
VERSION: settings.phases[phase].tag,
VERSION: version,
SOURCE_REPOSITORY_URL: `${oc.git.uri}`,
SOURCE_REPOSITORY_REF: `${oc.git.branch_ref}`,
},
Expand All @@ -36,7 +38,7 @@ module.exports = (settings) => {
param: {
NAME: `${settings.phases[phase].name}-api`,
SUFFIX: settings.phases[phase].suffix,
VERSION: settings.phases[phase].tag,
VERSION: version,
SOURCE_REPOSITORY_URL: `${oc.git.uri}`,
SOURCE_REPOSITORY_REF: `${oc.git.branch_ref}`,
},
Expand All @@ -51,7 +53,7 @@ module.exports = (settings) => {
param: {
NAME: `${settings.phases[phase].name}-hangfire`,
SUFFIX: settings.phases[phase].suffix,
VERSION: settings.phases[phase].tag,
VERSION: version,
SOURCE_REPOSITORY_URL: `${oc.git.uri}`,
SOURCE_REPOSITORY_REF: `${oc.git.branch_ref}`,
},
Expand Down
2 changes: 1 addition & 1 deletion .pipeline/lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const phases = {
client_memory: "100Mi",
},
uat: {
namespace: "d3d940-uat",
namespace: "d3d940-test",
name: `${name}`,
phase: "uat",
changeId: changeId,
Expand Down
79 changes: 73 additions & 6 deletions .pipeline/lib/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ module.exports = (settings) => {
const options = settings.options;
const phase = options.env;
const changeId = phases[phase].changeId;
const version = options.version || `${phases[phase].phase}-1.0.0`;

const oc = new OpenShiftClientX(
Object.assign({ namespace: phases[phase].namespace }, options)
);

const templatesLocalBaseUrl = oc.toFileUrl(
path.resolve(__dirname, "../../openshift")
);
Expand All @@ -31,7 +33,7 @@ module.exports = (settings) => {
param: {
NAME: `${phases[phase].name}-client`,
SUFFIX: phases[phase].suffix,
VERSION: phases[phase].tag,
VERSION: version,
ENV: phases[phase].phase,
HOST: phases[phase].host,
CPU: phases[phase].client_cpu,
Expand Down Expand Up @@ -64,7 +66,7 @@ module.exports = (settings) => {
param: {
NAME: `${phases[phase].name}-logdb`,
SUFFIX: phases[phase].suffix,
VERSION: phases[phase].tag,
VERSION: version,
ENV: phases[phase].phase,
},
}
Expand Down Expand Up @@ -95,7 +97,7 @@ module.exports = (settings) => {
NAME: `${phases[phase].name}-api`,
LOGDB_NAME: `${phases[phase].name}-logdb`,
SUFFIX: phases[phase].suffix,
VERSION: phases[phase].tag,
VERSION: version,
HOST: phases[phase].host,
ENV: phases[phase].phase,
ASPNETCORE_ENVIRONMENT: phases[phase].dotnet_env,
Expand All @@ -114,7 +116,7 @@ module.exports = (settings) => {
NAME: `${phases[phase].name}-hangfire`,
LOGDB_NAME: `${phases[phase].name}-logdb`,
SUFFIX: phases[phase].suffix,
VERSION: phases[phase].tag,
VERSION: version,
ENV: phases[phase].phase,
ASPNETCORE_ENVIRONMENT: phases[phase].dotnet_env,
CPU: phases[phase].hangfire_cpu,
Expand All @@ -137,5 +139,70 @@ module.exports = (settings) => {
phases.build.namespace,
phases.build.tag
);
oc.applyAndDeploy(objects, phases[phase].instance);
};

let imageExists = false;

oc.applyAndDeploy(objects, phases[phase].instance)
.then(() => {
const imageNames = ["hmcr-api", "hmcr-client", "hmcr-hangfire"];

imageExists = imageNames.every((imageName) => {
try {
const imageSha = oc.raw("get", [
"istag",
`${imageName}:${version}`,
"-n",
"d3d940-tools",
"-o",
"json",
]);
if (!imageSha) {
console.error(
`❌ Error: No built image found for ${imageName}:${version}`
);
return false;
}

console.log(`🔄 Tagging built image as 'latest' for ${imageName}`);

oc.raw("tag", [
`d3d940-tools/${imageName}:${version}`,
`d3d940-tools/${imageName}:latest`,
]);

console.log(
`✅ Successfully tagged ${imageName}:${version} as latest.`
);
return true;
} catch (error) {
console.error(error.message);
return false;
}
});

console.log("✅ All images are now tagged as latest.");
})
.finally(() => {
if (!imageExists) {
console.log("❌ Skipping final tagging because image does not exist.");
return;
}
const imageNames = ["hmcr-api", "hmcr-client", "hmcr-hangfire"];

imageNames.forEach((imageName) => {
const sourceImage = `d3d940-tools/${imageName}:latest`;
const targetImage = `d3d940-${phase}/${imageName}`;

console.log(
`🔄 Tagging Image for Deployment: ${sourceImage} -> ${targetImage}`
);

// Tag the image for the specific deployment environment
oc.raw("tag", [`${sourceImage}`, `${targetImage}:${version}`]);

console.log(
`✅ Image successfully tagged for ${phase}: ${targetImage}`
);
});
});
};
2 changes: 1 addition & 1 deletion client/src/js/components/fragments/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const Header = ({ currentUser }) => {
alt="B.C. Government Logo"
/>
</NavbarBrand>
<div className="navbar-brand">MoTI Contractor Reporting</div>
<div className="navbar-brand">MoTI Maintenance Contractor Reporting</div>
<NavbarToggler onClick={toggleNavbar} />
{/* <Collapse> is needed to keep justify-content: space-between maintain correct spacing */}
<Collapse isOpen={!collapsed} navbar />
Expand Down
15 changes: 3 additions & 12 deletions openshift/api-deploy-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ objects:
strategy:
activeDeadlineSeconds: 21600
type: Recreate
recreateParams:
pre: {}
recreateParams:
pre: {}
mid: {}
post: {}
template:
Expand All @@ -52,6 +52,7 @@ objects:
]
containers:
- image: image-registry.openshift-image-registry.svc:5000/d3d940-${ENV}/${NAME}:${VERSION}
imagePullPolicy: Always
name: api
ports:
- containerPort: 8080
Expand Down Expand Up @@ -126,16 +127,6 @@ objects:
defaultMode: 420
name: api-appsettings-${ENV}
test: false
triggers:
- imageChangeParams:
automatic: true
containerNames:
- api
from:
kind: ImageStreamTag
name: ${NAME}:${VERSION}
type: ImageChange
- type: ConfigChange
- apiVersion: v1
kind: Service
metadata:
Expand Down
11 changes: 1 addition & 10 deletions openshift/client-deploy-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ objects:
spec:
containers:
- image: image-registry.openshift-image-registry.svc:5000/d3d940-${ENV}/${NAME}:${VERSION}
imagePullPolicy: Always
name: client
ports:
- containerPort: 8080
Expand All @@ -63,16 +64,6 @@ objects:
- name: HMCR_DEPLOY_SUFFIX
value: ${SUFFIX}
test: false
triggers:
- imageChangeParams:
automatic: true
containerNames:
- client
from:
kind: ImageStreamTag
name: ${NAME}:${VERSION}
type: ImageChange
- type: ConfigChange
- apiVersion: v1
kind: Service
metadata:
Expand Down
11 changes: 1 addition & 10 deletions openshift/hangfire-deploy-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ objects:
]
containers:
- image: image-registry.openshift-image-registry.svc:5000/d3d940-${ENV}/${NAME}:${VERSION}
imagePullPolicy: Always
name: hangfire
ports:
- containerPort: 8080
Expand Down Expand Up @@ -101,16 +102,6 @@ objects:
defaultMode: 420
name: api-appsettings-${ENV}
test: false
triggers:
- imageChangeParams:
automatic: true
containerNames:
- hangfire
from:
kind: ImageStreamTag
name: ${NAME}:${VERSION}
type: ImageChange
- type: ConfigChange
- apiVersion: v1
kind: Service
metadata:
Expand Down

0 comments on commit 51c53b3

Please sign in to comment.