Skip to content

Commit 858337a

Browse files
authored
Docker CI/CD (#21)
* feat: searchProblem seed * feat: add Docker support with Dockerfile and CI workflow * fix: update deployment.yaml to correct Docker run command syntax
1 parent 463cb39 commit 858337a

File tree

7 files changed

+99
-7
lines changed

7 files changed

+99
-7
lines changed

.dockerignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.git
2+
.idea
3+
Dockerfile
4+
node_modules
5+
dist

.github/workflows/deployment.yaml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ jobs:
1919
password: ${{ secrets.SSH_PASSWORD }}
2020
port: ${{ secrets.SSH_PORT }}
2121
script: |
22-
cd CodePoker-be/
23-
git pull
24-
npm install
22+
IMAGE_ID=ghcr.io/${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
23+
docker pull $IMAGE_ID:latest
24+
docker stop codepoker-be
25+
docker rm codepoker-be
26+
docker run -d --name codepoker-be \
27+
-p ${{ secrets.SSH_PORT }}:${{ secrets.SSH_PORT }} \
28+
--restart=always \
29+
$IMAGE_ID:latest
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#
2+
name: Create and publish a Docker image
3+
4+
# Configures this workflow to run every time a change is pushed to the branch called `release`.
5+
on:
6+
push:
7+
branches: [ "main" ]
8+
pull_request:
9+
branches: [ "main" ]
10+
11+
# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds.
12+
env:
13+
REGISTRY: ghcr.io
14+
IMAGE_NAME: ${{ github.repository }}
15+
16+
# There is a single job in this workflow. It's configured to run on the latest available version of Ubuntu.
17+
jobs:
18+
build-and-push-image:
19+
runs-on: ubuntu-latest
20+
# Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job.
21+
permissions:
22+
contents: read
23+
packages: write
24+
attestations: write
25+
id-token: write
26+
#
27+
steps:
28+
- name: Checkout repository
29+
uses: actions/checkout@v4
30+
# Uses the `docker/login-action` action to log in to the Container registry registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here.
31+
- name: Log in to the Container registry
32+
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
33+
with:
34+
registry: ${{ env.REGISTRY }}
35+
username: ${{ github.actor }}
36+
password: ${{ secrets.GITHUB_TOKEN }}
37+
# This step uses [docker/metadata-action](https://github.com/docker/metadata-action#about) to extract tags and labels that will be applied to the specified image. The `id` "meta" allows the output of this step to be referenced in a subsequent step. The `images` value provides the base name for the tags and labels.
38+
- name: Extract metadata (tags, labels) for Docker
39+
id: meta
40+
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
41+
with:
42+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
43+
# This step uses the `docker/build-push-action` action to build the image, based on your repository's `Dockerfile`. If the build succeeds, it pushes the image to GitHub Packages.
44+
# It uses the `context` parameter to define the build's context as the set of files located in the specified path. For more information, see [Usage](https://github.com/docker/build-push-action#usage) in the README of the `docker/build-push-action` repository.
45+
# It uses the `tags` and `labels` parameters to tag and label the image with the output from the "meta" step.
46+
- name: Build and push Docker image
47+
id: push
48+
uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4
49+
with:
50+
context: .
51+
push: true
52+
tags: ${{ steps.meta.outputs.tags }}
53+
labels: ${{ steps.meta.outputs.labels }}
54+
55+
# This step generates an artifact attestation for the image, which is an unforgeable statement about where and how it was built. It increases supply chain security for people who consume the image. For more information, see [Using artifact attestations to establish provenance for builds](/actions/security-guides/using-artifact-attestations-to-establish-provenance-for-builds).
56+
- name: Generate artifact attestation
57+
uses: actions/attest-build-provenance@v2
58+
with:
59+
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}}
60+
subject-digest: ${{ steps.push.outputs.digest }}
61+
push-to-registry: true
62+

Dockerfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM node:18
2+
3+
RUN mkdir -p /var/app
4+
5+
WORKDIR /var/app
6+
7+
COPY . .
8+
9+
RUN npm install
10+
RUN npm run build
11+
12+
EXPOSE $PORT
13+
14+
CMD [ "npm", "run", "start" ]

src/solved/solved.controller.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,18 @@ export class SolvedController {
108108
name: 'direction',
109109
required: false,
110110
})
111+
@ApiQuery({
112+
name: 'seed',
113+
required: false,
114+
})
111115
searchProblem(
112116
@Query('query') query: string,
113117
@Query('page') page: number = 1,
114118
@Query('sort') sort: string = 'solved',
115119
@Query('direction') direction: string = 'descending',
120+
@Query('seed') seed: string = '',
116121
) {
117-
return this.solvedService.searchProblem(query, page, sort, direction);
122+
return this.solvedService.searchProblem(query, page, sort, direction, seed);
118123
}
119124

120125
@Get('search/user')

src/solved/solved.repository.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export class SolvedRepository {
174174
.then((res) => res.data);
175175
}
176176

177-
async searchProblem(query: string, page: number, sort: string, direction: string) {
177+
async searchProblem(query: string, page: number, sort: string, direction: string, seed: string) {
178178
const url = `https://solved.ac/api/v3/search/problem`;
179179
return await this.httpService.axiosRef
180180
.get(url, {
@@ -183,6 +183,7 @@ export class SolvedRepository {
183183
page: page,
184184
sort: sort,
185185
direction: direction,
186+
seed: seed,
186187
},
187188
})
188189
.then((res) => res.data);

src/solved/solved.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ export class SolvedService {
6262
return await this.solvedRepository.searchSuggestion(query);
6363
}
6464

65-
async searchProblem(query: string, page: number, sort: string, direction: string) {
66-
return await this.solvedRepository.searchProblem(query, page, sort, direction);
65+
async searchProblem(query: string, page: number, sort: string, direction: string, seed: string) {
66+
return await this.solvedRepository.searchProblem(query, page, sort, direction, seed);
6767
}
6868

6969
async searchUser(query: string, page: number) {

0 commit comments

Comments
 (0)