Skip to content

Bootcamp update 2024 #5

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
6 changes: 3 additions & 3 deletions .github/workflows/cd-docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
# run: |
# npm ci
# npm run build --if-present
# - uses: actions/upload-artifact@v3
# - uses: actions/upload-artifact@v4
# with:
# name: build-code
# path: hangman-api/dist/
Expand All @@ -34,14 +34,14 @@ jobs:

steps:
- uses: actions/checkout@v4
- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
with:
name: build-code
path: hangman-api/dist/
- name: Build and Push Docker Image
working-directory: ./hangman-api
env:
DOCKER_USER: "jaimesalas"
DOCKER_USER: "jaimesalas" # <your-docker-username>
DOCKER_REPOSITORY: "hangman-api"
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
run: |
Expand Down
72 changes: 72 additions & 0 deletions .github/workflows/cd-using-docker-actions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: Docker Image hangman API CD (using Docker actions)

on:
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 16
cache: 'npm'
cache-dependency-path: hangman-api/package-lock.json
- name: build
working-directory: ./hangman-api
run: |
npm ci
npm run build --if-present
- uses: actions/upload-artifact@v4
with:
name: build-code
path: hangman-api/dist/

# delivery:
# runs-on: ubuntu-latest
# needs: build

# steps:
# - uses: actions/checkout@v4
# - uses: actions/download-artifact@v4
# with:
# name: build-code
# path: hangman-api/dist/
# - name: Build and Push Docker Image
# working-directory: ./hangman-api
# env:
# DOCKER_USER: "jaimesalas" # <your-docker-username>
# DOCKER_REPOSITORY: "hangman-api"
# DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
# run: |
# echo $DOCKER_PASSWORD | docker login --username $DOCKER_USER --password-stdin
# image=$DOCKER_USER/$DOCKER_REPOSITORY:$(date +%s)
# docker build -t $image -f Dockerfile.workflow .
# docker push $image

delivery:
runs-on: ubuntu-latest
needs: build

steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Download dist folder as artifact
uses: actions/download-artifact@v4
- name: Docker Hub login
uses: docker/login-action@v3
with:
# registry: by default is set to Docker Hub
username: jtrillo # <your_docker_username>
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build and push Docker Image
uses: docker/build-push-action@v5
with:
context: ./hangman-api
push: true
tags: jtrillo/hangman-api-actions:latest # <your_docker_username>
file: ./hangman-api/Dockerfile.workflow
2 changes: 1 addition & 1 deletion .github/workflows/reusable-node-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
run: |
npm ci
npm run build --if-present
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
with:
name: build-code
path: ${{ inputs.working-directory }}/dist/
3 changes: 2 additions & 1 deletion .github/workflows/test-custom-action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ jobs:
uses: jtrillo/get-commodity-price@v1.0.1
with:
commodity: silver
currency: EUR
# Use the output from 'commodity_price' step
- name: Get the output price
run: echo "Price per ounce is ${{ steps.commodity_price.outputs.price }} USD"
run: echo "Price per ounce is ${{ steps.commodity_price.outputs.price }} ${{ steps.commodity_price.inputs.currency }}"
3 changes: 1 addition & 2 deletions .start-code/hangman-api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,4 @@ ENV NODE_ENV=production

RUN npm install


CMD ["npm", "start"]
CMD ["npm", "start"]
5 changes: 0 additions & 5 deletions 01-setup-ci/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ jobs:
whoami
pwd
node -v


```

Let's commit this file to a new branch:
Expand Down Expand Up @@ -149,7 +147,6 @@ jobs:
- whoami
- pwd
- node -v

```

```yaml
Expand Down Expand Up @@ -210,9 +207,7 @@ jobs:
run: |
npm ci
npm run build --if-present
ls ./dist
npm test

```

* With `paths` we filter the directories that are able to trigger a new workflow.
Expand Down
26 changes: 18 additions & 8 deletions 04-working-with-build-artifacts/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ jobs:
npm ci
npm run build --if-present
# diff #
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
with:
name: dependencies
path: hangman-api/node_modules/
include-hidden-files: true
# diff #
```

Expand All @@ -72,10 +73,11 @@ jobs:
run: |
npm ci
npm run build --if-present
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
with:
name: dependencies
path: hangman-api/node_modules/
include-hidden-files: true

test:
runs-on: ubuntu-latest
Expand All @@ -84,14 +86,17 @@ jobs:
steps:
- uses: actions/checkout@v4
# diff
- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
with:
name: dependencies
path: hangman-api/node_modules
# diff
include-hidden-files: true
- name: test
working-directory: ./hangman-api
run: npm test # npm ci is not needed here
run: |
chmod -R +x node_modules
npm test
# diff
```

Let's push the changes done.
Expand All @@ -104,7 +109,9 @@ git push

And trigger the workflow from project site.

If we check the workflow, we will notice that is taking a long time to resolve the dependencies upload. This is not the way to manage this, let's try something different.
**DEPRECATED.** ~~If we check the workflow, we will notice that is taking a long time to resolve the dependencies upload~~. **Both actions in version 4 improve significatly the performance. Check this article: <https://github.blog/changelog/2024-04-16-deprecation-notice-v3-of-the-artifact-actions/>**.

However, this approach is not the proper way to manage this, let's try something different: using the cache.

* [Caching dependencies to speed up workflows](https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows)

Expand All @@ -127,27 +134,30 @@ jobs:
run: |
npm ci
npm run build --if-present
- - uses: actions/upload-artifact@v3
- - uses: actions/upload-artifact@v4
- with:
- name: dependencies
- path: hangman-api/node_modules/
- include-hidden-files: true

test:
runs-on: ubuntu-latest
needs: build

