GitHub Actions can be used to do something whenever something happens in one of your GitHub repositories. If you are not familiar with GitHub Actions, check out my article Getting Started with GitHub Actions.
For example, let's say you have a repository named my-repo that contains files that you store in an Amazon Web Services (AWS) S3 Bucket. For example, perhaps your my-repo repository contains two JPGs, foo.jpg and bar.jpg, perhaps like this.
- https://github.com/JohnDoe/my-repo/blob/main/images/foo.jpg
- https://github.com/JohnDoe/my-repo/blob/main/images/bar.jpg
For example, your GitHub Action YAML could have something like this. Notice in this example that OpenID Connect (OIDC) is being used to authenticate to Amazon Web Services. If you are not familair with this, check out my article Authenticate to Amazon Web Services (AWS) in GitHub Action.
name: GitHub Action
run-name: ${{ github.workflow }} run by ${{ github.actor }}
on:
push:
branches:
- main
permissions:
id-token: write
contents: read
jobs:
github-action-job:
runs-on: ubuntu-latest
steps:
- name: Checking out the repository code . . .
uses: actions/checkout@v4
- name: aws-actions/configure-aws-credentials@v2
uses: aws-actions/configure-aws-credentials@v2
with:
role-to-assume: arn:aws:iam::123456789012:role/GitHubAction-AssumeRoleWithAction
role-session-name: GitHub_to_AWS_via_FederatedOIDC
aws-region: us-east-1
- name: aws s3api put-object
run: aws s3api put-object --bucket my-bucket-abcdefg --key my-repo/images/foo.png --body my-repo/images/foo.png
Better yet, you could use tj-actions/changed-files to get the list of files that have been changed in the repository and then only upload files that have changed.
name: GitHub Action
run-name: ${{ github.workflow }} run by ${{ github.actor }}
on:
push:
branches:
- main
permissions:
id-token: write
contents: read
jobs:
github-action-job:
runs-on: ubuntu-latest
steps:
- name: Checking out the repository code
uses: actions/checkout@v4
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v40
- name: aws-actions/configure-aws-credentials@v4
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/GitHubAction-AssumeRoleWithAction
role-session-name: GitHub_to_AWS_via_FederatedOIDC
aws-region: us-east-1
- name: aws s3api put-object
for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
aws s3api put-object --bucket my-bucket-abcdefg --key my-repo/images/foo.png --body my-repo/images/foo.png
done
Did you find this article helpful?
If so, consider buying me a coffee over at