-
Notifications
You must be signed in to change notification settings - Fork 18
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
Add workflow to automate backstage builds #17
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c7a51bb
enable automated image builds
omrishiv 9e102fa
put workflow in correct folder
omrishiv 0732324
use correct branch name
omrishiv e721bea
tag image with repo
omrishiv 3cebe4c
tag image with repo
omrishiv 1d77fd9
tag image with repo
omrishiv 480103c
tag image with repo
omrishiv 8b4a3e6
remove hardcoded repository paths
omrishiv 2b3f056
workflow cleanup
omrishiv b524d9d
workflow cleanup
omrishiv 57df409
workflow cleanup
omrishiv 6d592b2
add id-token permission for attestation
omrishiv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
.git | ||
.yarn/cache | ||
.yarn/install-state.gz | ||
dist-types | ||
node_modules | ||
packages/*/src | ||
packages/*/dist | ||
packages/*/node_modules | ||
plugins | ||
*.local.yaml | ||
plugins/*/dist | ||
plugins/*/node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# | ||
name: Create and publish a Docker image | ||
|
||
# Configures this workflow to run every time a change is pushed. | ||
on: push | ||
|
||
# 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. | ||
env: | ||
REGISTRY: ghcr.io | ||
IMAGE_NAME: ${{ github.repository }} | ||
|
||
# There is a single job in this workflow. It's configured to run on the latest available version of Ubuntu. | ||
jobs: | ||
build-and-push-image: | ||
runs-on: ubuntu-latest | ||
# Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job. | ||
permissions: | ||
contents: read | ||
packages: write | ||
attestations: write | ||
id-token: write | ||
# | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
# Uses the `docker/login-action` action to log in to the Container registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here. | ||
- name: Log in to the Container registry | ||
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 | ||
with: | ||
registry: ${{ env.REGISTRY }} | ||
username: ${{ github.repository_owner }} | ||
password: ${{ secrets.GITHUB_TOKEN }} | ||
# 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. | ||
- name: Extract metadata (tags, labels) for Docker | ||
id: meta | ||
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 | ||
with: | ||
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | ||
tags: ${{ github.sha }} | ||
# 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. | ||
# 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. | ||
# It uses the `tags` and `labels` parameters to tag and label the image with the output from the "meta" step. | ||
- name: Build and push Docker image | ||
id: push | ||
uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4 | ||
with: | ||
context: . | ||
push: true | ||
tags: ${{ steps.meta.outputs.tags }} | ||
labels: ${{ steps.meta.outputs.labels }} | ||
|
||
# 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 "[AUTOTITLE](/actions/security-guides/using-artifact-attestations-to-establish-provenance-for-builds)." | ||
- name: Generate artifact attestation | ||
uses: actions/attest-build-provenance@v1 | ||
with: | ||
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}} | ||
subject-digest: ${{ steps.push.outputs.digest }} | ||
push-to-registry: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
# Stage 1 - Create yarn install skeleton layer | ||
FROM node:18-bookworm-slim AS packages | ||
|
||
WORKDIR /app | ||
COPY package.json yarn.lock ./ | ||
|
||
COPY packages packages | ||
|
||
# Comment this out if you don't have any internal plugins | ||
COPY plugins plugins | ||
|
||
RUN find packages \! -name "package.json" -mindepth 2 -maxdepth 2 -exec rm -rf {} \+ | ||
|
||
# Stage 2 - Install dependencies and build packages | ||
FROM node:18-bookworm-slim AS build | ||
|
||
# Install isolate-vm dependencies, these are needed by the @backstage/plugin-scaffolder-backend. | ||
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \ | ||
--mount=type=cache,target=/var/lib/apt,sharing=locked \ | ||
apt-get update && \ | ||
apt-get install -y --no-install-recommends python3 g++ build-essential git && \ | ||
yarn config set python /usr/bin/python3 | ||
|
||
# Install sqlite3 dependencies. You can skip this if you don't use sqlite3 in the image, | ||
# in which case you should also move better-sqlite3 to "devDependencies" in package.json. | ||
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \ | ||
--mount=type=cache,target=/var/lib/apt,sharing=locked \ | ||
apt-get update && \ | ||
apt-get install -y --no-install-recommends libsqlite3-dev | ||
|
||
USER node | ||
WORKDIR /app | ||
|
||
COPY --from=packages --chown=node:node /app . | ||
|
||
RUN --mount=type=cache,target=/home/node/.cache/yarn,sharing=locked,uid=1000,gid=1000 \ | ||
yarn install --frozen-lockfile --network-timeout 600000 | ||
|
||
COPY --chown=node:node . . | ||
|
||
RUN yarn tsc | ||
RUN yarn --cwd packages/backend build | ||
# If you have not yet migrated to package roles, use the following command instead: | ||
# RUN yarn --cwd packages/backend backstage-cli backend:bundle --build-dependencies | ||
|
||
RUN mkdir packages/backend/dist/skeleton packages/backend/dist/bundle \ | ||
&& tar xzf packages/backend/dist/skeleton.tar.gz -C packages/backend/dist/skeleton \ | ||
&& tar xzf packages/backend/dist/bundle.tar.gz -C packages/backend/dist/bundle | ||
|
||
# Stage 3 - Build the actual backend image and install production dependencies | ||
FROM node:18-bookworm-slim | ||
|
||
# Install isolate-vm dependencies, these are needed by the @backstage/plugin-scaffolder-backend. | ||
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \ | ||
--mount=type=cache,target=/var/lib/apt,sharing=locked \ | ||
apt-get update && \ | ||
apt-get install -y --no-install-recommends python3 g++ build-essential && \ | ||
yarn config set python /usr/bin/python3 | ||
|
||
# Install sqlite3 dependencies. You can skip this if you don't use sqlite3 in the image, | ||
# in which case you should also move better-sqlite3 to "devDependencies" in package.json. | ||
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \ | ||
--mount=type=cache,target=/var/lib/apt,sharing=locked \ | ||
apt-get update && \ | ||
apt-get install -y --no-install-recommends libsqlite3-dev | ||
|
||
# Add kubectl. | ||
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \ | ||
--mount=type=cache,target=/var/lib/apt,sharing=locked \ | ||
apt-get update && \ | ||
apt-get install -y --no-install-recommends apt-transport-https ca-certificates curl gpg | ||
|
||
RUN curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.29/deb/Release.key | gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg && \ | ||
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.29/deb/ /' | tee /etc/apt/sources.list.d/kubernetes.list | ||
|
||
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \ | ||
--mount=type=cache,target=/var/lib/apt,sharing=locked \ | ||
apt-get update && \ | ||
apt-get install -y --no-install-recommends kubectl | ||
|
||
# Add cnoe cli. | ||
RUN curl -L -O https://github.com/cnoe-io/cnoe-cli/releases/download/v0.1.0/cnoe_Linux_x86_64.tar.gz && \ | ||
curl -L -O https://github.com/cnoe-io/cnoe-cli/releases/download/v0.1.0/checksums.txt && \ | ||
sha256sum -c --strict --status --ignore-missing checksums.txt && \ | ||
tar -xzf cnoe_Linux_x86_64.tar.gz && \ | ||
mv cnoe /usr/bin/cnoe-cli && \ | ||
chmod +x /usr/bin/cnoe-cli | ||
|
||
COPY ./cnoe-wrapper.sh /usr/bin/cnoe | ||
RUN chmod +x /usr/bin/cnoe | ||
|
||
# From here on we use the least-privileged `node` user to run the backend. | ||
USER node | ||
|
||
# This should create the app dir as `node`. | ||
# If it is instead created as `root` then the `tar` command below will | ||
# fail: `can't create directory 'packages/': Permission denied`. | ||
# If this occurs, then ensure BuildKit is enabled (`DOCKER_BUILDKIT=1`) | ||
# so the app dir is correctly created as `node`. | ||
WORKDIR /app | ||
|
||
# Copy the install dependencies from the build stage and context | ||
COPY --from=build --chown=node:node /app/yarn.lock /app/package.json /app/packages/backend/dist/skeleton/ ./ | ||
|
||
RUN --mount=type=cache,target=/home/node/.cache/yarn,sharing=locked,uid=1000,gid=1000 \ | ||
yarn install --frozen-lockfile --production --network-timeout 600000 | ||
|
||
# Copy the built packages from the build stage | ||
COPY --from=build --chown=node:node /app/packages/backend/dist/bundle/ ./ | ||
|
||
# Copy any other files that we need at runtime | ||
COPY --chown=node:node app-config.yaml ./ | ||
|
||
# This switches many Node.js dependencies to production mode. | ||
ENV NODE_ENV production | ||
|
||
CMD ["node", "packages/backend", "--config", "app-config.yaml"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/bin/bash | ||
|
||
SERVICE_ACCOUNT_DIR="/var/run/secrets/kubernetes.io/serviceaccount" | ||
KUBERNETES_SERVICE_SCHEME=$(case $KUBERNETES_SERVICE_PORT in 80|8080|8081) echo "http";; *) echo "https"; esac) | ||
KUBERNETES_SERVER_URL="$KUBERNETES_SERVICE_SCHEME"://"$KUBERNETES_SERVICE_HOST":"$KUBERNETES_SERVICE_PORT" | ||
KUBERNETES_CLUSTER_CA_FILE="$SERVICE_ACCOUNT_DIR"/ca.crt | ||
KUBERNETES_NAMESPACE=$(cat "$SERVICE_ACCOUNT_DIR"/namespace) | ||
KUBERNETES_USER_TOKEN=$(cat "$SERVICE_ACCOUNT_DIR"/token) | ||
KUBERNETES_CONTEXT="inCluster" | ||
|
||
rm -rf "$HOME"/.kube | ||
mkdir -p "$HOME"/.kube | ||
cat << EOF > "$HOME"/.kube/config | ||
apiVersion: v1 | ||
kind: Config | ||
preferences: {} | ||
current-context: $KUBERNETES_CONTEXT | ||
clusters: | ||
- cluster: | ||
server: $KUBERNETES_SERVER_URL | ||
certificate-authority: $KUBERNETES_CLUSTER_CA_FILE | ||
name: inCluster | ||
users: | ||
- name: podServiceAccount | ||
user: | ||
token: $KUBERNETES_USER_TOKEN | ||
contexts: | ||
- context: | ||
cluster: inCluster | ||
user: podServiceAccount | ||
namespace: $KUBERNETES_NAMESPACE | ||
name: $KUBERNETES_CONTEXT | ||
EOF | ||
|
||
cnoe-cli "$@" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this for? We are invoking the cnoe cli for no reason?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jessesanford this is from your work; can you clarify? I just lifted it from the other repo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't thoroughly looked through the cli code, but does it use the same method as kubernetes to get a client? If that method is leveraged, it can use either the kubeconfig in the home directory or an in-cluster driven client using kubernetes service account and I believe we won't need this file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This allows for us to just-in-time add the auth for the local Kubernetes cluster when the cnoe cli is invoked by the cnoe plugin to do perform the "verify" step. Without it, the cnoe cli invocation dies because it can't auth to the local kube.