Skip to content

Commit

Permalink
Add check_certs.yml and associated Fastfile changes to check and re…
Browse files Browse the repository at this point in the history
…voke certificates if less than 30 days to expiry. Launch `create_certs.yml`if certs are revoked.

Commenting out cert.revoke! for testing.
  • Loading branch information
bjornoleh committed Jan 7, 2025
1 parent 28c190b commit f33c006
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/check_certs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Check Certificates
run-name: Check Certificates (${{ github.ref_name }})

on:
workflow_dispatch:

jobs:
check_certs:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: '3.1'

- name: Install dependencies
run: bundle install

- name: Check and Revoke Certificates
env:
FASTLANE_USER: ${{ secrets.APPLE_ID }}
FASTLANE_PASSWORD: ${{ secrets.APPLE_PASSWORD }}
run: fastlane check_and_revoke_certificates

trigger_create_certs:
needs: check_certs
if: env.cert_revoked == 'true'
uses: ./.github/workflows/create_certs.yml
3 changes: 1 addition & 2 deletions .github/workflows/create_certs.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
name: 3. Create Certificates
run-name: Create Certificates (${{ github.ref_name }})
on:
workflow_dispatch:
on: [workflow_call, workflow_dispatch]

jobs:
validate:
Expand Down
34 changes: 34 additions & 0 deletions fastlane/Fastfile
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,38 @@ platform :ios do
UI.success("Certificates renewed successfully.")
end
end

lane :check_and_revoke_certificates do
require 'spaceship'

Spaceship::Portal.login(ENV['FASTLANE_USER'], ENV['FASTLANE_PASSWORD'])

revoked = false

# Fetch all certificates
certificates = Spaceship::Portal.certificate.all

# Filter for Production/Distribution certificates
distribution_certs = certificates.select { |cert| cert.kind_of?(Spaceship::Portal::Certificate::Production) }

# Check for expiration
distribution_certs.each do |cert|
expiration_date = cert.expires
puts "Checking Distribution Certificate: #{cert.id}, Expiration: #{expiration_date}"

if expiration_date < Time.now + 30 * 24 * 60 * 60 # Less than 30 days to expiry
puts "Certificate #{cert.id} is expiring soon or already expired. Revoking..."
#cert.revoke!
revoked = true
end
end

if revoked
puts "Certificates were revoked. Triggering workflow to recreate them."
Actions.sh("echo 'cert_revoked=true' >> $GITHUB_ENV") # Set GitHub Actions environment variable
else
puts "All certificates are valid. No action required."
Actions.sh("echo 'cert_revoked=false' >> $GITHUB_ENV") # Set GitHub Actions environment variable
end
end
end

0 comments on commit f33c006

Please sign in to comment.