Define a build job
First, we will clean up our existing workflow to have a focused build job. We'll simplify the matrix strategy from the previous lab for clarity, going back to a single version to keep the focus on job dependencies.
- On your GitHub repository page for
github-actions-demo, click the green Code button.
- Ensure the HTTPS tab is selected and copy the URL. It should look like
https://github.com/your-username/github-actions-demo.git.
- Open the terminal in the LabEx environment. The default path is
~/project.
- Use the
git clone command to download the repository. Replace your-username with your actual GitHub username.
cd ~/project
git clone https://github.com/your-username/github-actions-demo.git
Example Output:
Cloning into 'github-actions-demo'...
remote: Enumerating objects: X, done.
remote: Counting objects: 100% (X/X), done.
remote: Total X (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (X/X), done.
- Navigate into the cloned repository:
cd ~/project/github-actions-demo
-
Create a new workflow file .github/workflows/job-dependencies.yml using the WebIDE editor. You can find the file in the file explorer on the left side under project/github-actions-demo/.github/workflows/.
-
Start by creating the basic workflow structure. Add the workflow name and trigger:
name: Job Dependencies
on: [push]
- Add the jobs section and define the build job with its runner:
jobs:
build:
runs-on: ubuntu-latest
- Add the steps section. First, add the checkout step to get the repository code:
steps:
- uses: actions/checkout@v4
- Add the Node.js setup step:
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
- Add the step to install dependencies:
- name: Install dependencies
run: npm install
- Add the step to run tests:
- name: Run tests
run: npm test
- Add the build step that creates the artifact directory and file:
- name: Build project
run: |
mkdir dist
echo "Build artifact created at $(date)" > dist/build.txt
- Finally, add the upload artifact step. This step is crucial because it saves the build output so the next job can use it:
- name: Upload build artifact
uses: actions/upload-artifact@v4
with:
name: dist-files
path: dist
Your complete file should now look like this:
name: Job Dependencies
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
- name: Install dependencies
run: npm install
- name: Run tests
run: npm test
- name: Build project
run: |
mkdir dist
echo "Build artifact created at $(date)" > dist/build.txt
- name: Upload build artifact
uses: actions/upload-artifact@v4
with:
name: dist-files
path: dist
Explanation
- We removed the
matrix strategy for simplicity.
- We kept the
Upload build artifact step. This is crucial because the next job will need these files!
Save the file (Ctrl+S or Cmd+S) after making changes.