Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Last active July 15, 2022 05:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guitarrapc/d161ec8032a3ff25090f1bec9bb9e7eb to your computer and use it in GitHub Desktop.
Save guitarrapc/d161ec8032a3ff25090f1bec9bb9e7eb to your computer and use it in GitHub Desktop.
Pulumi to create ACR and ACR Task to purge image automatically
using Pulumi.AzureNative.ContainerRegistry;
using Pulumi.AzureNative.ContainerRegistry.Inputs;
var opt = new CustomResourceOptions { Parent = this };
var cmd = "acr purge --filter 'samples/devimage1:.*' --filter 'samples/devimage2:.*' --ago 0d --untagged --dry-run";
var acr = new Registry("nanika", new RegistryArgs
{
RegistryName = "nantoka",
Sku = new SkuArgs
{
Name = SkuName.Basic,
},
AdminUserEnabled = true, // 無効推奨。だが、ContainerApp で必要: https://azure.github.io/PSRule.Rules.Azure/en/rules/Azure.ACR.AdminUser/
ResourceGroupName = "foo-group",
Location = "japaneast",
}, opt);
_ = new Pulumi.AzureNative.ContainerRegistry.Task("nanika-task", new TaskArgs
{
TaskName = "purgeTask",
AgentConfiguration = new AgentPropertiesArgs
{
Cpu = 2,
},
Platform = new PlatformPropertiesArgs
{
Architecture = Architecture.Amd64,
Os = OS.Linux,
},
Step = new EncodedTaskStepArgs
{
EncodedTaskContent = ToEncodedTaskString(cmd),
Type = "EncodedTask",
},
Status = TaskStatus.Enabled,
Timeout = 3600,
Trigger = new TriggerPropertiesArgs
{
BaseImageTrigger = new BaseImageTriggerArgs
{
BaseImageTriggerType = BaseImageTriggerType.Runtime,
Name = "defaultBaseimageTriggerName",
Status = TriggerStatus.Enabled,
UpdateTriggerPayloadType = UpdateTriggerPayloadType.Default,
},
TimerTriggers = new [] {
new TimerTriggerArgs
{
Name = "t1",
Schedule = "0 0 * * *", // UTCベース
Status = TriggerStatus.Enabled,
},
}
},
RegistryName = acr.Name,
ResourceGroupName = "foo-group",
Location = "japaneast",
}, opt);
private static string ToEncodedTaskString(string cmd)
{
const string format = @"version: v1.1.0
steps:
- cmd: {0}
disableWorkingDirectoryOverride: true
timeout: 3600";
var step = string.Format(format, cmd);
return Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(step));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment