Replies: 1 comment
-
|
This might be useful for someone who prefers to handle CI separately from CD :) const apikey = process.env.DOKPLOY_API_KEY;
const url = process.env.DOKPLOY_URL;
const applicationId = process.env.DOKPLOY_APPLICATION_ID;
const newDockerImage = process.env.NEW_DOCKER_IMAGE;
if (!apikey){
throw new Error("DOKPLOY_API_KEY is not set");
}
if (!url){
throw new Error("DOKPLOY_URL is not set");
}
if (!applicationId){
throw new Error("DOKPLOY_APPLICATION_ID is not set");
}
if (!newDockerImage){
throw new Error("NEW_DOCKER_IMAGE is not set");
}
const checkIfApplicationExists = async (applicationId) => {
const response = await fetch(`${url}/application.one?applicationId=${applicationId}`, {
method: "GET",
headers: {
"x-api-key": apikey,
"Content-Type": "application/json"
}
});
if (!response.ok) {
throw new Error(`Error fetching application: ${response.statusText}`);
}
console.log(`Application ${applicationId} exists.`);
}
const changeDockerImage = async (applicationId, newDockerImage) => {
const response = await fetch(`${url}/application.update`, {
method: "POST",
headers: {
"x-api-key": apikey,
"Content-Type": "application/json"
},
body: JSON.stringify({
applicationId: applicationId,
dockerImage: newDockerImage
})
});
if (!response.ok) {
throw new Error(`Error updating application: ${response.statusText}`);
}
console.log(`Application ${applicationId} updated to use Docker image ${newDockerImage}.`);
}
const deployApplication = async (applicationId) => {
const response = await fetch(`${url}/application.deploy`, {
method: "POST",
headers: {
"x-api-key": apikey,
"Content-Type": "application/json"
},
body: JSON.stringify({
applicationId: applicationId,
title: "Automated deployment via CI/CD"
})
});
if (!response.ok) {
throw new Error(`Error deploying application: ${response.statusText}`);
}
console.log(`Application ${applicationId} deployment triggered.`);
}
const checkIfDeploymentSucceeded = async (applicationId) => {
let status = "running";
while(status === "running") {
const response = await fetch(`${url}/deployment.all?applicationId=${applicationId}`, {
method: "GET",
headers: {
"x-api-key": apikey,
"Content-Type": "application/json"
}
});
if (!response.ok) {
throw new Error(`Error fetching deployments: ${response.statusText}`);
}
const deployments = await response.json();
const latestDeployment = deployments[0];
status = latestDeployment.status;
console.log(`Current deployment status: ${status}`);
if(status === "error"){
throw new Error(`Deployment failed: ${latestDeployment.errorMessage}`);
}
await new Promise(resolve => setTimeout(resolve, 5000));
}
console.log(`Application ${applicationId} deployed successfully.`);
}
const main = async () => {
await checkIfApplicationExists(applicationId);
await changeDockerImage(applicationId, newDockerImage);
await deployApplication(applicationId);
await checkIfDeploymentSucceeded(applicationId);
}
main(); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello!
What is the easiest way to change the Docker image in an application via CI/CD?
Here’s the case:
I want to build a Docker image through CI/CD, but I don’t want to publish it as latest. Instead, I want to deploy using a specific Docker image tag. This approach helps distinguish versions and makes rollbacks easier in case of a failure.
Beta Was this translation helpful? Give feedback.
All reactions