Skip to content

Commit

Permalink
cd: add tests step to continuous deployment process
Browse files Browse the repository at this point in the history
Rename `build-and-push` workflow file to `continuous-deployment`, And
add three steps to the main & only job of the workflow after the first step,
the new steps:
- "Install dependencies" step
- "Create `.env.secret` file" step
- "Run tests" step

Write an additional scripts in `mongodb-replicas-init.sh` for checking
the status of the members in mongo database replica set frequently every
1 second to know if the database (the replica set) is ready to receive queries.
  • Loading branch information
AbdulrhmanGoni committed Apr 5, 2024
1 parent 8b1c4ac commit 7a7bc6b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build a docker image, push it to docker hub and trigger deploy event
name: Continuous deployment workflow

on:
push:
Expand All @@ -7,19 +7,28 @@ on:
branches: [main]

jobs:
build-and-push:
continuous-deployment:
runs-on: ubuntu-latest
steps:
- name: Fetch the repository
uses: actions/checkout@v4

- name: Build the Docker image
- name: Install the dependencies
run: npm install

- name: Create `.env.secret` file
run: echo "${{ secrets.TESTING_ENV_FILE_CONTENT }}" >> .env.secret

- name: Run tests
run: npm run test

- name: Build the Docker Image
run: docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/am-store-server .

- name: Log in to Docker hub
run: echo ${{ secrets.DOCKERHUB_PASSWORD }} | docker login -u ${{ secrets.DOCKERHUB_USERNAME }} --password-stdin

- name: Push the Docker image
- name: Push the Docker Image
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/am-store-server

- name: Trigger deploy event for the server
Expand Down
27 changes: 26 additions & 1 deletion scripts/mongodb-replicas-init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,30 @@ var config = {
}
]
};
rs.initiate(config, { force: true });
rs.status();
function isItReadyMember(member) {
if (member.name === "mongodb1:27017") {
return member.state === 1
} else {
return member.state === 2
}
}
function checkMembersStatus(members) {
return members.every(isItReadyMember)
}
async function checkReplicaSetReadiness() {
return new Promise((resolve) => {
console.log("Wait the database to be ready...")
setInterval(() => {
if (checkMembersStatus(rs.status().members)) {
resolve("The database is ready to recieve queries")
}
}, 1000)
})
}
checkReplicaSetReadiness().then((result) => { console.log(result) });

0 comments on commit 7a7bc6b

Please sign in to comment.