Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 17 additions & 7 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,32 @@ on:
branches: [ master ]
pull_request:
branches: [ master ]
workflow_dispatch:

jobs:
setup-build-publish-deploy:
name: Setup, Build, Publish, and Deploy
runs-on: ubuntu-latest
environment: GCP Deploy
steps:

- name: Checkout
uses: actions/checkout@master

# Setup gcloud CLI
- uses: google-github-actions/setup-gcloud@v0
- id: 'auth'
uses: 'google-github-actions/auth@v2'
with:
service_account_email: ${{ secrets.GCP_EMAIL }}
service_account_key: ${{ secrets.GCP_CREDENTIALS }}
export_default_credentials: true
credentials_json: '${{ secrets.GCP_CREDENTIALS }}'

- name: 'Set up Cloud SDK'
uses: 'google-github-actions/setup-gcloud@v2'

# Setup gcloud CLI
# - uses: google-github-actions/setup-gcloud@v2
# with:
# service_account_email: ${{ secrets.GCP_EMAIL }}
# service_account_key: ${{ secrets.GCP_CREDENTIALS }}
# export_default_credentials: true

# Configure Docker with Credentials
- name: Configure Docker
Expand All @@ -29,8 +39,8 @@ jobs:

# Build the Docker image
- name: Build & Publish
# gcloud config set project ${{ secrets.GCP_PROJECT }}
run: |
gcloud config set project ${{ secrets.GCP_PROJECT }}
gcloud builds submit --tag gcr.io/${{ secrets.GCP_PROJECT }}/${{ secrets.GCP_APPLICATION }}
gcloud config set run/region us-central1

Expand Down
5 changes: 5 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ RUN npm install -g express-generator
RUN npm install express --save
RUN useradd -ms /bin/bash user
COPY app.js /home/user/app.js
COPY site.html /home/user/index.html
COPY gcp_iam.png /home/user/gcp_iam.png
COPY start.sh /home/user/start.sh
RUN chmod a+x /home/user/start.sh
USER user
WORKDIR /home/user
RUN mkdir public
RUN mv /home/user/index.html /home/user/public/index.html
RUN mv /home/user/gcp_iam.png /home/user/public/gcp_iam.png

CMD ["sh","/home/user/start.sh"]
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,23 @@ In order for this to be provisioned on your Google Cloud instance, you need to m
* GCP_PROJECT

You'll also need to activate a couple of APIs in Google Cloud, the first deployment will probably fail and point you into the right direction. Alternatively, you could deploy the first version manually.

## Updates

### Web Access

https://nodejsapi-hkqcqmboga-uc.a.run.app
https://nodejsapi-hkqcqmboga-uc.a.run.app/site

### Changed GCP action to version 2

This is to fix the deprecation error on version 0 before.

The new version requires different authentication semantics. A separate auth action is added, depending only on the JSON key of an GCP service account, from the `GCP_CREDENTIALS`; `GCP_EMAIL` is no longer needed.

### Workflow change for the forked repo

We add an GitHub environment for storing the deployment credentials. This is added to the `deploy.yml` script, so the workflow can access the credentials variables, including the GCP service account key.

The GCP service account used in this project is created as an Editor role, under a GCP project. It thus only have access to this project and do not need to switch project in the `gcloud` shell.

2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ app.get("/random", (req, res, next) => {
res.json([Math.floor(Math.random() * 90) + 10]);
});

app.use('/site', express.static('public'));

app.get('*',function (req, res) {
res.redirect('/');
});
Expand Down
2 changes: 2 additions & 0 deletions site.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hello, this is a static HTML page.
<img src="gcp_iam.png" alt="Sample Image">