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

chore(docker): Add Github Actions to build and push Docker image #22

Merged
merged 3 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
76 changes: 76 additions & 0 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
name: Build project, then Docker Image and publish to Github Artifact Registry

on:
push:
# Publish semver tags as releases.
tags: ['v*.*.*']
pfongkye marked this conversation as resolved.
Show resolved Hide resolved

env:
REGISTRY: ghcr.io
# github.repository as <account>/<repo>
IMAGE_NAME: ${{ github.repository }}

jobs:
build:
name: Build from source to dist
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '18'
- run: yarn install
- run: yarn build
- name: Upload build artifact
uses: actions/upload-artifact@v4
with:
name: dist
path: dist

docker:
name: Build Docker Image and publish to Github Artifact Registry
needs: build
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
attestations: write
id-token: write
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: ↙️ Download build artifact
uses: actions/download-artifact@v4
with:
name: dist
path: dist

- name: Log into registry ${{ env.REGISTRY }}
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
greg0ire marked this conversation as resolved.
Show resolved Hide resolved
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

- name: Generate artifact attestation
uses: actions/attest-build-provenance@v1
with:
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}}
subject-digest: ${{ steps.build-and-push.outputs.digest }}
push-to-registry: true
20 changes: 20 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM node:18

RUN mkdir -p /home/node/app \
&& chown -R node:node /home/node/app

WORKDIR /home/node/app

ENV NODE_ENV=production

EXPOSE 3000

USER node

COPY --chown=node:node package.json /home/node/app/package.json
COPY --chown=node:node yarn.lock /home/node/app/yarn.lock
COPY --chown=node:node dist /home/node/app/dist

RUN yarn install --production
pfongkye marked this conversation as resolved.
Show resolved Hide resolved

CMD ["node", "dist/src/index.js"]
Loading