Skip to main content

GitHub Actions and S3

Published

2026-12-01

In Create S3 & Push/Pull Data we copied the model folders to S3 by hand. GitHub Actions can build and publish our vetiver models to S3 whenever we push changes to a specific branch. We covered GitHub Actions in Lab 5 and GitHub Actions as CI/CD. I’ve stored the Quarto files for building, pushing, and pulling the model data in the _labs/lab07/ folder.

In this section I’ll cover connecting GitHub to AWS with OpenID Connect (OIDC) and creating a workflow to automate the model publishing process.

%%{init: {'theme': 'base', 'look': 'classic', 'themeVariables': {'fontFamily': 'monospace'}}}%%

graph TD
    Push(["<strong>Developer</strong><br/><em>Pushes to branch</em>"]) 
    GHA["<strong>GitHub Actions</strong>"]
    Push --Triggers--> GHA
    GHA --> Auth("<strong>Assume IAM Role</strong><br><em>(OIDC)</em>")
    Auth --"Build Models"--> Build("<strong>Vetiver Models</strong><br/><em>(py_model.qmd, <br>r_model.qmd)</em>")
    Build --"Upload to"--> S3[("<strong>S3</strong><br/><em>(using pins)</em>")]
    S3 --> Success(["Models<br/>Published"])

    style Push fill:#5B8C5A,stroke:#000000,stroke-width:1px,color:#ffffff
    style GHA fill:#E8A33D,stroke:#000000,stroke-width:1px,color:#ffffff
    style Build fill:#1B2A41,stroke:#000000,stroke-width:1px,color:#ffffff
    style Auth fill:#2A6F77,stroke:#000000,stroke-width:1px,color:#ffffff
    style S3 fill:#D2562B,stroke:#000000,stroke-width:1px,color:#ffffff
    style Success fill:#5B8C5A,stroke:#000000,stroke-width:1px,color:#ffffff

GitHub Actions Automated Deployment

Automating Model Deployment with GitHub Actions

The workflow needs AWS credentials to write to S3. We could store the mjfrigaard-cli access key in the repository’s secrets, but that key can do much more than this workflow needs. It carries every permission in dev-deploy-group (including AmazonEC2FullAccess and IAMFullAccess), it never expires, and our local dev-deploy profile uses the same key. If the secret leaked, someone could launch instances or create IAM users. Rotating the key (Rotating access keys) would also mean updating the laptop and GitHub at the same time.

OIDC avoids storing a key at all. Each workflow run asks GitHub for a signed token that names the repository and branch it’s running from. AWS checks the token and hands back temporary credentials for an IAM role, and those credentials expire after an hour. The role can only write to the model bucket, and only this repository’s lab07-s3 branch can assume it.

%%{init: {'theme': 'base', 'look': 'classic', 'themeVariables': {'fontFamily': 'monospace'}}}%%

graph TD
    Run(["<strong>Workflow Run</strong><br/><em>(lab07-s3)</em>"])
    Token("<strong>GitHub OIDC Token</strong><br/><em>repo + branch</em>")
    STS("<strong>AWS STS</strong><br/><em>checks trust policy</em>")
    Role("<strong>gha-s3-publish-role</strong>")
    Creds("Temporary AWS<br/>Credentials (1 hour)")
    Access[("Access to S3")]

    Run --"requests"--> Token
    Token --"AssumeRoleWithWebIdentity"--> STS
    STS --> Role
    Role --> Creds
    Creds --> Access

    style Run fill:#E8A33D,stroke:#000000,stroke-width:1px,color:#ffffff
    style Token fill:#1B2A41,stroke:#000000,stroke-width:1px,color:#ffffff
    style STS fill:#2A6F77,stroke:#000000,stroke-width:1px,color:#ffffff
    style Role fill:#D2562B,stroke:#000000,stroke-width:1px,color:#ffffff
    style Creds fill:#2A6F77,stroke:#000000,stroke-width:1px,color:#ffffff
    style Access fill:#5B8C5A,stroke:#000000,stroke-width:1px,color:#ffffff

