Skip to content

Commit

Permalink
fix(ci): setup Terraform on ubuntu-latest (#452)
Browse files Browse the repository at this point in the history
Addresses the `terraform` CLI being removed from new GitHub Actions
runner images, which is preventing automated provider upgrades to happen
for all of our providers that don't use custom runners.
  • Loading branch information
xiehan authored Oct 3, 2024
1 parent bd89d6a commit a755c46
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 77 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ const githubActionPinnedVersions = {
"0723387faaf9b38adef4775cd42cfd5155ed6017", // v5.5.3
"dessant/lock-threads": "1bf7ec25051fe7c00bdd17e6a7cf3d7bfb7dc771", // v5.0.1
"hashicorp/setup-copywrite": "32638da2d4e81d56a0764aa1547882fc4d209636", // v1.1.3
"hashicorp/setup-terraform": "b9cd54a3c349d3f38e8881555d616ced269862dd", // v3.1.2
"imjohnbo/issue-bot": "572eed14422c4d6ca37e870f97e7da209422f5bd", // v3.4.4
"peter-evans/create-pull-request": "c5a7806660adbe173f04e3e038b0ccdcd758773c", // v6.1.0
"slackapi/slack-github-action": "70cd7be8e40a46e8b0eced40b0de447bdb42f68e", // v1.26.0
Expand Down
169 changes: 92 additions & 77 deletions src/provider-upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import { javascript } from "projen";
import { JobPermission } from "projen/lib/github/workflows-model";
import { JobPermission, JobStep } from "projen/lib/github/workflows-model";
import { generateRandomCron } from "./util/random-cron";

interface ProviderUpgradeOptions {
Expand Down Expand Up @@ -47,88 +47,103 @@ export class ProviderUpgrade {
const newVersion = "${{ steps.new_version.outputs.value }}";
const semanticType = "${{ steps.release.outputs.type }}";

const steps: JobStep[] = [
{
name: "Checkout",
uses: "actions/checkout@v4",
},
{
name: "Setup Node.js",
uses: "actions/setup-node",
with: {
"node-version": project.minNodeVersion,
},
},
{ run: "yarn install" },
{
id: "check_version",
run: "yarn check-if-new-provider-version",
},
{
name: "get provider current version",
if: newerVersionAvailable,
id: "current_version",
run: `echo "value=$(jq -r '.cdktf.provider.version' package.json)" >> $GITHUB_OUTPUT`,
},
{
run: "yarn fetch",
if: newerVersionAvailable,
env: {
CHECKPOINT_DISABLE: "1",
GH_TOKEN: "${{ secrets.GITHUB_TOKEN }}",
},
},
{
name: "get provider updated version",
if: newerVersionAvailable,
id: "new_version",
run: `echo "value=$(jq -r '. | to_entries[] | .value' src/version.json)" >> $GITHUB_OUTPUT`,
},
{
name: "Determine if this is a minor or patch release",
if: newerVersionAvailable,
id: "release",
env: {
CURRENT_VERSION: currentVersion,
NEW_VERSION: newVersion,
},
run: [
`CURRENT_VERSION_MINOR=$(cut -d "." -f 2 <<< "$CURRENT_VERSION")`,
`NEW_VERSION_MINOR=$(cut -d "." -f 2 <<< "$NEW_VERSION")`,
`[[ "$CURRENT_VERSION_MINOR" != "$NEW_VERSION_MINOR" ]] && IS_MINOR_RELEASE=true || IS_MINOR_RELEASE=false`,
`[[ "$IS_MINOR_RELEASE" == "true" ]] && SEMANTIC_TYPE=feat || SEMANTIC_TYPE=fix`,
`echo "is_minor=$IS_MINOR_RELEASE" >> $GITHUB_OUTPUT`,
`echo "type=$SEMANTIC_TYPE" >> $GITHUB_OUTPUT`,
].join("\n"),
},
// generate docs
{ run: "yarn compile", if: newerVersionAvailable },
{ run: "yarn docgen", if: newerVersionAvailable },
// submit a PR
{
name: "Create Pull Request",
if: newerVersionAvailable,
uses: "peter-evans/create-pull-request@v3",
with: {
branch: "auto/provider-upgrade",
"commit-message": `${semanticType}: upgrade provider from \`${currentVersion}\` to version \`${newVersion}\``,
title: `${semanticType}: upgrade provider from \`${currentVersion}\` to version \`${newVersion}\``,
body: `This PR upgrades the underlying Terraform provider to version ${newVersion}`,
labels: "automerge,auto-approve",
token: "${{ secrets.GH_TOKEN }}",
"delete-branch": true,
committer: "team-tf-cdk <[email protected]>",
author: "Team Terraform CDK <[email protected]>",
signoff: true,
},
},
];

// @TODO Figure out if this is really necessary; this has not been tested
// But I saw https://github.com/hashicorp/setup-terraform/issues/425
// so I added this "if" statement as a precaution
if (options.workflowRunsOn.includes("ubuntu-latest")) {
steps.splice(2, 0, {
name: "Setup Terraform",
uses: "hashicorp/setup-terraform",
with: {
terraform_wrapper: false,
},
});
}

workflow.addJobs({
upgrade: {
runsOn: options.workflowRunsOn,
env: {
NODE_OPTIONS: `--max-old-space-size=${options.nodeHeapSize}`,
},
steps: [
{
name: "Checkout",
uses: "actions/checkout@v4",
},
{
name: "Setup Node.js",
uses: "actions/setup-node",
with: {
"node-version": project.minNodeVersion,
},
},
{ run: "yarn install" },
{
id: "check_version",
run: "yarn check-if-new-provider-version",
},
{
name: "get provider current version",
if: newerVersionAvailable,
id: "current_version",
run: `echo "value=$(jq -r '.cdktf.provider.version' package.json)" >> $GITHUB_OUTPUT`,
},
{
run: "yarn fetch",
if: newerVersionAvailable,
env: {
CHECKPOINT_DISABLE: "1",
GH_TOKEN: "${{ secrets.GITHUB_TOKEN }}",
},
},
{
name: "get provider updated version",
if: newerVersionAvailable,
id: "new_version",
run: `echo "value=$(jq -r '. | to_entries[] | .value' src/version.json)" >> $GITHUB_OUTPUT`,
},
{
name: "Determine if this is a minor or patch release",
if: newerVersionAvailable,
id: "release",
env: {
CURRENT_VERSION: currentVersion,
NEW_VERSION: newVersion,
},
run: [
`CURRENT_VERSION_MINOR=$(cut -d "." -f 2 <<< "$CURRENT_VERSION")`,
`NEW_VERSION_MINOR=$(cut -d "." -f 2 <<< "$NEW_VERSION")`,
`[[ "$CURRENT_VERSION_MINOR" != "$NEW_VERSION_MINOR" ]] && IS_MINOR_RELEASE=true || IS_MINOR_RELEASE=false`,
`[[ "$IS_MINOR_RELEASE" == "true" ]] && SEMANTIC_TYPE=feat || SEMANTIC_TYPE=fix`,
`echo "is_minor=$IS_MINOR_RELEASE" >> $GITHUB_OUTPUT`,
`echo "type=$SEMANTIC_TYPE" >> $GITHUB_OUTPUT`,
].join("\n"),
},
// generate docs
{ run: "yarn compile", if: newerVersionAvailable },
{ run: "yarn docgen", if: newerVersionAvailable },
// submit a PR
{
name: "Create Pull Request",
if: newerVersionAvailable,
uses: "peter-evans/create-pull-request@v3",
with: {
branch: "auto/provider-upgrade",
"commit-message": `${semanticType}: upgrade provider from \`${currentVersion}\` to version \`${newVersion}\``,
title: `${semanticType}: upgrade provider from \`${currentVersion}\` to version \`${newVersion}\``,
body: `This PR upgrades the underlying Terraform provider to version ${newVersion}`,
labels: "automerge,auto-approve",
token: "${{ secrets.GH_TOKEN }}",
"delete-branch": true,
committer: "team-tf-cdk <[email protected]>",
author: "Team Terraform CDK <[email protected]>",
signoff: true,
},
},
],
steps,
permissions: {
pullRequests: JobPermission.WRITE,
issues: JobPermission.WRITE,
Expand Down
12 changes: 12 additions & 0 deletions test/__snapshots__/index.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a755c46

Please sign in to comment.