steps:
- uses: actions/checkout@v4
- - uses: actions/download-artifact@v3.0.0
- - uses: actions/download-artifact@v4
- with:
- name: dependencies
- path: hangman-api/node_modules
- include-hidden-files: true
+ - uses: actions/setup-node@v4
+ with:
+ node-version: 16
- name: test
working-directory: ./hangman-api
run: |
- chmod -R +x node_modules
+ npm ci
npm test
```
Expand Down
13 changes: 11 additions & 2 deletions 06-service-containers/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The previous solution for integration test works, but have some downsides.
* Dockerfile.migrations
* Dockerfile.test-integration

This code is not related with our solution, is just there to solve the CI issue. Well is not the end of the world, but is thera another way that we can solve this? Let's introduce [Service Containers](https://docs.github.com/en/actions/using-containerized-services/about-service-containers)
This code is not related with our solution, it is just there to solve the CI issue. Well, this is not the end of the world, but is there another way to execute the integration tests without adding these files? Let's introduce [Service Containers](https://docs.github.com/en/actions/using-containerized-services/about-service-containers)

> **About service containers** - Service containers are Docker containers that provide a simple and portable way for you to host services that you might need to test or operate your application in a workflow. For example, your workflow might need to run integration tests that require access to a database and memory cache.

Expand Down Expand Up @@ -71,7 +71,7 @@ git commit -m "added database relationships step"
git push
```

* Run the workflow manually from GiHub website.
* Run the workflow manually from GitHub website.

If everything goes right we must see and output as follows:

Expand Down Expand Up @@ -138,3 +138,12 @@ Snapshots: 0 total
Time: 3.519 s
Ran all test suites.
```

* Before moving on, let's delete files added in the previous demo (`wait-for-it.sh`, `test-integration.yml`, `Dockerfile.migrations` and `Dockerfile.test-integration`) to verify that service containers approach does not need them.

```bash
rm wait-for-it.sh test-integration.yml Dockerfile.migrations Dockerfile.test-integration
git add .
git commit -m "Delete no longer required files to run the integration tests"
git push
```
30 changes: 15 additions & 15 deletions 08-continous-delivery/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ Before adding the required steps into our workflow, let's try it on local.
First from `hangman-api` root directory run:

```bash
docker build -t jaimesalas/hangman-api .
docker build -t <your_docker_user>/hangman-api .
```

And test by running:

```bash
docker run -d -p 3000:3000 jaimesalas/hangman-api
docker run -d -p 3000:3000 <your_docker_user>/hangman-api
```

```bash
Expand All @@ -31,7 +31,7 @@ We could also push it to Docker Hub

```bash
docker login # Just if not already logged
docker push jaimesalas/hangman-api
docker push <your_docker_user>/hangman-api
```

## Building the Docker Image in a workflow
Expand Down Expand Up @@ -115,7 +115,7 @@ CMD ["npm", "start"]
We should use the following command to build an image with an specific Node.js version:

```bash
docker build -t jtrillo/hangman-api -f Dockerfile.workflow --build-arg version=20.10-alpine .
docker build -t <your_docker_username>/hangman-api -f Dockerfile.workflow --build-arg version=20.10-alpine .
```

First, we're going to use the same build job as the one on `ci.yml`. But here we're going to upload the build as an artifact to be used on a new `delivery` job:
Expand Down Expand Up @@ -144,7 +144,7 @@ jobs:
run: |
npm ci
npm run build --if-present
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
with:
name: build-code
path: hangman-api/dist/
Expand All @@ -161,7 +161,7 @@ Now we need to build the image before push it to Docker registry we can do this

steps:
- uses: actions/checkout@v4
- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
with:
name: build-code
path: hangman-api/dist/
Expand Down Expand Up @@ -195,7 +195,7 @@ Ok, almost done. There are prebaked actions to authenticate and push Docker imag
+ - name: Build and Push Docker Image
working-directory: ./hangman-api
+ env:
+ DOCKER_USER: "jaimesalas"
+ DOCKER_USER: "jaimesalas" # <your-docker-username>
+ DOCKER_REPOSITORY: "hangman-api"
+ DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
- run: docker build . --file Dockerfile.workflow --tag my-image-name:$(date +%s)
Expand Down Expand Up @@ -224,8 +224,8 @@ And fire the workflow from GitHub site. If everything works we must see somethin
```console
---> 12c19641de84
Successfully built 12c19641de84
Successfully tagged jaimesalas/hangman-api:1665501807
The push refers to repository [docker.io/jaimesalas/hangman-api]
Successfully tagged <your_docker_user>/hangman-api:1665501807
The push refers to repository [docker.io/<your_docker_user>/hangman-api]
```

And visit Docker Hub to find out the uploaded image
Expand All @@ -238,23 +238,23 @@ In this case, we will use actions instead of commands. We are going to make use
* [docker/setup-buildx-action](https://github.com/marketplace/actions/docker-setup-buildx)
* [docker/build-push-action](https://github.com/marketplace/actions/build-and-push-docker-images)

Let's add a new job to our workflow.
Let's update `delivery` job:

```yaml
buildAndPush:
delivery:
runs-on: ubuntu-latest
needs: build

steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Download dist folder as artifact
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
- name: Docker Hub login
uses: docker/login-action@v3
with:
# registry: by default is set to Docker Hub
username: jtrillo
username: jtrillo # <your_docker_username>
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
Expand All @@ -263,15 +263,15 @@ Let's add a new job to our workflow.
with:
context: ./hangman-api
push: true
tags: jtrillo/hangman-api-actions:latest
tags: jtrillo/hangman-api-actions:latest # <your_docker_username>
file: ./hangman-api/Dockerfile.workflow
```

Push the new changes

```bash
git add .
git commit -m "added buildAndPush job"
git commit -m "update delivery job"
git push
```

Expand Down
Loading