GitHub OIDC and AWS

Setting this up takes three steps in AWS (an identity provider, a role, and a permissions policy) and two repository variables. The AWS commands use the dev-deploy profile, which has IAMFullAccess from Stand up an EC2 instance.

Adding GitHub as an identity provider

First we tell IAM to trust tokens signed by GitHub. We only do this once per AWS account:

aws iam create-open-id-connect-provider \
  --url https://token.actions.githubusercontent.com \
  --client-id-list sts.amazonaws.com \
  --profile dev-deploy

The output is the provider’s ARN:

{
    "OpenIDConnectProviderArn": "arn:aws:iam::ACCOUNT_ID:oidc-provider/token.actions.githubusercontent.com"
}

Writing the trust policy

The role’s trust policy decides who can assume it. Store the account ID in a shell variable, then write the policy to trust-policy.json. Type ACCOUNT_ID exactly as written (don’t replace it with your account number), because the shell fills in the number for us. Run both commands in the same Terminal session from the repository root:

ACCOUNT_ID=$(aws sts get-caller-identity \
  --query Account --output text \
  --profile dev-deploy)

cat > trust-policy.json <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Federated": "arn:aws:iam::${ACCOUNT_ID}:oidc-provider/token.actions.githubusercontent.com"
      },
      "Action": "sts:AssumeRoleWithWebIdentity",
      "Condition": {
        "StringEquals": {
          "token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
          "token.actions.githubusercontent.com:sub": "repo:mjfrigaard/do4ds-labs:ref:refs/heads/lab07-s3"
        }
      }
    }
  ]
}
EOF

Confirm the shell filled in the account ID by searching the file for the Federated line:

grep Federated trust-policy.json

The ARN should contain the 12 digit account ID (replaced with ACCOUNT_ID below):

        "Federated": "arn:aws:iam::ACCOUNT_ID:oidc-provider/token.actions.githubusercontent.com"

If the ARN shows arn:aws:iam:::oidc-provider (nothing between the colons), ACCOUNT_ID wasn’t set in this shell. Run the ACCOUNT_ID= line again and rewrite the file.

The aud condition checks that the token was issued for AWS. The sub condition limits the role to pushes on the lab07-s3 branch of this repository. A workflow in any other repository (or on another branch) can’t assume it.

Creating the role

Create the role from the trust policy:

aws iam create-role \
  --role-name gha-s3-publish-role \
  --assume-role-policy-document file://trust-policy.json \
  --profile dev-deploy

The output includes the role’s ARN (arn:aws:iam::<ACCOUNT_ID>:role/gha-s3-publish-role), which goes in the repository below.

NoteAWS Pager Output

The output repeats because the AWS CLI sends long output through a pager (less), and each scroll redrew it. Press q to get back to the prompt. To turn the pager off for this session, run:

export AWS_PAGER=""

Limiting the role to the model bucket

The new role has no permissions. pins lists, reads, and writes objects in the bucket, so the permissions policy allows those three actions on penguin-vetiver-model-data and nothing else:

cat > s3-publish-policy.json <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "s3:ListBucket",
      "Resource": "arn:aws:s3:::penguin-vetiver-model-data"
    },
    {
      "Effect": "Allow",
      "Action": ["s3:GetObject", "s3:PutObject"],
      "Resource": "arn:aws:s3:::penguin-vetiver-model-data/*"
    }
  ]
}
EOF

aws iam put-role-policy \
  --role-name gha-s3-publish-role \
  --policy-name s3-publish \
  --policy-document file://s3-publish-policy.json \
  --profile dev-deploy

