Skip to content

Azure Images

Azure Images #432

name: Azure Images
on:
schedule:
- cron: "45 4 * * *"
# push:
# branches:
# - main
workflow_dispatch:
inputs:
environment:
description: 'Choose your deployment target environment'
required: true
default: 'staging'
type: choice
options:
- 'production'
- 'staging'
permissions:
actions: write
contents: read
id-token: write
env:
AWS_DEFAULT_OUTPUT: json
AWS_PAGER: ""
jobs:
setup:
name: Setup workflow envs
runs-on: ubuntu-latest
outputs:
environment: ${{ steps.set-environment.outputs.environment }}
bucket: ${{ steps.set-bucket.outputs.bucket }}
steps:
# Only set target environment to production if the workflow is triggered by
# a scheduled execution or workflow_dispatch with production input selected
- name: Setup target environment
id: set-environment
run: |
if [ "${{ github.event_name }}" = "schedule" ] || [ "${{ inputs.environment }}" = "production" ]; then
echo "environment=production" >> $GITHUB_OUTPUT
else
echo "environment=staging" >> $GITHUB_OUTPUT
fi
- name: Setup target bucket
# Set target bucket according to target environment
id: set-bucket
run: |
if [ "${{ github.event_name }}" = "schedule" ] || [ "${{ inputs.environment }}" = "production" ]; then
echo "bucket=cloudx-json-bucket" >> $GITHUB_OUTPUT
else
echo "bucket=cid-bucket-staging" >> $GITHUB_OUTPUT
fi
list_regions:
name: "List Regions"
runs-on: ubuntu-latest
needs: setup
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
env:
ENVIRONMENT: ${{ needs.setup.outputs.environment }}
steps:
- name: Clone repo
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Checkout latest tagged release
if: env.ENVIRONMENT == 'production'
run: |
LATEST_RELEASE=$(git describe --tags `git rev-list --tags --max-count=1`)
git checkout $LATEST_RELEASE
- name: "Az CLI login"
uses: azure/login@v2
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Get Azure locations
run: |
az account list-locations | \
jq -r '.[] | select(.metadata.regionCategory == "Recommended") | .name' | \
grep -v euap | jq --raw-input --slurp 'split("\n") | sort | del(.[0])' \
> regions.json
- name: Generate a list of regions for the actions matrix
id: set-matrix
run: |
export REGIONS=$(cat regions.json)
echo -n 'matrix={"region": ' >> $GITHUB_OUTPUT
echo -n $REGIONS >> $GITHUB_OUTPUT
echo '}' >> $GITHUB_OUTPUT
# Temporary short circuit because Azure's API is incredibly slow. 👀
echo -n 'matrix={"region": ["eastus"]}' > $GITHUB_OUTPUT
list_images:
name: "List Images"
needs: [list_regions, setup]
runs-on: ubuntu-latest
strategy:
fail-fast: false
max-parallel: 5
matrix: ${{fromJSON(needs.list_regions.outputs.matrix)}}
env:
REGION: ${{ matrix.region }}
ENVIRONMENT: ${{ needs.setup.outputs.environment }}
steps:
- name: Clone repo
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Checkout latest tagged release
if: env.ENVIRONMENT == 'production'
run: |
LATEST_RELEASE=$(git describe --tags `git rev-list --tags --max-count=1`)
git checkout $LATEST_RELEASE
- name: "Az CLI login"
uses: azure/login@v2
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Write region data
uses: nick-fields/retry@v3
with:
timeout_minutes: 15
max_attempts: 3
command: |
az vm image list --offer rh-rhel --publisher RedHat --all --only-show-errors -o json -l $REGION | jq -c > ${REGION}.json
zstd -v ${REGION}.json
- name: Store image data in artifact
uses: actions/upload-artifact@v4
with:
name: "azure"
path: "${{ env.REGION }}.json.zst"
upload_data:
name: "Upload Data"
runs-on: ubuntu-latest
needs: [list_images, setup]
env:
ENVIRONMENT: ${{ needs.setup.outputs.environment }}
BUCKET: ${{ needs.setup.outputs.bucket }}
steps:
- name: Clone repo
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Checkout latest tagged release
if: env.ENVIRONMENT == 'production'
run: |
LATEST_RELEASE=$(git describe --tags `git rev-list --tags --max-count=1`)
git checkout $LATEST_RELEASE
- name: Download image data artifacts
uses: actions/download-artifact@v4
with:
path: raw
- name: Install awscli v2
uses: unfor19/install-aws-cli-action@v1
- name: Configure AWS credentials for production
uses: aws-actions/configure-aws-credentials@v4
if: env.ENVIRONMENT == 'production'
with:
role-to-assume: arn:aws:iam::426579533370:role/github_actions_image_retriever
role-duration-seconds: 1800
aws-region: us-east-1
- name: Configure AWS credentials for staging
uses: aws-actions/configure-aws-credentials@v4
if: env.ENVIRONMENT == 'staging'
with:
role-to-assume: arn:aws:iam::426579533370:role/github_actions_cloud_image_directory_staging
role-duration-seconds: 1800
aws-region: us-east-1
- name: Decompress artifacts
run: |
pushd raw/azure
zstd -vd --rm *
popd
- name: Upload raw data to S3
run: |
aws s3 sync \
--acl public-read \
--delete \
--no-progress \
--content-type application/json \
$(pwd)/raw/azure/ s3://${BUCKET}/raw/azure/