ListBucket applies to the bucket itself, while GetObject and PutObject apply to the objects inside it (the /*). put-role-policy returns no output on success, so we check the role directly. list-role-policies lists the policies attached to the role:

aws iam list-role-policies \
  --role-name gha-s3-publish-role \
  --profile dev-deploy
{
    "PolicyNames": [
        "s3-publish"
    ]
}

get-role-policy returns the policy itself, so we can confirm the actions and the bucket:

aws iam get-role-policy \
  --role-name gha-s3-publish-role \
  --policy-name s3-publish \
  --profile dev-deploy
{
    "RoleName": "gha-s3-publish-role",
    "PolicyName": "s3-publish",
    "PolicyDocument": {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Effect": "Allow",
                "Action": "s3:ListBucket",
                "Resource": "arn:aws:s3:::penguin-vetiver-model-data"
            },
            {
                "Effect": "Allow",
                "Action": [
                    "s3:GetObject",
                    "s3:PutObject"
                ],
                "Resource": "arn:aws:s3:::penguin-vetiver-model-data/*"
            }
        ]
    }
}

IAM now keeps its own copy of each policy, so we can delete the two JSON files:

rm trust-policy.json s3-publish-policy.json

Setting up GitHub Variables

The workflow reads two values from the repository (Settings > Secrets and variables > Actions > Variables). Neither one is sensitive. The role ARN is useless outside the repository and branch named in the trust policy:

  1. AWS_ROLE_ARN: the ARN of gha-s3-publish-role
  2. AWS_REGION: AWS region (e.g., us-east-1)

We can set both from the Terminal with the GitHub CLI (in the same shell, so ACCOUNT_ID is still set):

gh variable set AWS_ROLE_ARN \
  --body "arn:aws:iam::${ACCOUNT_ID}:role/gha-s3-publish-role"
✓ Created variable AWS_ROLE_ARN for mjfrigaard/do4ds-labs
gh variable set AWS_REGION --body "us-east-1"
✓ Updated variable AWS_REGION for mjfrigaard/do4ds-labs
gh variable list
NAME          VALUE                                             UPDATED
AWS_REGION    us-east-1                                         less than a minute ago
AWS_ROLE_ARN  arn:aws:iam::ACCOUNT_ID:role/gha-s3-publish-role  about 1 minute ago

Removing old AWS secrets

The repository doesn’t need any AWS secrets. If it still has AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY from an earlier setup, delete them after the first successful run with the role:

gh secret delete AWS_ACCESS_KEY_ID
gh secret delete AWS_SECRET_ACCESS_KEY

If a secret doesn’t exist, gh secret delete returns a 404 error:

failed to delete secret AWS_ACCESS_KEY_ID: HTTP 404 (https://api.github.com/repos/mjfrigaard/do4ds-labs/actions/secrets/AWS_ACCESS_KEY_ID)

The 404 means there’s nothing to delete, so we can ignore it. gh secret list confirms the repository has no secrets left (it prints no secrets found):

gh secret list

GitHub Actions Workflow File

Next, we create .github/workflows/publish.yml1 to build and publish the models to our S3 bucket. vetiver_pin_write() saves each model as a new pins version (e.g., 20260710T065220Z-91fdd), so we’re creating a new version in S3 with each workflow run.

name: Publish Models to S3

on:
  push:
    branches: [lab07-s3]
    paths:
      - '_labs/lab07/py_model.qmd'
      - '_labs/lab07/r_model.qmd'
      - '.github/workflows/publish.yml'

permissions:
  id-token: write
  contents: read

jobs:
  publish-models:
    runs-on: ubuntu-latest

    steps:
      - name: Check out repository
        uses: actions/checkout@v4

      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v4
        with:
          role-to-assume: ${{ vars.AWS_ROLE_ARN }}
          aws-region: ${{ vars.AWS_REGION || 'us-east-1' }}

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.12'

      - name: Set up R
        uses: r-lib/actions/setup-r@v2
        with:
          r-version: '4.4'
          use-public-rspm: true

      - name: Set up Quarto
        uses: quarto-dev/quarto-actions/setup@v2

      - name: Install Python dependencies
        working-directory: ./_labs/lab07
        run: |
          python -m pip install --upgrade pip
          pip install -r Python/requirements.txt pins vetiver python-dotenv s3fs jupyter "setuptools<81"

      - name: Install R dependencies
        working-directory: ./_labs/lab07
        run: |
          R -e "install.packages(c('vetiver', 'pins', 'plumber2', 'paws.storage', 'palmerpenguins', 'duckdb', 'DBI', 'dplyr', 'rmarkdown', 'knitr'))"

      - name: Build and publish Python model
        working-directory: ./_labs/lab07
        env:
          USE_S3: 'true'
          AWS_REGION: ${{ vars.AWS_REGION || 'us-east-1' }}
          PINS_ALLOW_PICKLE_READ: '1'
        run: |
          quarto render py_model.qmd

      - name: Build and publish R model
        working-directory: ./_labs/lab07
        env:
          USE_S3: 'true'
          AWS_REGION: ${{ vars.AWS_REGION || 'us-east-1' }}
        run: |
          quarto render r_model.qmd
          
1
Trigger: Runs when py_model.qmd, r_model.qmd, or this workflow file changes on the lab07-s3 branch
2
Permissions: id-token: write lets the job request an OIDC token from GitHub. Setting permissions removes the defaults, so contents: read keeps checkout working
3
Checkout: Downloads the latest code from the repository
4
AWS Auth: Uses aws-actions/configure-aws-credentials to trade the OIDC token for temporary credentials from gha-s3-publish-role
5
Setup: Installs Python 3.12, R 4.4, Quarto, and the required dependencies
6
Build & Publish: Renders py_model.qmd and r_model.qmd with the USE_S3=true environment variable, which causes the model code to write directly to S3

The workflow renders the same .qmd files we render locally, so there are no separate scripts to keep in sync.

The use-public-rspm: true option in the Set up R step points R at the Posit Public Package Manager, which serves prebuilt Linux binaries. Without it, install.packages() compiles every R package from source. The first successful run of this workflow took about 25 minutes (most of it in Install R dependencies).

Publishing with GitHub Actions automates the model deployment without storing any AWS credentials in the repository.

Developing on the lab07-s3 Branch

I want the main branch reserved for the Quarto book rendering. So I’ll create and check out a lab07-s3 branch from main for model changes (pushing to this branch triggers the model publishing workflow):

git checkout -b lab07-s3

And push the new branch to GitHub:

git push -u origin lab07-s3

Once on the lab07-s3 branch, I’ll need to follow this process:

Make changes

Make changes to the model files (i.e., _labs/lab07/py_model.qmd or _labs/lab07/r_model.qmd)

Test changes (locally)

To test changes locally, I’ll move into _labs/lab07, where the model files live. Then I’ll set the environment variables for S3 (or use local for testing):

cd _labs/lab07
export USE_S3=false  # or true if testing S3 locally

Then render/run the files with the changes:

quarto render py_model.qmd

Or

quarto render r_model.qmd

Commit changes

Commit the changes (be specific about the files):

git add _labs/lab07/py_model.qmd _labs/lab07/r_model.qmd
git commit -m "Update model configuration or training logic"

Push changes

Push to the lab07-s3 branch:

git push origin lab07-s3

GitHub Actions will automatically detect the push to lab07-s3, build the models using the latest code, and publish the versioned models to S3 with temporary credentials from gha-s3-publish-role.

Verify

The GitHub CLI (gh) lets us follow the workflow from the Terminal. First, list the most recent runs of publish.yml. Each row shows the status, the commit message, the branch, the run ID, and how long the run took:

gh run list --workflow publish.yml --limit 2

Pass the run ID from that list to gh run watch to follow the run until it finishes. The --exit-status flag makes the command fail when the run fails, so we don’t have to read the output to know the result:

gh run watch <run-id> --exit-status

To confirm the run used the role (and not an access key), search the Configure AWS credentials step in the run’s log:

gh run view <run-id> --log | grep -i -m5 "assum\|gha-s3-publish-role"
publish-models  Configure AWS credentials   2026-11-15T05:41:59.5159429Z   role-to-assume: arn:aws:iam::ACCOUNT_ID:role/gha-s3-publish-role
publish-models  Configure AWS credentials   2026-11-15T05:41:59.9075276Z Assuming role with OIDC
publish-models  Configure AWS credentials   2026-11-15T05:42:00.1552995Z Authenticated as assumedRoleId ROLE_ID:GitHubActions

Assuming role with OIDC means GitHub traded its token for temporary credentials, and GitHubActions is the session name the action gives the role. If the role is missing an S3 permission, the build steps fail with AccessDenied, so we also check that the log has none (0 means no matches):

gh run view <run-id> --log | grep -i -c "accessdenied\|not authorized"
0

To see where the time goes, gh api requests the run’s jobs from the GitHub REST API ({owner} and {repo} are filled in from the current repository). The --jq filter prints each step’s name and how many seconds it took. The Install R dependencies step should take about a minute and a half with use-public-rspm: true (compiling from source took most of the 25 minute first run):

gh api repos/{owner}/{repo}/actions/runs/<run-id>/jobs \
  --jq '.jobs[0].steps[] | "\(.name): \(((.completed_at|fromdate)-(.started_at|fromdate)))s"'
Set up job: 2s
Check out repository: 1s
Configure AWS credentials: 1s
Set up Python: 0s
Set up R: 40s
Set up Quarto: 7s
Install Python dependencies: 62s
Install R dependencies: 92s
Build and publish Python model: 12s
Build and publish R model: 6s
Post Set up Python: 0s
Post Configure AWS credentials: 0s
Post Check out repository: 0s
Complete job: 0s

We can also confirm R downloaded its packages from Package Manager by searching the run’s log for its address (packagemanager.posit.co). The __linux__ part of each URL means Package Manager sends a prebuilt binary for the runner’s Linux version:

gh run view <run-id> --log | grep -m3 "packagemanager.posit.co"

Finally, we verify the models were published by listing the files in the bucket. Each run adds a new version folder (named with the date and time, e.g., 20261106T183259Z-6a79d) under Python/models/penguin_lab07_model/ and R/models/penguin_lab07_model/:

# List files in S3
aws s3 ls s3://penguin-vetiver-model-data/ --recursive --profile dev-deploy
2026-07-10 06:52:22        498 Python/models/penguin_lab07_model/20260710T065220Z-91fdd/data.txt
2026-07-10 06:52:22        921 Python/models/penguin_lab07_model/20260710T065220Z-91fdd/penguin_lab07_model.joblib
2026-11-06 11:33:01        499 Python/models/penguin_lab07_model/20261106T183259Z-6a79d/data.txt
2026-11-06 11:33:01        921 Python/models/penguin_lab07_model/20261106T183259Z-6a79d/penguin_lab07_model.joblib
2026-11-14 22:45:32        499 Python/models/penguin_lab07_model/20261115T054530Z-91fdd/data.txt
2026-11-14 22:45:32        921 Python/models/penguin_lab07_model/20261115T054530Z-91fdd/penguin_lab07_model.joblib
2026-07-08 22:49:21        486 Python/models/penguin_model/20260618T104900Z-91fdd/data.txt
2026-07-08 22:49:21        921 Python/models/penguin_model/20260618T104900Z-91fdd/penguin_model.joblib
2026-07-10 08:23:33        317 R/models/penguin_lab07_model/20260710T152331Z-09946/data.txt
2026-07-10 08:23:33      16394 R/models/penguin_lab07_model/20260710T152331Z-09946/penguin_lab07_model.rds
2026-07-11 13:56:51        317 R/models/penguin_lab07_model/20260711T205650Z-09946/data.txt
2026-07-11 13:56:51      16394 R/models/penguin_lab07_model/20260711T205650Z-09946/penguin_lab07_model.rds
2026-07-11 13:59:46        317 R/models/penguin_lab07_model/20260711T205945Z-09946/data.txt
2026-07-11 13:59:46      16394 R/models/penguin_lab07_model/20260711T205945Z-09946/penguin_lab07_model.rds
2026-07-11 13:59:57        317 R/models/penguin_lab07_model/20260711T205956Z-09946/data.txt
2026-07-11 13:59:58      16394 R/models/penguin_lab07_model/20260711T205956Z-09946/penguin_lab07_model.rds
2026-07-11 14:02:54        317 R/models/penguin_lab07_model/20260711T210253Z-09946/data.txt
2026-07-11 14:02:55      16394 R/models/penguin_lab07_model/20260711T210253Z-09946/penguin_lab07_model.rds
2026-11-06 11:33:07        317 R/models/penguin_lab07_model/20261106T183305Z-c812e/data.txt
2026-11-06 11:33:07      16402 R/models/penguin_lab07_model/20261106T183305Z-c812e/penguin_lab07_model.rds
2026-11-06 11:45:27        317 R/models/penguin_lab07_model/20261106T184526Z-c812e/data.txt
2026-11-06 11:45:27      16402 R/models/penguin_lab07_model/20261106T184526Z-c812e/penguin_lab07_model.rds
2026-11-06 12:52:11        317 R/models/penguin_lab07_model/20261106T195210Z-c812e/data.txt
2026-11-06 12:52:11      16402 R/models/penguin_lab07_model/20261106T195210Z-c812e/penguin_lab07_model.rds
2026-11-06 12:58:48        317 R/models/penguin_lab07_model/20261106T195846Z-c812e/data.txt
2026-11-06 12:58:48      16402 R/models/penguin_lab07_model/20261106T195846Z-c812e/penguin_lab07_model.rds
2026-11-14 22:45:38        317 R/models/penguin_lab07_model/20261115T054536Z-54566/data.txt
2026-11-14 22:45:38      16393 R/models/penguin_lab07_model/20261115T054536Z-54566/penguin_lab07_model.rds
2026-07-05 16:42:14        305 R/models/penguin_model/20260628T205736Z-8856d/data.txt
2026-07-05 16:42:14      16393 R/models/penguin_model/20260628T205736Z-8856d/penguin_model.rds
2026-07-08 22:49:35        305 R/models/penguin_model/20260709T053628Z-09946/data.txt
2026-07-08 22:49:35      16394 R/models/penguin_model/20260709T053628Z-09946/penguin_model.rds
2026-07-09 22:19:06        305 R/models/penguin_model/20260710T051905Z-09946/data.txt
2026-07-09 22:19:06      16394 R/models/penguin_model/20260710T051905Z-09946/penguin_model.rds
2026-07-09 22:23:32        305 R/models/penguin_model/20260710T052331Z-09946/data.txt
2026-07-09 22:23:32      16394 R/models/penguin_model/20260710T052331Z-09946/penguin_model.rds

Merge to main

Then switch back to the main branch:

git checkout main

This will merge the changes in lab07-s3 to main (if the changes also belong in the book).

git merge lab07-s3

Push changes to main.

git push origin main

The push to main also publishes the book: .github/workflows/book.yml renders it and deploys it to the gh-pages branch (GitHub Actions as CI/CD), so we don’t run quarto publish ourselves.

Recap

  1. GitHub authenticates to AWS with OIDC instead of an access key. We added GitHub as an identity provider and created gha-s3-publish-role. Only the lab07-s3 branch of this repository can assume the role, and it can only list, read, and write objects in the model bucket. The role ARN and AWS_REGION go in the repository’s Variables, and each run gets credentials that expire after an hour.

  2. publish.yml runs on pushes to the lab07-s3 branch that change py_model.qmd, r_model.qmd, or the workflow file itself. It installs Python and R, then renders both model documents with USE_S3=true, so each run writes a new vetiver version to S3.

  3. Model changes happen on lab07-s3 (make changes, test locally, commit, push, and verify with aws s3 ls), which keeps main reserved for rendering the book. Changes that also belong in the book get merged back into main.

Lab 8 moves to the command line and how SSH keys work.


  1. Note that file is stored in the root directory (.github/workflows/), where all workflow files belong.↩︎